Repository navigation
fix(async): restore per-activation trap_next in async-step resume thunks (#5485) #5514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
97 changes: 97 additions & 0 deletions
97
crates/perry/tests/issue_5485_async_step_trap_next_leak.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| //! Regression test for #5485: an async function must resolve with its own | ||
| //! `return` value, not the value of an intermediate `await`. | ||
| //! | ||
| //! Root cause: `async_step_fulfill_thunk` / `async_step_reject_thunk` (the | ||
| //! resume path for `await <pending promise>`) preserved the *ambient* | ||
| //! `INLINE_TRAP.trap_next` while pointing `current_step` at the resumed step. | ||
| //! When the resumed step belonged to a *nested* activation, its | ||
| //! `js_async_step_done` reuse-gate (`current_step == step_closure`) then fired | ||
| //! against an *outer* activation's result promise and settled it prematurely | ||
| //! with the nested call's (intermediate) value — exactly the hazard | ||
| //! `js_async_first_call` already guards against by clearing `trap_next`. | ||
| //! | ||
| //! Symptom (Hono / Skelpo CMS): `POST /admin/login` returned an empty 200 | ||
| //! instead of the handler's `302` redirect, because the handler's awaited | ||
| //! result resolved to `checkLoginRateLimit()`'s `{ allowed: true }` (the first | ||
| //! `await`) instead of the `c.redirect(...)` it actually returned. | ||
| //! | ||
| //! The fix captures each activation's own `trap_next` as a second closure | ||
| //! capture on the resume thunks and restores *that* (not the ambient one). | ||
| //! | ||
| //! This minimal repro exhibits the leak deterministically via a | ||
| //! catch-handler `await` of a nested async fn: on the buggy runtime | ||
| //! `tryCatchAwait()` resolves to `{"allowed":true}` (the inner value) instead | ||
| //! of `"wrap:{\"allowed\":true}"` (its real return). | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::process::Command; | ||
|
|
||
| fn perry_bin() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_BIN_EXE_perry")) | ||
| } | ||
|
|
||
| #[test] | ||
| fn async_fn_returns_its_own_value_not_intermediate_await() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let entry = dir.path().join("main.ts"); | ||
| let output = dir.path().join("main_bin"); | ||
|
|
||
| std::fs::write( | ||
| &entry, | ||
| r#" | ||
| const tick = (): Promise<void> => new Promise((r) => setTimeout(() => r(), 0)); | ||
| async function inner(): Promise<{ allowed: boolean }> { | ||
| await tick(); | ||
| return { allowed: true }; | ||
| } | ||
| // The #5485 leak case: a catch-handler `await` of a nested async fn. The | ||
| // enclosing async fn must resolve with its OWN return value ("wrap:{...}"), | ||
| // not the inner await's intermediate value ({allowed:true}). On the buggy | ||
| // runtime the nested call's `js_async_step_done` settled this fn's result | ||
| // promise prematurely with {allowed:true}. | ||
| async function tryCatchAwait(): Promise<string> { | ||
| try { | ||
| await Promise.reject(new Error("e")); | ||
| } catch { | ||
| const r = await inner(); | ||
| return "wrap:" + JSON.stringify(r); | ||
| } | ||
| return "x"; | ||
| } | ||
| async function main(): Promise<void> { | ||
| console.log("B=" + (await tryCatchAwait())); | ||
| } | ||
| void main(); | ||
| "#, | ||
| ) | ||
| .expect("write entry"); | ||
|
|
||
| let compile = Command::new(perry_bin()) | ||
| .current_dir(dir.path()) | ||
| .arg("compile") | ||
| .arg(&entry) | ||
| .arg("-o") | ||
| .arg(&output) | ||
| .output() | ||
| .expect("run perry compile"); | ||
| assert!( | ||
| compile.status.success(), | ||
| "perry compile failed\nstdout:\n{}\nstderr:\n{}", | ||
| String::from_utf8_lossy(&compile.stdout), | ||
| String::from_utf8_lossy(&compile.stderr) | ||
| ); | ||
|
|
||
| let run = Command::new(&output).output().expect("run compiled binary"); | ||
| assert!( | ||
| run.status.success(), | ||
| "compiled binary failed\nstatus: {:?}\nstdout:\n{}\nstderr:\n{}", | ||
| run.status, | ||
| String::from_utf8_lossy(&run.stdout), | ||
| String::from_utf8_lossy(&run.stderr) | ||
| ); | ||
| let stdout = String::from_utf8_lossy(&run.stdout); | ||
| assert_eq!( | ||
| stdout, "B=wrap:{\"allowed\":true}\n", | ||
| "async fn must resolve with its own return value, not an intermediate await (#5485)" | ||
| ); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gate the captured
trap_nextwithcan_reuse.Line 262, Line 268, and Line 276 pass the raw ambient
trap_nextinto the resume thunks. Whencurrent_stepdoes not matchstep_closure, that pointer belongs to another activation; capturing it can recreate the cross-activation settlement leak on the pending/thenable fallback paths. Capturenullunless the reuse gate passed.Proposed fix
let trap = INLINE_TRAP.with(|c| c.get()); let can_reuse = !trap.trap_next.is_null() && trap.current_step == step_closure as usize; let trap_next = trap.trap_next; + let activation_trap_next = if can_reuse { + trap_next + } else { + std::ptr::null_mut() + }; @@ - let (fulfill, reject) = build_async_step_thunks(step_closure, trap_next); + let (fulfill, reject) = + build_async_step_thunks(step_closure, activation_trap_next); @@ - let (fulfill, reject) = build_async_step_thunks(step_closure, trap_next); + let (fulfill, reject) = + build_async_step_thunks(step_closure, activation_trap_next); @@ - let (fulfill, reject) = build_async_step_thunks(step_closure, trap_next); + let (fulfill, reject) = + build_async_step_thunks(step_closure, activation_trap_next);📝 Committable suggestion
🤖 Prompt for AI Agents