From d498d6664606a42b5ab632a80dfdba6636cb053b Mon Sep 17 00:00:00 2001 From: Ofer Morag Date: Sat, 11 Apr 2026 09:32:12 +0300 Subject: [PATCH] fix(ios): evict stale FetchCache entries on barrier queue Avoid mutating results inside queue.sync read path; eviction runs async with .barrier and re-checks age to reduce races with concurrent readers. Made-with: Cursor --- .../react-native-nitro-fetch/ios/FetchCache.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/react-native-nitro-fetch/ios/FetchCache.swift b/packages/react-native-nitro-fetch/ios/FetchCache.swift index d915d62..6e82f75 100644 --- a/packages/react-native-nitro-fetch/ios/FetchCache.swift +++ b/packages/react-native-nitro-fetch/ios/FetchCache.swift @@ -40,13 +40,24 @@ final class FetchCache { static func getResultIfFresh(_ key: String, maxAgeMs: Int64) -> NitroResponse? { var out: NitroResponse? + var shouldEvict = false queue.sync { if let entry = results[key] { let age = Int64(Date().timeIntervalSince1970 * 1000) - entry.timestampMs if age <= maxAgeMs { out = entry.response } else { - results.removeValue(forKey: key) + shouldEvict = true + } + } + } + if shouldEvict { + queue.async(flags: .barrier) { + if let entry = results[key] { + let age = Int64(Date().timeIntervalSince1970 * 1000) - entry.timestampMs + if age > maxAgeMs { + results.removeValue(forKey: key) + } } } }