Skip to content

engine: stop reporting routine page isolate kills as errors - #254

Merged
ruifigueira merged 2 commits into
mainfrom
rfigueira/page-isolate-kill-hang
Nov 27, 2026
Merged

ruifigueira merged 2 commits into
mainfrom
rfigueira/page-isolate-kill-hang

Conversation

@ruifigueira

Copy link
Copy Markdown
Collaborator

Summary

When the engine kills a page isolate on purpose, it now reports nothing as an error. Two paths reported routine kills as failures:

  1. Hung loopback request. Since engine: host page isolates and tighten teardown #192, page and renderer isolates run inside a PageIsolateHost loopback request. PageIsolateHandle.killIsolate returned the page's own killIsolate promise. 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 an exception outcome on the engine's PageIsolateHost entrypoint.
  2. Abort errors in the tail. abortIsolate() also fails every other call still in flight in the killed isolate, with Error: abortIsolate() was called. Only the killIsolate call logs the authenticated [ks-kill] line, so PageScriptTail logged these calls at error level and passed them to reportException.

Behavior and API impact

  • CDP behavior: none. Pages are killed as before.
  • A hosted kill now settles when the page replies, or after at most 750 ms if the reply is lost. Before, a lost reply held the engine for its full 1 s teardown grace.
  • PageScriptTail no 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.killIsolate and RendererIsolateHandle.killIsolate race 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.killIsolate writes 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 killIsolate immediately

I tested the options in a minimal wrangler dev 4.168.0 worker. A host entrypoint loads a child with env.LOADER, and the child's killIsolate calls abortIsolate():

Host killIsolate Child's killIsolate runs Error logged in the host
Return the child's promise (before) yes Uncaught Error: internal error
Return at once, do not wait no: disposing the handle ends the request and cancels the kill none
Race the kill against a timer (this PR) yes, also when the caller disposes the handle at once none

Returning at once would leave killed pages running, so this PR waits on a timer.

Validation

Before the fix:

  • The hung cancellation and the abort errors appear in deployment logs. One page's kill call logged [ks-kill] Target.closeTarget (engine closed the isolate on purpose …). A separate invocation of the same page logged Error: abortIsolate() was called. twice. That invocation had no [ks-kill] line.
  • Local workerd rejects the dropped reply with internal error instead 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 (in packages/engine): 54 passed. Without the src/ 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_test fails the same way on unchanged origin/main on my machine, because my ~/.cargo/config.toml sets a rustc-wrapper that is missing in the Bazel sandbox. bazel run //scripts:workspace_checks passed.

Not checked:

  • That a deployment shows no more hung PageIsolateHost invocations or abort errors after this change. I will check after it ships.
  • Whether workerd should settle the call with an error instead of dropping it. That would make the timer unnecessary.

Risks and follow-ups

  • A page busy in a long task receives the kill only when the task ends. If that takes more than 750 ms, the handle returns, the engine disposes it, and the kill can be cancelled before it lands. Before this PR the same happened at the engine's 1 s grace, so the window is 250 ms shorter. 750 ms keeps most of it, with a margin for the engine's timer, which starts first.
  • The registry relies on PageScriptTail running in the same isolate as the engine, as notifyIsolateLimit and getTailTrace already do. If the tail runs in another isolate, it reports abort errors as before.
  • Renderer kills keep the old tail behavior. Renderer script names are reused within a connection, so a record would hide later renderer isolates' abort errors.

Checklist

  • This pull request solves one problem; see CONTRIBUTING.md.
  • Every commit has a Signed-off-by line (git commit -s); see CONTRIBUTING.md.
  • Added or updated focused tests for behavior changes.
  • Updated documentation and generated files, or confirmed they do not apply.

beep-boop-🤖

…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>
@github-actions github-actions Bot added the vouch:trusted Author is vouched, a collaborator, or a bot. label 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
ruifigueira merged commit d554baf into main Nov 27, 2026
13 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

vouch:trusted Author is vouched, a collaborator, or a bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants