fix: #788 — AsyncLocalStorage real context propagation#852
Merged
Conversation
Propagates AsyncLocalStorage stores across await, .then/.catch, queueMicrotask, process.nextTick, setTimeout, setImmediate and setInterval. Store stacks moved out of per-handle Vecs into a thread-local active context that schedulers snapshot at registration time and restore while the callback runs. exit() now restores only the affected ALS so mutations to other instances inside the callback survive. Microtask context restoration uses a stack and the queueMicrotask drain joins the same setjmp trap, so a throw through a callback does not leak context. The promise side table is cleared on GC sweep; a TODO notes the follow-up to inline the snapshot into Promise once PERRY_GEN_GC_EVACUATE=1 becomes default. The node:async_hooks shim note no longer flags ALS as lossy; the remaining gap (createHook lifecycle + real asyncId) is tracked in #789.
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.
Summary
AsyncLocalStoragepropagation acrossawait,.then/.catch,queueMicrotask,process.nextTick,setTimeout,setImmediateandsetInterval. Closes AsyncLocalStorage: real async-context tracking across await / microtasks / timers #788.Vecinto a thread-local active context (perry-runtime/src/async_context.rs); schedulers snapshot it at registration time and restore it while the callback runs.exit()now restores only the affected ALS, preserving mutations made to other instances inside the callback.Option) and thequeueMicrotaskdrain participates in the same setjmp trap, so a throw through a callback does not leak the callee's context.PROMISE_CONTEXTSis cleared on GC sweep forGC_TYPE_PROMISEpayloads. ATODOnotes the follow-up to inline the snapshot intoPromiseoncePERRY_GEN_GC_EVACUATE=1becomes default (today the evacuator does not rewrite the side-table key).node:async_hooksrewritten: ALS is no longer flagged as losing context; the remaining gap (createHook lifecycle + real asyncId) is tracked in async_hooks: real createHook lifecycle + asyncId tracking #789.Test plan
cargo fmt --checkcargo check -p perry-hir -p perry-codegen -p perry-runtime -p perry-stdlib./run_parity_tests.sh --filter test_async_local_storage_context— PASS (new test covering await / then / catch / nextTick / timer / immediate / interval / nested run / enterWith / exit isolation / two instances)./run_parity_tests.sh --filter test_harness_async_context— only section 8 (createHook typeof: undefined) fails, as expected for async_hooks: real createHook lifecycle + asyncId tracking #789; every ALS section now passes.