Repository navigation
fix(cms,sdk): timeout module-level inflight Promise caches to evict zombies - #224
Merged
Merged
Conversation
…ombies Four module-level `Map<string, Promise<...>>` caches evicted entries only via `.finally()` on the stored Promise. If the underlying work never settled (downstream `fetch()` hangs, awaited Promise never resolves), the `.finally()` never ran and the Map entry leaked forever — every subsequent request for the same key joined the zombie Promise, pinning request context in memory until `exceededMemory`. Combined with `no_handle_cross_request_promise_resolution: true` on the consumer Worker (which is the recommended setup for cross-request dedup), the zombie Promise survived across requests in the same isolate. Two of the four caches (`layoutInflight`, `resolvedLayoutInflight`) use request-agnostic keys (just the block reference name like "Header - 01"), so a single hung Promise blocked every request on the isolate, not just same-URL requests. Caches fixed: - `src/sdk/cachedLoader.ts` — `inflightRequests` - `src/cms/sectionLoaders.ts` — `sectionLoaderInflight` - `src/cms/sectionLoaders.ts` — `layoutInflight` - `src/cms/resolve.ts` — `resolvedLayoutInflight` (two set-sites) Adds a shared `withInflightTimeout` helper that races each stored Promise against a 10s timeout so the `.finally()` cleanup always runs. Observed prod impact this resolves (24h window on a TanStack Start storefront): 514 hard exceededMemory crashes, 403 canceled, 371 HTTP 503s, with one PLP route holding 82% of events — consistent with a single poisoned cache key blocking every request for that URL. CPU near zero + wall_time in tens of minutes per isolate confirmed workers were sleeping on a zombie Promise, not computing. Closes #223
|
🎉 This PR is included in version 6.4.7 🎉 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
withInflightTimeouthelper (src/sdk/inflightTimeout.ts) that races a Promise against a 10s timeout so its terminal state is guaranteed.Map<string, Promise<...>>cache sites — so.finally()always runs and the Map slot is freed even when the underlying work hangs.fetch()poisoned the cache entry; every subsequent request for the same key joined the zombie Promise, pinning context in memory untilexceededMemoryterminated the isolate.Closes #223.
Caches fixed
src/sdk/cachedLoader.tsinflightRequests${name}::${djb2(props)}src/cms/sectionLoaders.tssectionLoaderInflight${component}::${djb2(props)}src/cms/sectionLoaders.tslayoutInflightsrc/cms/resolve.tsresolvedLayoutInflight(two set-sites)The two request-agnostic caches were the worst case — a single hung
Header - 01resolution blocked every request on the isolate, not just same-URL requests.Production impact this resolves
Observed on a TanStack Start storefront over a 24h window:
/escolar/malas/outlet/mochilas514 hard exceededMemory crashes + 403 canceled + 371 HTTP 503s in 24h. CPU near zero with wall time in the tens of minutes per isolate — workers sleeping on zombie Promises, not computing. One PLP route held 82% of events, consistent with a single poisoned cache key.
Test plan
npm run typecheckcleannpm test— 51 files, 748 tests pass (including newinflightTimeout.test.tscovering settle / reject / timeout cases)await new Promise(() => {})in a section loader) and confirm the matching inflight Map evicts after 10s and the next request retries instead of joining the dead PromiseNotes for reviewers
DEFAULT_INFLIGHT_TIMEOUT_MS) is intentionally short relative to typical Worker invocation budgets. The point is to surface hangs as evictions rather than masking them as long waits — if a legitimate loader takes longer than 10s we want to know.layoutInflight,resolvedLayoutInflight), I considered also bounding entries by a TTL on the inflight Map itself. The timeout is sufficient because once an entry can't outlive 10s, the LRU/TTL on the resolved-cache side handles the rest.Related
Companion fix in
@decocms/apps: decocms/apps-start#74 (same pattern invtex/utils/fetchCache.ts).Summary by cubic
Adds a 10s timeout guard to all module-level inflight Promise caches to evict hung entries and stop zombie cache leaks. This prevents stuck workers and reduces exceeded-memory crashes.
withInflightTimeoutto race cached inflight work against a timeout so.finally()cleanup always runs.inflightRequests(src/sdk/cachedLoader.ts),sectionLoaderInflightandlayoutInflight(src/cms/sectionLoaders.ts), andresolvedLayoutInflight(src/cms/resolve.ts).fetch()hangs.src/sdk/inflightTimeout.test.tscovering settle, reject, and timeout cases.Written for commit ea75422. Summary will update on new commits.