fix(vtex/fetchCache): timeout inflight Promise to evict zombie cache entries#74
Merged
Merged
Conversation
…entries Module-level `inflight: Map<string, Promise<CacheEntry>>` only evicted entries via `.finally()` on the stored Promise. If the underlying `fetch()` never settled (TCP connection dropped without FIN, CDN holds response open), the `.finally()` never ran and the Map entry leaked forever — every subsequent request for the same cache key joined the zombie Promise, pinning context references in memory until `exceededMemory`. Wrap `executeFetch` with a 10s timeout so `.finally()` always runs. The underlying hung fetch is abandoned (CF runtime will GC it after the request ends) but the cache slot is freed and the next request retries. Observed production impact on a TanStack Start storefront (24h window): - 514 hard exceededMemory crashes - 403 canceled requests - 371 HTTP 503s - One PLP route accounted for 82% of events (single poisoned cache key) - CPU near zero + wall_time in tens of minutes = isolates sleeping awaiting the zombie Promise, not computing Closes #73
|
🎉 This PR is included in version 4.0.1 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
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.
Summary
executeFetchcalls invtex/utils/fetchCache.tswith a 10s timeout so the.finally()cleanup always runs, even if the underlying VTEXfetch()never settles.exceededMemory.Closes #73.
Production impact this resolves
Observed on a TanStack Start storefront over a 24h window:
/escolar/malas/outlet/mochilasCPU near zero + wall time in the tens of minutes = isolates sleeping awaiting the zombie Promise. One route held 82% of events — consistent with a single poisoned cache key blocking every request for that URL.
Test plan
npx vitest run vtex/utils/__tests__/fetchCache.test.ts— 8/8 pass including newevicts the inflight slot when the fetch never settlescasenpm run typecheckcleangetFetchCacheStats().inflightstays at 0 after the timeoutRelated
Companion fix for the four equivalent inflight Maps in
@decocms/start: decocms/deco-start#223 (PR forthcoming).Summary by cubic
Adds a 10s timeout around
fetchWithCacheinflight requests to evict zombie cache entries when VTEXfetch()hangs, preventing memory leaks and blocked requests. Also applies to stale-refresh and includes a regression test.executeFetchwithwithTimeout(10s) so.finally()always runs and clears theinflightslot.refreshingstaying true forever.inflightMap is evicted after the timeout.Written for commit 914553a. Summary will update on new commits.