Skip to content

Workers errors: document cross-request promise cancellation#29797

Closed
lukevalenta wants to merge 1 commit into
cloudflare:productionfrom
lukevalenta:lvalenta/cross-request-promise-cancellation
Closed

Workers errors: document cross-request promise cancellation#29797
lukevalenta wants to merge 1 commit into
cloudflare:productionfrom
lukevalenta:lvalenta/cross-request-promise-cancellation

Conversation

@lukevalenta

@lukevalenta lukevalenta commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds Cause 3 to the "script will never generate a response" errors section explaining cross-request promise cancellation — a common pitfall when using promise-based lazy initialization (e.g. OnceCell.getOrInit()) in global scope. Includes the exact warning message, a problematic code example, and the recommended check-then-set fix pattern.

Background

The cross-request promise cancellation behavior was discovered when diagnosing 500 errors in a Rust-based Worker that used tokio::sync::OnceCell::get_or_try_init for lazy initialization of a certificate pool. Concurrent cold-start requests would fail because request B awaited a Promise created by request A; when A's context ended, B's continuation was cancelled. The exact runtime warning is:

A promise was resolved or rejected from a different request context than the one it was created in. However, the creating request has already been completed or canceled. Continuations for that request are unlikely to run safely and have been canceled.

This scenario is not currently covered in the errors docs. The existing "Cause 1: Unresolved Promises" section covers a different case (a Promise that is never resolved), and "Cannot perform I/O on behalf of a different request" covers sharing I/O objects — but neither addresses cross-request Promise awaiting.

@lukevalenta
lukevalenta requested a review from a team April 13, 2026 13:43
@github-actions github-actions Bot added product:workers Related to Workers product size/s labels Apr 13, 2026
@lukevalenta
lukevalenta force-pushed the lvalenta/cross-request-promise-cancellation branch from 79b4cc4 to dee8e55 Compare April 13, 2026 13:46
Adds a new 'Cause 3' subsection under the 'script will never generate a
response' error explaining cross-request promise cancellation — a common
pitfall when using promise-based lazy initialization (e.g. OnceCell or
getOrInit patterns) in global scope.

- Describes the exact warning message logged to the console
- Explains the root cause: Promises are owned by the request context that
  created them; awaiting a Promise from a different request context causes
  cancellation when that context ends
- Shows a common problematic pattern: promise-based OnceCell/getOrInit
  in global scope where concurrent requests await each other's Promises
- Shows the correct fix: synchronous check-then-set pattern where each
  request initializes independently without cross-request awaiting
- Notes the no_handle_cross_request_promise_resolution compatibility flag
  as a suppression-only workaround (not a real fix)
@lukevalenta
lukevalenta force-pushed the lvalenta/cross-request-promise-cancellation branch from dee8e55 to ccb2f58 Compare April 13, 2026 14:00
@lukevalenta lukevalenta changed the title Workers errors: document cross-request promise cancellation; fix ctx.id.name DO note Workers errors: document cross-request promise cancellation Apr 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hey there, we've marked this pull request as stale because there's no recent activity on it. This label helps us identify PRs that might need updates (or to be closed out by our team if no longer relevant).

@github-actions github-actions Bot added the stale label Apr 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hey there, we've closed out this pull request because it's been stale for a while and there's been no additional action on it. If these changes are still relevant, open a new pull request (or flag to us in a GitHub issue).

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.

5 participants