Skip to content

tanstack: app middlewares run before bindCacheStorage — cacheBackground is a no-op there, and joinInflight resumes joiners in the owner's context #726

Open

Description

@aline-pereira

Context

On Cloudflare Workers with no_handle_cross_request_promise_resolution, a storefront (oficina-tanstack, deco-sites/oficina-tanstack#507) had requests hang forever after a client cancelled an earlier request. Every route was affected: /deco/invoke, PLP, PDP, and the /api/checkout proxy. The trigger was site middleware awaiting isolate-wide shared work. Two framework behaviours made it possible, and they will affect any site that shares work from middleware.

1. cacheBackground is a no-op inside app middleware

createDecoWorkerEntry (packages/tanstack/src/sdk/workerEntry.ts) runs getAppMiddleware() before handleRequest. Only handleRequest calls bindCacheStorage({ waitUntil }).

cacheBackground (packages/blocks/src/sdk/cacheStorage.ts) reads waitUntil from that binding. So work a middleware starts and shares with later requests is never kept alive. When the starting client disconnects, the runtime cancels its I/O, the shared Promise never settles, and every joiner hangs.

Proposal: bind the cache-storage context (or at least waitUntil) before running app middlewares. Alternatively, expose the ExecutionContext to middlewares, e.g. RequestContext.waitUntil.

2. joinInflight resumes the joiner in the owner's I/O context

joinInflight (packages/blocks/src/sdk/inflightTimeout.ts) resolves the joiner through shared.then(resolve). Under no_handle_cross_request_promise_resolution, the joiner's continuation then runs inside the owner's request. That includes everything the joiner does next: fetches, and timers created later. If the owner request ends or is cancelled, the joiner's later work dies with it.

The site works around this with a "settle in own context" helper. A reaction only records the outcome, and a timer chain created in the caller's context polls for it. A framework-level equivalent in joinInflight would remove that class of hang.

Related

Measured impact on the site (in-place versions, same time window): hang rate went from 67–78% of rounds to 0/12, with the fix applied on the site side.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions