Repository navigation
engine: stop reporting routine page isolate kills as errors - #254
Merged
Merged
Conversation
…eplies PageIsolateHandle and RendererIsolateHandle returned the page's killIsolate promise as their own. abortIsolate() can drop that reply, so the PageIsolateHost loopback request was left waiting on a promise that never settles, and workerd cancelled it as hung. A routine page kill then showed up as an exception on the engine's PageIsolateHost entrypoint. Wait for the reply on a timer instead, for at most 750 ms. The pending timer keeps the request alive until the kill lands, and the wait is shorter than the engine's 1 s teardown grace, so the handle replies before the engine disposes it. Returning at once is not safe: when the engine disposes the handle, the loopback request ends and workerd cancels the kill before the page receives it. beep-boop-🤖 Signed-off-by: Rui Figueira <rfigueira@cloudflare.com>
abortIsolate() fails every call still in flight in the killed isolate with "abortIsolate() was called.", or ends it as hung. Only the killIsolate call itself logs the authenticated [ks-kill] line, so PageScriptTail treated the other calls' errors as page failures: it logged each one at error level and sent it to reportException. The engine now records each kill it requests for a script, before it calls the isolate. The tail treats an abort or hung error from a recorded script as the kill. The record is written by the engine, not by page code, so a page cannot hide its own errors this way. Script names are unique per navigation generation, and a record lasts one minute. beep-boop-🤖 Signed-off-by: Rui Figueira <rfigueira@cloudflare.com>
ruifigueira
requested review from
BernardoPe,
LuisDuarte1,
celso and
jonnyparris
as code owners
November 27, 2026 11:58
BernardoPe
approved these changes
Nov 27, 2026
ruifigueira
added a commit
that referenced
this pull request
Nov 27, 2026
The pinned WebMCP IDL (spec PRs #241, #246, #251, #253, #254, #348) changed what getTools() returns and what executeTool() and registerTool() accept. getTools() still returned RegisteredTool dictionaries, and executeTool() found a tool by name, parsed a JSON string and rejected every failure with UnknownError. getTools() now returns instances of the exposed Tool interface, with source instead of window, and null inputSchema, outputSchema and annotations when the tool has none. Each Tool keeps the ID of the registration it came from, and executeTool() runs only that registration, so a same-named tool registered later is a NotFoundError. A plain object is not a Tool and rejects with TypeError. A Tool is a Proxy over its instance, so structuredClone() throws DataCloneError and JSON.stringify() gives {}. executeTool() takes an optional object, not a JSON string. A primitive, or input that JSON cannot serialize, rejects with TypeError. A failed tool or an unserializable result rejects with OperationError, as the spec's tool execution errors say. registerTool() converts its dictionary as WebIDL does, validates and stores outputSchema like inputSchema, and keeps the debugging annotation. beep-boop-🤖 Signed-off-by: Rui Figueira <rfigueira@cloudflare.com>
ruifigueira
added a commit
that referenced
this pull request
Nov 27, 2026
The pinned WebMCP IDL (spec PRs #241, #246, #251, #253, #254, #348) changed what getTools() returns and what executeTool() and registerTool() accept. getTools() still returned RegisteredTool dictionaries, and executeTool() found a tool by name, parsed a JSON string and rejected every failure with UnknownError. getTools() now returns instances of the exposed Tool interface, with source instead of window, and null inputSchema, outputSchema and annotations when the tool has none. Each Tool keeps the ID of the registration it came from, and executeTool() runs only that registration, so a same-named tool registered later is a NotFoundError. A plain object is not a Tool and rejects with TypeError. A Tool is a Proxy over its instance, so structuredClone() throws DataCloneError and JSON.stringify() gives {}. executeTool() takes an optional object, not a JSON string. A primitive, or input that JSON cannot serialize, rejects with TypeError. A failed tool or an unserializable result rejects with OperationError, as the spec's tool execution errors say. registerTool() converts its dictionary as WebIDL does, validates and stores outputSchema like inputSchema, and keeps the debugging annotation. beep-boop-🤖 Signed-off-by: Rui Figueira <rfigueira@cloudflare.com>
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
When the engine kills a page isolate on purpose, it now reports nothing as an error. Two paths reported routine kills as failures:
PageIsolateHostloopback request.PageIsolateHandle.killIsolatereturned the page's ownkillIsolatepromise.abortIsolate()can drop that reply, so the loopback request waited on a promise that never settled, and workerd cancelled it with "The Workers runtime canceled this request because it detected that your Worker's code had hung". In a deployment, many routine page kills ended this way, as anexceptionoutcome on the engine'sPageIsolateHostentrypoint.abortIsolate()also fails every other call still in flight in the killed isolate, withError: abortIsolate() was called.Only thekillIsolatecall logs the authenticated[ks-kill]line, soPageScriptTaillogged these calls at error level and passed them toreportException.Behavior and API impact
PageScriptTailno longer logs or reports an abort or hung error from a script that the engine asked to kill. It still reports every other error from that script, and abort errors from scripts that the engine did not kill.Implementation
page-isolate-host.ts:PageIsolateHandle.killIsolateandRendererIsolateHandle.killIsolaterace the kill against a 750 ms timer (KILL_REPLY_WAIT_MS). The pending timer gives workerd an event to wait on, so the loopback request is no longer treated as hung. 750 ms is shorter than the engine's 1 s grace, so the handle replies before the engine disposes it.isolate-kill-registry.ts(new): the engine records the script name of each kill that it requests.PageScriptRpcSession.killIsolatewrites the record before it calls the isolate. Records last one minute, with at most 256 entries.page-script-tail.ts: an abort or hung error counts as the kill when the invocation has the[ks-kill]line, as before, or when the engine recorded a kill for the script. Page code cannot write the record, so a page cannot hide its own errors this way. Script names are unique per navigation generation.Why not return from
killIsolateimmediatelyI tested the options in a minimal
wrangler dev4.168.0 worker. A host entrypoint loads a child withenv.LOADER, and the child'skillIsolatecallsabortIsolate():killIsolatekillIsolaterunsUncaught Error: internal errorReturning at once would leave killed pages running, so this PR waits on a timer.
Validation
Before the fix:
[ks-kill] Target.closeTarget (engine closed the isolate on purpose …). A separate invocation of the same page loggedError: abortIsolate() was called.twice. That invocation had no[ks-kill]line.internal errorinstead of leaving it pending, so you can't reproduce the hung cancellation locally. The unit tests model it with a kill promise that never settles.After the fix:
npx vitest run test/page-isolate-host-kill.test.ts test/page-isolate-host-fallback.test.ts test/page-script-tail.test.ts test/page-script-rpc-session.test.ts(inpackages/engine): 54 passed. Without thesrc/changes, the 9 new tests fail and the 42 existing tests pass.pnpm run test:e2e isolate-kill: 1 passed.pnpm run check: 32 of 33 Bazel targets passed.//:cargo_lock_testfails the same way on unchangedorigin/mainon my machine, because my~/.cargo/config.tomlsets arustc-wrapperthat is missing in the Bazel sandbox.bazel run //scripts:workspace_checkspassed.Not checked:
PageIsolateHostinvocations or abort errors after this change. I will check after it ships.Risks and follow-ups
PageScriptTailrunning in the same isolate as the engine, asnotifyIsolateLimitandgetTailTracealready do. If the tail runs in another isolate, it reports abort errors as before.Checklist
Signed-off-byline (git commit -s); see CONTRIBUTING.md.beep-boop-🤖