Repository navigation
fix: a cancelled request no longer hangs later visitors (page + loader inflight dedup) - #677
Merged
Merged
Conversation
On Cloudflare Workers a Promise's subrequests and timers belong to the request that created it. When that client disconnects (reload, navigation, closed tab) the runtime cancels them: the Promise left in `inflightRequests` (createCachedLoader) or `moduleInflight` (createCachedLoaderFromModule / createLoaderEntry) never settles, its `.finally()` never frees the slot, and `withInflightTimeout` is dead too (its timer was the cancelled request's). Every later caller with that key hangs for the life of the isolate. - The owner registers its flight with `cacheBackground` (ctx.waitUntil), so its invocation stays alive until the work settles. - Joiners go through `joinInflight`: a timer of the joiner's own request. If the shared Promise has not settled in 10s, the joiner evicts the slot (only while it still holds that Promise) and runs again. - `.finally()` only frees a slot it still owns, so a late flight never evicts a newer one. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ows synchronously onStall ran bare inside the setTimeout callback. createCachedLoaderFromModule's dedupedFn is not async, so a synchronous throw (cacheKeyFn, JSON.stringify of circular props, mod.default before Promise.resolve wraps it) escaped the timer and left the joiner's Promise unsettled — the same hang this PR removes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🎉 This PR is included in version 7.78.2 🎉 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.
Problem: one closed tab leaves a URL hanging (524) for the life of the isolate
Production, casaevideo (7.71.5), 13/10:
https://www.casaevideo.com.br/4221?map=productClusterIds&order=OrderByTopSaleDESC&sort=orders%3Adescreturns 524 every time on mobile. Desktop serves it only because the edge cache still has a copy. Any variant of the URL (&utm_source=x,&page=2, a different param order) renders in ~1 s with all 51 products. The upstream VTEX calls are healthy.Evidence:
wrangler tailof the hung request:outcome: canceled,cpuTime: 2,wallTime: 100003, no logs, no exceptions. The worker never starts rendering; it awaits a promise that never settles.Authorizationheader (privateRequest, which skips every cache) the request still hangs, 4/4;pageInflightincmsRoute.ts, keyed by URL only..finally()never frees the slot. That is the mechanism fix(blocks): inflight dedup survives a cancelled owner request #672 describes.slow cancel: client disconnectedappears 1,126 times in 6 h inotel_logs(casaevideo-tanstack).raw_cloudflarethe 524s are chronic, 10–90 per hour across 10–25 distinct URIs for the last 3 days. Examples: PLPs, PDPs, and/fonts/lato-bold.woff2, a lowercase miss that falls through to the CMS 404 page.Fix: the two existing fixes, together on 7.x so they ship in one release
Cherry-picked, original authors kept:
fix(tanstack): isolate pending page resolution between requests #541 (@viktormarinho):
pageInflight→ per-request single flight (WeakMap<Request, …>). A stalled or cancelled visitor can no longer block later visitors of the same URL. Within one request, calls are still deduplicated.fix(blocks): inflight dedup survives a cancelled owner request #672 (@aline-pereira): the same failure in the loader layer (
createCachedLoader,createCachedLoaderFromModule).cacheBackground(waitUntil).joinInflight). It evicts a dead slot and runs again.Follow-up from review (
046c8496):joinInflightnow runsonStallthroughPromise.resolve().then(onStall). Before, it was called bare inside the timer. A synchronous throw escaped the timer and left the joiner hanging; the throw can come fromcacheKeyFn, fromJSON.stringifyof circular props, or frommod.defaultbeforePromise.resolvewraps it. A new test covers this; it fails without the fix.Known trade-off (kept on purpose):
moduleInflighthas no owner-side timeout. A joiner cannot tell a slow owner from a dead one, so after 10 s it evicts the slot and calls upstream again, even when the owner's loader is healthy and just slow.releaseSlotonly evicts while the slot still holds the old Promise, so concurrent joiners join the new flight.withInflightTimeout, ascreateCachedLoaderalready does.Each fix alone leaves the other layer exposed. The page fix is the one that resolves the URL above; the loader fix covers shelves and PLPs that hang behind a dead loader flight.
Relation to #664:
pageInflightkey, plus a 10 s release timer) becomes moot with per-request scoping. Its release timer is armed in the owner request, so it dies with that request; fix(blocks): inflight dedup survives a cancelled owner request #672 describes the same trap.moduleInflightpart overlaps with fix(blocks): inflight dedup survives a cancelled owner request #672.Validation
packages/blocks: 987/987 pass, including the 10 new tests incachedLoaderCancelledOwner.test.ts.packages/tanstack: 307/309. The 2 failures areworkerEntry.test.ts › draft preview, and they fail identically onorigin/7.xwithout these commits.wrangler devdoes not cancel an aborted request's I/O, so the production hang itself cannot be reproduced locally. The unit tests simulate it.Closes #541 and #672 once merged. #664 needs a rebase decision.
🤖 Generated with Claude Code
Summary by cubic
Stops a cancelled request from hanging later visitors of the same URL. On Workers, a client disconnect cancels the owner's pending I/O and timers, so the promise left in module-global inflight dedup maps never settles and its cleanup never runs — every later caller waits forever and gets a 524.
WeakMapkeyed byRequest) instead of by URL, so a stalled visitor can't block later ones.cacheBackground(ctx.waitUntil); joiners usejoinInflight, a timer owned by their own request, to evict dead slots and re-run.joinInflightrunsonStallthroughPromise.resolve()so a synchronous throw rejects the joiner instead of hanging it.Written for commit 046c849. Summary will update on new commits.