Repository navigation
Conversation
…an awaited async handler When a worker's transform() is parked inside wait_for_promise for an async element/document handler and terminate() arrives, the wait breaks with the promise still Pending and a TerminationException set on the VM. handler_callback then returned false, so lol-html dispatched the next matching handler into JS with that exception pending, tripping Interpreter::executeCallImpl's assertNoException() and aborting the process on assert-enabled builds. Check the scope for a pending exception both on entry to handler_callback and immediately after wait_for_promise, and return the stop directive so lol-html unwinds instead of re-entering JS.
|
Reproduced on debug+ASAN with The test matrix covers element/text/comment handlers on CI: the diff is green for HTMLRewriter. Build 83335 failed on |
WalkthroughChangesHTMLRewriter now preserves pending termination exceptions across output and callback paths, stops processing after worker termination, and adds concurrent coverage for blocked asynchronous handlers across multiple entry points and input types. HTMLRewriter termination handling
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
|
Found 1 issue this PR may fix:
🤖 Generated with Claude Code |
|
This does not fix #34690. Same |
…or in test The entry-guard ran after Z::init + ref_() (refcount=2) but before to_js adopted the second +1 into a GC wrapper, so returning true there would strand one ref. Move the handler/global/scope setup and the check above the allocation so every return true is refcount-balanced. Also wire the worker 'error' event in the test fixture to reject the in-handler wait so a future regression before the handler is reached surfaces its message instead of an empty-stdout diff.
|
Updated 8:06 AM PT - Jul 27th, 2026
❌ @robobun, your commit 89ce581 has 1 failures in
🧪 To try this PR locally: bunx bun-pr 36100That installs a local version of the PR into your bun-36100 --bun |
…it and in create_lolhtml_error Extend the stop-on-termination check to the remaining paths where handler_callback observes a pending exception: the Err arm and the post-call check both call scope.clear_exception(), which (unlike tryClearException) clears a TerminationException. Return the stop directive with it still pending instead. create_lolhtml_error now recognizes a pending TerminationException and returns undefined rather than the Exception cell, so the termination propagates instead of being surfaced as the transform's rejection. Test covers every lol-html handler bridge (element/text/comments on .on() and comments/text on .onDocument(), plus both timer-await and never-settling promise shapes).
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/workerd/html-rewriter-worker-terminate.test.ts`:
- Around line 92-97: In the subprocess test’s result assertions, remove the bare
console.error(stderr) call and add an if (exitCode !== 0) block that asserts
stderr is empty immediately before expect(exitCode).toBe(0). Preserve the
existing concurrent Promise.all draining of stdout, stderr, and process exit,
along with the stdout assertion.
- Around line 4-16: Extend the test cases collection in the worker termination
test to include the missing onDocument doctype and end handlers. Keep the
existing comments and text cases, and configure the new cases through the same
handler_callback path so every onDocument bridge listed in the test header is
exercised.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 5ad2ed6b-4db7-4937-b0cd-4562c7b0798d
📒 Files selected for processing (2)
src/runtime/api/html_rewriter.rstest/js/workerd/html-rewriter-worker-terminate.test.ts
The sync arm of run_output_sink goes through create_lolhtml_error, which already bails on a pending TerminationException. The is_async twin calls to_error_instance directly; skip it when an exception is pending so the body-promise reject path cannot re-enter JS during worker teardown. Add a Bun.file-backed body cell so the is_async=true buffering path is exercised under the same terminate-during-wait scenario.
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
test/js/workerd/html-rewriter-worker-terminate.test.ts (1)
81-112: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winAssert stdout, stderr, and exit together
The stderr check is skipped on the success path, so a passing run can still emit errors. Fold all three results into one assertion so every case checks the full subprocess result.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/js/workerd/html-rewriter-worker-terminate.test.ts` around lines 81 - 112, Update the subprocess result assertions in the terminate() test to validate stdout, stderr, and exitCode together for every case. Remove the conditional stderr check and assert the complete expected result, preserving the existing expected output, empty stderr, and zero exit code.Source: Coding guidelines
src/runtime/api/html_rewriter.rs (1)
1435-1439: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy liftPreserve the termination path here.
JSValue::UNDEFINEDis not a no-error sentinel on the sync path: it gets stored insink_errorand later treated as a throwable value, so termination can surface asthrow undefined. Thread an explicit termination/no-error result through the caller and skipsink_errorstorage for that case.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/runtime/api/html_rewriter.rs` around lines 1435 - 1439, Update the termination handling around tryClearException() and its caller so termination is represented by an explicit no-error outcome rather than JSValue::UNDEFINED. Propagate that outcome through the synchronous transform path and avoid storing it in sink_error, while preserving normal error values and termination behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/workerd/html-rewriter-worker-terminate.test.ts`:
- Around line 66-79: Update the worker lifecycle flow in mainMjs so worker error
events reject a separate workerError promise, rather than only rejecting
inHandler. Race workerError with both inHandler and closed, ensuring any
subprocess failure after the handler resolves still rejects instead of allowing
“survived” to print.
---
Outside diff comments:
In `@src/runtime/api/html_rewriter.rs`:
- Around line 1435-1439: Update the termination handling around
tryClearException() and its caller so termination is represented by an explicit
no-error outcome rather than JSValue::UNDEFINED. Propagate that outcome through
the synchronous transform path and avoid storing it in sink_error, while
preserving normal error values and termination behavior.
In `@test/js/workerd/html-rewriter-worker-terminate.test.ts`:
- Around line 81-112: Update the subprocess result assertions in the terminate()
test to validate stdout, stderr, and exitCode together for every case. Remove
the conditional stderr check and assert the complete expected result, preserving
the existing expected output, empty stderr, and zero exit code.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 62db5781-3ddb-4be0-853c-129c776c2199
📒 Files selected for processing (2)
src/runtime/api/html_rewriter.rstest/js/workerd/html-rewriter-worker-terminate.test.ts
The previous create_lolhtml_error special-case returned UNDEFINED on a pending TerminationException, which on the sync path was stored via write_tmp_sync_error and later passed to throw_value. Guard both sync and async arms at the same place and drop the UNDEFINED branch so termination never reaches either error path. Test: race a dedicated workerError promise with both inHandler and closed so a worker error after the handler resolves is surfaced; assert stdout/stderr/exitCode together via toEqual.
…h; drop create_lolhtml_error UNDEFINED special-case Returning UNDEFINED from create_lolhtml_error on the sync path was stored via write_tmp_sync_error and later passed to throw_value. Guard both sync and async arms together and let create_lolhtml_error stay on its pre-PR shape (callers now never reach it with termination pending).
There was a problem hiding this comment.
No issues found — all prior review threads (refcount balance on the entry guard, the async run_output_sink arm, test dedup, onEndTag coverage) are resolved in the current diff. Deferring only because this is JSC exception-scope handling across a worker-termination boundary; worth a human glance at the global.has_exception() gates in run_output_sink and the sync-path return None under a pending TerminationException.
What was reviewed:
handler_callbackreorder:Z::init/ref_()/scopeguard now sit below the entryscope.has_exception()check, so everyreturn trueis refcount-balanced (2→1 via guard, 1→0 via GC wrapper on paths afterto_js; nothing allocated on the entry path).is_termination_exception()early-returns precedescope.clear_exception()so the termination stays pending forrun_output_sinkto observe.run_output_sinkguards are hoisted above theis_asyncbranch so both sync and async error arms skip JS re-entry;create_lolhtml_errorno longer needs a special case.- Test matrix covers all six repeated-dispatch bridges plus the async-body arm;
erroris wired to reject via a racedworkerErrorpromise; combined{stdout, stderr, exitCode}assertion.
Extended reasoning...
Overview
Two files: src/runtime/api/html_rewriter.rs (+47/−21) and a new test at test/js/workerd/html-rewriter-worker-terminate.test.ts (+115). The fix adds four TerminationException/pending-exception guards to the shared handler_callback<H, Z, L> generic (entry, cb.call Err arm, post-call check, post-wait_for_promise) and two global.has_exception() gates to run_output_sink (after write() and end() fail). The wrapper-allocation block (Z::init → ref_() → scopeguard) was moved from above top_scope! to below the new entry guard.
Security risks
None. This is a crash-on-worker-terminate fix; no user-controlled input reaches new parsing or allocation. The only behavioral change is that an interrupted async handler now stops the rewriter instead of re-entering Interpreter::executeCallImpl with a pending exception.
Level of scrutiny
High. This is unsafe Rust bridging lol-html callbacks into JSC, with intrusive refcounting, exception scopes, and re-entrancy across wait_for_promise. The changes are additive guards plus one code-motion, and I traced refcount balance on every terminal path — but JSC exception-scope semantics under validateExceptionChecks and the interaction between TerminationException and try_take_exception()/clear_exception() are subtle enough that a maintainer familiar with the JSC bindings should confirm the run_output_sink sync-path return None (which lets init() return Ok(response_js_value) with the termination still pending) is the intended shape.
Other factors
This PR went through five review rounds: my earlier passes flagged the entry-guard refcount leak (fixed by moving Z::init below the check), the unguarded is_async arm (fixed by hoisting the has_exception() gate above the branch), test duplication (folded into the matrix), and the missing onEndTag cell (added). CodeRabbit's stderr-assertion and workerError-race feedback was applied. The github-actions comment-cop flags on the two-line guard comment were reasonably declined. Evidence in the PR body shows 5/7 cells crash on main (debug+ASAN) and 7/7 pass with the fix; release-build behavior is unchanged. is_termination_exception() is an established pattern used identically in fetch.rs, TopExceptionScope.rs, and JSGlobalObject.rs.
There was a problem hiding this comment.
No further issues found — all four prior rounds of feedback (wrapper-alloc ordering, async run_output_sink arm, fixture dedup, onEndTag cell) are addressed and the 8-cell matrix passes on assert builds. Deferring to a human because the fix sits at the intersection of TerminationException propagation, scope.clear_exception(), and wait_for_promise re-entry, which is subtle enough to warrant a maintainer's sign-off.
What was reviewed:
handler_callback: entry guard now precedesZ::init/ref_()so the early-return path does not leak the wrapper's second +1;is_termination_exception()gates bothclear_exception()sites.run_output_sink:has_exception()hoisted above theis_asyncbranch on bothwrite()andend()Errarms; sync-pathNonereturn leaves the pending termination for JSC to unwind on host-fn return (entering JS is what asserts, not returning).is_termination_exception()/has_exception()usage matches the established pattern inTopExceptionScope.rsandreport_active_exception_as_unhandled.- Test:
describe.concurrent+test.eachover 8 cells, pipes drained concurrently, combined{stdout, stderr, exitCode}assertion,Bun.filecell exercisesis_async == true.
Extended reasoning...
Overview
Fixes a debug/ASAN-build process abort (ASSERTION FAILED: !exception() in Interpreter::executeCallImpl) when Worker.terminate() lands while an HTMLRewriter async content handler is parked in wait_for_promise. Touches src/runtime/api/html_rewriter.rs (guards in handler_callback and run_output_sink; wrapper allocation reordered after the entry guard) and adds test/js/workerd/html-rewriter-worker-terminate.test.ts (8-cell subprocess matrix).
Security risks
None. No user-controlled input parsing, no auth/crypto, no new API surface. The change adds early-return guards on an existing termination path.
Level of scrutiny
High. This is native JSC exception handling around worker termination — the exact category REVIEW.md calls out ("Never clearException()", "Exception checks after every call that can enter JS"). The fix is small and pattern-matched to existing is_termination_exception() usage, but the interaction between TerminationException (which tryClearException refuses to clear), scope.clear_exception() (which would clear it), lol-html's next-token dispatch, and the sync/async run_output_sink twins is subtle enough that a maintainer should confirm the guard placement is complete.
Other factors
This PR has been through four automated review rounds; every inline comment (mine, CodeRabbit's, comment-cop's) is resolved. The evidence block shows the test fails on main (exit 134, JSC assert in stderr) and passes on both debug+ASAN and release with the fix. The create_lolhtml_error special-case mentioned in the PR description was superseded by the hoisted run_output_sink guard in 4d53884 — I confirmed no other caller of create_lolhtml_error is reachable with a pending termination. The one open judgment call — whether global.has_exception() (vs. a scope-based check) is the right probe in run_output_sink — looks fine given it's only gating a bail-out, not paired with a fresh throwing call, but that's the kind of thing a maintainer familiar with the top_scope! validation rules should confirm.
|
Closing: this is fixed on current main by #36733, which removed the nested wait_for_promise from the HTMLRewriter handler callback (async handlers now suspend the rewriter instead), so there is no longer a pending TerminationException to trip over when a worker is terminated mid-handler. This PR's test file (test/js/workerd/html-rewriter-worker-terminate.test.ts, all 8 cases) passes unmodified against a debug build of main at 04148c8, twice in a row. |
Repro
Deterministic on debug+ASAN. Needs two or more matching tokens; a single-token document is clean. Release builds survive (no JSC asserts) and the worker exits with code 1.
Cause
Every lol-html handler bridge (
on_element/on_comment/on_text/on_doctype/on_end/on_end_tag) funnels intohandler_callback, which spins the event loop viawait_for_promisewhen a handler returns a promise. Ifterminate()arrives during that wait, the loop breaks onexecution_forbidden()with the promise stillPendingand aTerminationExceptionset on the VM.handler_callbackthen checked onlypromise.status() == Rejected, returnedfalse, and lol-html dispatched the next matching token's handler.cb.call()→JSC::call→Interpreter::executeCallImplthen asserts!exception()and aborts the process.Fix
handler_callbacknow checks for a pending exception:cb.call()), so a stale pending exception from any path never reachesexecuteCallImpl;wait_for_promise, so an interrupted wait returns the stop directive;Errarm and the post-call check, wherescope.clear_exception()would otherwise clear aTerminationExceptionraised at a safepoint inside the handler body.Returning
truemaps to lol-html's stop directive and unwinds the rewrite without re-entering JS. This matches the existing gateEventLoop::run_callbackalready carries for the same failure mode.create_lolhtml_errornow recognizes a pendingTerminationException(whichtryClearException()leaves set) and returnsundefinedinstead of theExceptioncell, so the termination propagates rather than being surfaced as the transform's rejection value.Verification
test/js/workerd/html-rewriter-worker-terminate.test.tsspawns a worker that parks inside an async handler, terminates it once it reportsin-handler, and checks the parent printssurvivedand exits 0. The matrix covers every handler bridge (elementwith both a never-settling promise and a timer await,text,commentson.on(), andcomments/texton.onDocument()). Without the fix all six abort on assert builds andstdoutis empty; with the fix the worker closes and all six pass.[review] gate passed · iteration 1 · 2 files touched
fails on main (without fix)
passes on PR (with fix)
diff hotspot
gate history · 6 passed · 0 rejected · iteration 1
evidence per changed file