fix: avoid trapping on non-finite prefetch TTLs and disable hits for TTL <= 0 - #244
Merged
riteshshukla04 merged 1 commit intoOct 3, 2026
Merged
Conversation
…TTL <= 0 On iOS, Int64(prefetchCacheTtlMs) traps for NaN, Infinity and out-of-range values. Compare the TTL as a Double instead. Both caches now also require a positive TTL, so a zero TTL no longer hits when the response completed in the same millisecond. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@huytdps13400 is attempting to deploy a commit to the Margelo Team on Vercel. A member of the Team first needs to authorize it. |
Member
|
Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #196
Root cause
FetchCache.getResultIfFreshcall sites inHybridNitroFetchClient.swiftconverted the TTL withInt64(req.prefetchCacheTtlMs ?? 5_000). Swift'sInt64(Double)traps on NaN, ±Infinity and values outside the Int64 range.typeof NaN === 'number', soprefetchCacheTtlMs: NaNorInfinitypassed from JS (or anNSNumberpassed toNitroAutoPrefetcher.registerPrefetch) crashes the app. Android'sDouble.toLong()saturates instead, so it doesn't crash there.age <= maxAgeMs. When the response completed in the same millisecond,ageis0, so a TTL of0returned a hit. The docs say a value<= 0disables cache hits. On Android,NaN.toLong()is0, so a NaN TTL hit the same way.Changes
ios/FetchCache.swift:getResultIfFreshnow takesmaxAgeMs: Doubleand comparesDouble(age) <= maxAgeMs, so there's no trapping conversion. It also requiresmaxAgeMs > 0. The three call sites passreq.prefetchCacheTtlMs ?? 5_000directly.android/.../FetchCache.kt:getResultIfFreshandhasFreshResultalso requiremaxAgeMs > 0.docs-website/docs/prefetch.md: I removed the parenthetical that described the oldage <= maxAgeMsmechanism.After this change both platforms handle unusual TTLs the same way:
<= 0, NaN and-Infinitynever hit,Infinity(and huge values) never expire, and ordinary TTLs work as before. Joining an in-flight prefetch still works whatever the TTL. The JS builders are unchanged.Verification
I couldn't drive the full prefetch flow on a device in this environment. Instead I tested the real
FetchCache.swiftandFetchCache.ktfiles in small standalone harnesses, with a stubNitroResponseand the same call-site expressions:0(same ms)NaNInfinity1e19-5/-160000Other checks:
./gradlew :react-native-nitro-fetch:compileDebugKotlininexample/androidpasses.xcodebuild -scheme NitroFetch -sdk iphonesimulator(Debug, arm64) for theNitroFetchpod in the example workspace succeeds.bun typecheck,bun lint(0 errors) andbun testall pass.Platforms: iOS (crash and zero-TTL fix) and Android (zero-TTL fix).
This fix was prepared with AI assistance (Claude Code) and verified locally with the tests above.
🤖 Generated with Claude Code