Skip to content

fix(cms,sdk): timeout module-level inflight Promise caches to evict zombies - #224

Merged
JonasJesus42 merged 1 commit into
mainfrom
fix/inflight-cache-promise-leak
Jun 4, 2026
Merged

JonasJesus42 merged 1 commit into
mainfrom
fix/inflight-cache-promise-leak

Conversation

@JonasJesus42

@JonasJesus42 JonasJesus42 commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Adds a shared withInflightTimeout helper (src/sdk/inflightTimeout.ts) that races a Promise against a 10s timeout so its terminal state is guaranteed.
  • Applies it at all four module-level Map<string, Promise<...>> cache sites — so .finally() always runs and the Map slot is freed even when the underlying work hangs.
  • Without this, a single hung downstream fetch() poisoned the cache entry; every subsequent request for the same key joined the zombie Promise, pinning context in memory until exceededMemory terminated the isolate.

Closes #223.

Caches fixed

File Cache Key
src/sdk/cachedLoader.ts inflightRequests ${name}::${djb2(props)}
src/cms/sectionLoaders.ts sectionLoaderInflight ${component}::${djb2(props)}
src/cms/sectionLoaders.ts layoutInflight block ref name (request-agnostic)
src/cms/resolve.ts resolvedLayoutInflight (two set-sites) block ref name (request-agnostic)

The two request-agnostic caches were the worst case — a single hung Header - 01 resolution 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:

Path Events exceededMemory Avg CPU Avg wall
/escolar 849 333 743ms 197s
/malas 20 20 183ms 1738s
/outlet 20 20 9ms 2064s
/mochilas 6 6 2ms 3846s

514 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 typecheck clean
  • npm test — 51 files, 748 tests pass (including new inflightTimeout.test.ts covering settle / reject / timeout cases)
  • Staging validation: force a hung loader (e.g. 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 Promise

Notes for reviewers

  • The 10s default (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.
  • The timeout only triggers if the Promise hasn't settled — fast-path callers (cache HIT, resolved value) never touch the timer.
  • For request-agnostic keys (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 in vtex/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.

  • Bug Fixes
    • Introduced withInflightTimeout to race cached inflight work against a timeout so .finally() cleanup always runs.
    • Applied to inflightRequests (src/sdk/cachedLoader.ts), sectionLoaderInflight and layoutInflight (src/cms/sectionLoaders.ts), and resolvedLayoutInflight (src/cms/resolve.ts).
    • Ensures request-agnostic layout keys can’t block an isolate when a downstream fetch() hangs.
    • Added src/sdk/inflightTimeout.test.ts covering settle, reject, and timeout cases.

Written for commit ea75422. Summary will update on new commits.

Review in cubic

…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
@JonasJesus42
JonasJesus42 requested a review from a team June 4, 2026 18:43
@JonasJesus42
JonasJesus42 merged commit 7c82bf6 into main Jun 4, 2026
1 check passed
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 6.4.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak: 4 module-level inflight Maps in start retain hung Promises (exceededMemory in prod)

1 participant