Skip to content

fix(blocks,tanstack): joiners of a cancelled owner's single-flight no longer hang - #714

Merged
JonasJesus42 merged 5 commits into
7.xfrom
fix/inflight-owner-cancel
Nov 17, 2026
Merged

JonasJesus42 merged 5 commits into
7.xfrom
fix/inflight-owner-cancel

Conversation

@igoramf

@igoramf igoramf commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Production incident on a TanStack storefront (14/10 and 15/10, both starting 16:10 UTC): ~1.2k requests/h site-wide (/_serverFn, pages, PDP) hung with CPU 1–3 ms and wall 100 s until Cloudflare returned 524 (outcome: canceled). Spread across all colos. Every time, a no-op redeploy cleared it instantly, so the stuck state was held by the isolate.

Root cause

Module-level single-flight maps let request B join the flight request A started. On Workers, when A's client disconnects (reload, navigation, closed tab) the runtime cancels A's I/O and its timers, including the owner's withInflightTimeout. The shared Promise never settles, .finally() never frees the slot, and every later request for the same key hangs for the life of the isolate.

cachedLoader already documents and handles this (joinInflight + publishInflight/cacheBackground). These sites did not:

File Map
blocks/src/cms/sectionLoaders.ts sectionLoaderInflight, layoutInflight
blocks/src/cms/resolve.ts resolvedLayoutInflight (Header/Footer: every page joins it)
blocks/src/sdk/fetchCache.ts inflight (upstream fetch dedup)
tanstack/src/routes/withSiteGlobals.ts inflight in resolveSiteGlobals (no timeout at all)

handle_cross_request_promise_resolution doesn't help here: it allows resolving another request's Promise, it doesn't stop the owner's I/O from being cancelled.

Fix

Same pair as cachedLoader, at every site:

  • Joiners are bounded by their own request's timer (joinInflight; inlined in withSiteGlobals because sdk/inflightTimeout isn't exported). On stall, they evict the dead slot and run again.
  • The owner's work is kept alive with waitUntil (cacheBackground), so a client disconnect doesn't kill the Promise others are waiting on.
  • Slots are released by identity, so a late finally never evicts a newer flight.

Tests

Every test simulates the cancelled owner: its work never settles and its timer died with it. All of them fail (hang) on 7.x without the fix and pass with it.

  • cms/inflightCancelledOwner.test.ts covers:
    • a layout section: 5 joiners, each keeping its own index, and the loader runs exactly twice;
    • a cacheable section;
    • the resolved Header/Footer in resolveDecoPage (sharedResolvedLayout);
    • the owner's work being handed to ctx.waitUntil.
  • sdk/fetchCache.test.ts and routes/withSiteGlobals.test.ts each have one cancelled-owner case.
  • bun run test: blocks 1007/1007. tanstack 320/322; the 2 failures (workerEntry.test.ts › draft preview) fail identically on base 7.x.
  • bun run lint: 550 errors on base → 549 (pre-existing). typecheck: only pre-existing blocks-cli errors.

Review follow-ups:

  • @decocms/blocks/sdk/inflightTimeout is now exported, and withSiteGlobals uses the real joinInflight instead of a copy.
  • Its slot is released by identity from an outer .finally, so a slow owner can't evict a newer flight.
  • Deliberately NOT added: an owner timeout on resolveSiteGlobals. It never rejected before, and a slow-but-valid resolve would turn into empty globals.

Note: wrangler dev doesn't reproduce this. Local workerd doesn't cancel the request context on client disconnect, so the owner's timer still fires.

main (8.x) doesn't have these maps, so this is 7.x-only.


Summary by cubic

Fixes single-flight joiners hanging when the owner request is cancelled on Workers: a client disconnect cancels that request's I/O and timers, so the shared Promise never settles and every later caller waits until the next deploy. In production this caused ~1.2k requests/h to time out at 100s (Cloudflare 524s), cleared only by a redeploy.

Applies the same pattern cachedLoader already uses — joiners bounded by their own timer, the owner's work kept alive with waitUntil (cacheBackground), and slots released by identity so a late finally never evicts a newer flight — to these module-level single-flight maps:

  • blocks/src/cms/sectionLoaders.ts (sectionLoaderInflight, layoutInflight)
  • blocks/src/cms/resolve.ts (resolvedLayoutInflight — laid out by Header/Footer, joined by every page)
  • blocks/src/sdk/fetchCache.ts (inflight upstream fetch dedup)
  • tanstack/src/routes/withSiteGlobals.ts (resolveSiteGlobals — had no timeout at all; sdk/inflightTimeout is now exported so it uses the real helper)

Joiners that give up now log a structured [inflight] stall warning labeled by call site, making cancelled-owner volume and poisoned keys visible in Workers logs.

  • A stalled layout join re-reads the layout cache before re-resolving, since a newer flight may have filled it.
  • A fetchCache join counts as HIT only when the shared fetch answers it; a stalled retry records its own MISS, so one call isn't counted twice.

Tests simulate the cancelled owner (work never settles, owner timers swallowed) and hang on 7.x without the fix. This is 7.x-only — main doesn't have these maps.

Written for commit 13c4dd2. Summary will update on new commits.

View guided diff Turn on auto-fix

… longer hang

Module-level dedup maps let request B join the flight request A started. On
Workers, when A's client disconnects the runtime cancels A's I/O AND its
timers — including the owner's `withInflightTimeout` — so the shared Promise
never settles, its `.finally()` never frees the slot, and every later request
for the same key hangs for the life of the isolate (CPU ~0, wall 100s → 524).
Only a deploy (fresh isolates) cleared it in production.

`cachedLoader` already handled this (`joinInflight` + `publishInflight`); the
other sites did not. Same pair applied to:

- cms/sectionLoaders: `sectionLoaderInflight`, `layoutInflight`
- cms/resolve: `resolvedLayoutInflight` (Header/Footer — every page joins it)
- sdk/fetchCache: `inflight` (upstream fetch dedup)
- tanstack routes/withSiteGlobals: `resolveSiteGlobals` (had no timeout at all)

Joiners are bounded by their OWN request's timer and re-run on stall; the
owner's work is kept alive with `waitUntil` (`cacheBackground`); slots are
released by identity so a late `finally` never evicts a newer flight.

Tests simulate the cancelled owner (work never settles, owner timers
swallowed) and fail without the fix.
…resolve; siteGlobals uses joinInflight

Review follow-ups on the cancelled-owner fix:

- New `cms/inflightCancelledOwner.test.ts`: layout section, cacheable
  section and the resolved Header/Footer layout (resolveDecoPage) — joiners
  of a flight whose owner timer died must recover on their own timer and
  keep their own index; plus the owner's work reaching `ctx.waitUntil`.
  All four fail on 7.x without the fix.
- Export `@decocms/blocks/sdk/inflightTimeout` and use the real
  `joinInflight` in withSiteGlobals instead of an inlined copy.
- withSiteGlobals releases its slot by identity from an outer `.finally`
  (always async), so a slow owner no longer evicts a newer flight.
…ed flight

`joinInflight` takes an optional label and emits one structured warn when a
joiner's own timer fires and it re-runs instead of waiting:

  [inflight] stall {"label":"layoutSection <key>","waitedMs":10000}

Until now that event was silent, so there was no way to tell how often owners
are cancelled (client disconnects) or which key got poisoned. Every call site
passes a label (sectionLoader, layoutSection, resolvedLayout, fetchCache,
cachedLoader, dedupLoader, siteGlobals) so the volume can be grouped by kind
and key in Workers logs.
JonasJesus42 and others added 2 commits November 17, 2026 13:32
# Conflicts:
#	packages/blocks/src/sdk/cachedLoader.ts
…led fetch join once

- sharedResolvedLayout: a joiner that stalls reads the layout cache before
  resolving again, since a newer flight may have filled it meanwhile.
- fetchCache: a join counts as HIT only when the shared fetch answers it; after
  a stall the retry records its own decision, so one call isn't HIT and MISS.
- Merge 7.x (cachedLoader: keep the stall label and pass ctx to the retry).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 7.86.6 🎉

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants