Skip to content

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

Closed

Description

@JonasJesus42

Summary

Four module-level Map<string, Promise<...>> caches in @decocms/start evict entries only via .finally() on the stored Promise. If the underlying work never settles (downstream fetch hangs, awaited Promise never resolves), the entry leaks forever — and every subsequent request for the same key joins the zombie Promise, accumulating context references in memory until exceededMemory.

Two of the four caches use request-agnostic keys (just the block reference name like "Header - 01"), so a single hung Promise blocks every request on the isolate, not just same-URL requests.

Affected caches

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

Production impact

Observed on a TanStack Start storefront (baggagio-tanstack, version 14c6fad4). 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
/bolsas 2 2 27ms 4552s

514 hard exceededMemory crashes in 24h, plus 403 canceled and 371 HTTP 503s.

CPU near zero, wall time in the tens of minutes per isolate — workers sleeping awaiting hung Promises, not computing. Crash loops localized to specific Cloudflare colos, consistent with a single poisoned isolate per colo. One route accounting for 82% of events points to a single poisoned cache key.

The companion VTEX fetchCache issue is at decocms/apps-start. The same fix pattern applies to all four caches here.

Proposed fix

Wrap each stored Promise with a timeout so .finally() always runs:

const INFLIGHT_TIMEOUT_MS = 10_000;

function withTimeout<T>(p: Promise<T>, ms: number, label: string): Promise<T> {
  let timer: ReturnType<typeof setTimeout> | undefined;
  const timeout = new Promise<never>((_, reject) => {
    timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);
  });
  return Promise.race([p, timeout]).finally(() => clearTimeout(timer));
}

// at the inflight.set / resolvedLayoutInflight.set call site:
const promise = withTimeout(doWork(...), INFLIGHT_TIMEOUT_MS, `cacheName ${key}`)
  .then(...)
  .finally(() => inflight.delete(key));

inflight.set(key, promise);

For request-agnostic caches (layoutInflight, resolvedLayoutInflight) the timeout is especially important — a hung Header/Footer resolution currently blocks every request on the isolate.

If doWork involves a fetch, plumb an AbortController from the timeout into it so the underlying subrequest is cancelled, not just abandoned.

Workaround in consumer code

Removing no_handle_cross_request_promise_resolution from wrangler.jsonc makes the CF runtime abort hung Promises at request end, preventing accumulation across requests. We applied this in baggagio-tanstack and the crash loop stopped. Trade-off: the cross-request dedup benefit of these inflight Maps is reduced. Acceptable temporarily, but the fix belongs in the framework so consumers can keep that flag on.

Validation plan

  1. In a synthetic test, make a section loader / layout resolver hang.
  2. Issue N parallel requests against the worker.
  3. Confirm: inflightRequests.size, sectionLoaderInflight.size, layoutInflight.size, resolvedLayoutInflight.size all stay bounded (entries evicted after timeout).
  4. Confirm: subsequent requests retry instead of joining the dead Promise.

Activity

  1. added a commit that references this issue on Jun 4, 2026
    7c82bf6
  2. github-actions commented on Jun 4, 2026

    @github-actions

    🎉 This issue has been resolved 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions