feat(runtime): #789 — real async_hooks createHook lifecycle + asyncId tracking - #865
Merged
Merged
Conversation
… tracking - New crates/perry-runtime/src/async_hooks.rs: process-wide hook registry, monotonic asyncId allocator, per-thread execution/trigger id stack, eager destroy queue, GC-driven destroy for dropped/unsettled Promises. Zero-hook hot path is a single relaxed atomic load + branch. - Emission sites wired in promise.rs (init/promiseResolve/destroy), timer.rs (init/before/after/destroy for setTimeout/setImmediate), and async_hooks itself for AsyncResource construction / runInAsyncScope / emitDestroy. - AsyncResource#bind synthesizes a real native closure trampoline (captures = handle + callback, registered as rest-arity 0) that replays the call through runInAsyncScope so before/after fire with the bound resource's id. - V8 fallback module (perry-jsruntime/src/modules.rs) implements the same surface in JS so the embedded runtime stays in sync. - gc.rs gains a finalizer queue + async_hooks root scanner so leaked Promises still fire destroy on collection. - Drops the compile-time `[perry] note:` shim warning for node:async_hooks now that #788 and this issue both land. Verified byte-for-byte against `node --experimental-strip-types` for test-files/test_parity_async_hooks.ts.
proggeramlug
added a commit
that referenced
this pull request
May 25, 2026
…guard exports (#789, #788, #1423) The async-context core landed earlier (#852 ALS propagation, #865 async_hooks foundation). This finishes the remaining gaps and makes it usable in release: #789 — async_hooks lifecycle on promise/await: - emit_init boxed the `type` string with POINTER_TAG, so the init hook's `type` argument read back as `[object Object]` instead of `"PROMISE"`. Box it with STRING_TAG (new box_string helper). - the await continuation (Task::AsyncStep) didn't fire before/after or set the execution id, so executionAsyncId() stayed 0 across `await` and the after hook never fired. Bracket the step call with async_hooks::before/after using the result promise's async_id (captured as a plain value before the callback per #1663; no-op when async_hooks are inactive), mirroring the Task::Promise arm. #1764 follow-up — #1762 added class-field / direct-call guard helpers in typed_feedback/guards.rs with no #[used] anchors, so the auto-optimize thin-LTO + strip build dead-stripped them, failing the link for any class-field program (incl. the async harness). Add typed fn-pointer retention anchors for the four new guards. Verified: executionAsyncId now survives await, init reports "PROMISE", and before/after both fire (byte-for-byte vs node). All four async tests pass under both PERRY_NO_AUTO_OPTIMIZE and auto-optimize; removed their stale known_failures entries. Closes #789, #788, #1423.
proggeramlug
added a commit
that referenced
this pull request
May 25, 2026
…guard exports (#789, #788, #1423) (#1769) The async-context core landed earlier (#852 ALS propagation, #865 async_hooks foundation). This finishes the remaining gaps and makes it usable in release: #789 — async_hooks lifecycle on promise/await: - emit_init boxed the `type` string with POINTER_TAG, so the init hook's `type` argument read back as `[object Object]` instead of `"PROMISE"`. Box it with STRING_TAG (new box_string helper). - the await continuation (Task::AsyncStep) didn't fire before/after or set the execution id, so executionAsyncId() stayed 0 across `await` and the after hook never fired. Bracket the step call with async_hooks::before/after using the result promise's async_id (captured as a plain value before the callback per #1663; no-op when async_hooks are inactive), mirroring the Task::Promise arm. #1764 follow-up — #1762 added class-field / direct-call guard helpers in typed_feedback/guards.rs with no #[used] anchors, so the auto-optimize thin-LTO + strip build dead-stripped them, failing the link for any class-field program (incl. the async harness). Add typed fn-pointer retention anchors for the four new guards. Verified: executionAsyncId now survives await, init reports "PROMISE", and before/after both fire (byte-for-byte vs node). All four async tests pass under both PERRY_NO_AUTO_OPTIMIZE and auto-optimize; removed their stale known_failures entries. Closes #789, #788, #1423.
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
Closes #789. Implements the observability half of
node:async_hooks—createHookcallbacks, realexecutionAsyncId()/triggerAsyncId(), and the fullAsyncResourcesurface — on top of the AsyncLocalStorage context propagation that landed in #788.init/before/after/destroy/promiseResolveemitted at Promise construction & settlement, timer scheduling & firing,AsyncResourceconstruction,runInAsyncScope, andemitDestroy.destroyfor settled Promises, fired timers, and explicitemitDestroy(); GC-drivendestroyfor dropped/unsettled Promises via a new finalizer queue inperry-runtime/src/gc.rs.AsyncResource#bind(fn)synthesizes a native closure trampoline that replays throughrunInAsyncScope, so bound callbacks firebefore/afterwith the bound resource's async id active.perry-jsruntime/src/modules.rs) mirrors the same surface for code running through the embedded runtime.[perry] note:shim warning fornode:async_hooks— it's no longer accurate once both AsyncLocalStorage: real async-context tracking across await / microtasks / timers #788 and this issue land.Test plan
cargo build --release -p perry-runtime -p perry-stdlib -p perrytest-files/test_parity_async_hooks.tsbyte-for-byte parity vsnode --experimental-strip-typesAsyncResource#bindexercise — bound callback observes the bound resource'sexecutionAsyncId()and firesbefore/after