fix(after): don't retain the caller's async context for tasks that never settle - #98568
Open
mircoservices wants to merge 1 commit into
Open
fix(after): don't retain the caller's async context for tasks that never settle#98568mircoservices wants to merge 1 commit into
mircoservices wants to merge 1 commit into
Conversation
…ver settle `AwaiterMulti.waitUntil` registers its bookkeeping `.then()` inside the caller's async context. A `.then()` holds the context that was current when it was registered for as long as the promise stays unsettled, so a task that never settles pins the context of the request that registered it. On a self-hosted server that is unbounded: `createInternalWaitUntil` builds one `AwaiterOnce` per server and only drains it on `onServerClose`, so nothing releases the entry while the process runs. For a render the retained context holds the RSC flight payload. Register the reaction through `createSnapshot()` instead, which restores an empty context. The promise is still tracked and still awaited, so `after()` semantics are unchanged; only the retained graph shrinks. Measured on 16.4.0-canary.26 with a route calling `after()` on a never-settling promise, 300 requests each holding 1 MiB in request-scoped context: 300.0 MiB retained before, 0.0 MiB after. Claude-Session: https://claude.ai/code/session_014QS6jWEQXPeLzx5L9jELTf
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.
fixes #98561
AwaiterMulti.waitUntilregisters a bookkeeping.then()in the caller’s async context. If the promise never settles, that context stays in memory. On self-hosted servers, the shared awaiter only drains when the server closes, so retained request data keeps growing.The fix uses
createSnapshot()to register the bookkeeping callback in an empty context. Tasks are still tracked and awaited.All nine unit tests pass with the fix. Reverting the fix causes the two never-settling tests to fail.