Skip to content

webview: let a dead transport release its browser, and route closeAll() through it - #40176

Closed
robobun wants to merge 1 commit into
mainfrom
farm/b401f24a/webview-closeall-retire
Closed

robobun wants to merge 1 commit into
mainfrom
farm/b401f24a/webview-closeall-retire

Conversation

@robobun

@robobun robobun commented Aug 23, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #40165.

Problem

  • WebView.closeAll() only sent SIGKILL. A new Bun.WebView() in the same tick bound to the dying transport, and a later one threw "Failed to spawn Chrome" until the old process was reaped (Bun__Chrome__ensure refuses to spawn while a process is published). The docs promise "respawn as needed". Three tests carried reap-retry loops for this.
  • Every other path that marks the transport dead while the process lives (a closed reply pipe on Windows, a corrupt frame from the WebKit host, a pipe that cannot be adopted) left the same stuck state.
  • webview: retire the transport with its global under bun test --isolate #40165 covered bun test --isolate for the pipe transport only. The WebSocket mode had no test.

Fix

  • rejectAllAndMarkDead calls Bun__Chrome__retire() / Bun__WebViewHost__retire(): a dead transport never leaves a published browser behind. The adopt-failure branches retire too, and the spawn entry points assert in debug builds that nothing is published. retireGlobal shrinks to rejectAllAndMarkDead plus clearing the cached global.
  • WebView.closeAll() goes through rejectAllAndMarkDead("WebView closed by WebView.closeAll()") on both backends and throws ERR_INVALID_STATE off the main thread. Over WebSocket it used to be a no-op (no process to kill); it now closes the connection and the views. Process exit stays kill-only.
  • The constructor throws ERR_INVALID_STATE once the global's active DOM objects are stopped, so a handler that runs while --isolate retires a file cannot bind a new browser to the dying global.
  • Verified: webview-chrome-pipe.test.ts (new closeAll() case and the late-creation check fail on main), webview-chrome-disconnect.test.ts (new WebSocket closeAll() and --isolate cases on the mock CDP server, now in mock-cdp.ts), the real-Chrome suite with its new Worker case.

Background

  • The CDP Transport and the WebKit HostClient are process singletons, each bound to one published browser process (INSTANCE in ChromeProcess.rs / HostProcess.rs) and to the global that spawned it.
  • rejectAllAndMarkDead is where a transport dies: it closes the connection, rejects every pending promise and marks the views closed. Every loss path ends there.
  • Bun__Chrome__retire (webview: retire the transport with its global under bun test --isolate #40165) unpublishes and kills the process and marks it retired, so on_exit only reaps it.
Notes

Fail-before with main's src/: closeAll() rejects what is pending and the next WebView spawns at once fails (the next view binds to the dying transport or throws), the --isolate case fails on late: "created" instead of ERR_INVALID_STATE, and the real-Chrome closeAll() disconnect case rejects with "Chrome killed by signal 9" instead of the new message. The WebSocket --isolate case passes on main as well: it documents what #40165 fixed in that mode, which no test exercised.

closeAll() on a transport that never spawned anything marks it dead with no published process to retire. The next constructor resets it and spawns. The fake browser in webview-chrome-pipe.test.ts takes --exit-delay=20000 in the closeAll() case, so the same-tick spawn only works if closeAll() unpublished the old process itself.

Once INSTANCE lets go of a retired process, only its exit handler references it. When bun exits before the exit is reaped, LeakSanitizer on the ASAN lane reported the Process and its ChromeProcess box as leaks (the same memory was live before, but reachable through INSTANCE). Retired processes now hang off a static RETIRED list until on_exit frees them.

Real Chrome here runs through a --no-sandbox wrapper in BUN_CHROME_PATH (this container is root): all 5 files in test/js/bun/webview/, 77 pass, 59 skip (WebKit), 6 todo.


no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/webview/webview.test.ts, test/js/bun/webview/webview-chrome.test.ts, test/js/bun/webview/webview-chrome-pipe.test.ts, test/js/bun/webview/webview-chrome-disconnect.test.ts

…() through it

rejectAllAndMarkDead now unpublishes and kills the browser process it was
bound to, for both the CDP transport and the WebKit host client. A dead
transport never leaves a published process behind, so the next
new Bun.WebView() spawns at once instead of failing with "Failed to spawn
Chrome" until the old process has been reaped. The isolation hook shrinks
to rejectAllAndMarkDead plus clearing the cached global.

WebView.closeAll() goes through the same path: every view closes, its
pending promises reject with "WebView closed by WebView.closeAll()", and
the browser dies. Before, closeAll() only sent SIGKILL; a WebView created in
the same tick bound to the dying transport, and one created later threw
until the reap. The tests that carried reap-retry loops for this lose them.

The constructor refuses to create a view on a global whose active DOM
objects are stopped, so a rejection handler that runs while bun test
--isolate retires a file cannot bind a new browser to the dying global.

The WebSocket transport mode gets the --isolate test the pipe mode already
had, against a mock CDP endpoint that now lives in mock-cdp.ts.
@coderabbitai

coderabbitai Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Your included review limit has been reached.

You’re in a promotional period — use the checkbox below to run this review for free:

  • Run review for free

On-demand reviews are free for the next 28 days. After that, they cost $0.25 per reviewed file.

How can I continue?

Run this review now using the option above, or comment @coderabbitai review --use-credits.

You can also wait for the limit to reset (next review available in 14 minutes), then comment @coderabbitai review or push new commits to the PR.

An organization admin can change what happens after included review limits in Billing.

How do review limits work?

CodeRabbit enforces per-developer PR review limits within each organization.

For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 76039fe6-93eb-4edd-a5bc-9d20555ed409

📥 Commits

Reviewing files that changed from the base of the PR and between fb4227f and e76868a.

📒 Files selected for processing (14)
  • docs/runtime/webview.mdx
  • packages/bun-types/bun.d.ts
  • src/runtime/webview/ChromeBackend.cpp
  • src/runtime/webview/ChromeProcess.rs
  • src/runtime/webview/HostProcess.rs
  • src/runtime/webview/JSWebView.cpp
  • src/runtime/webview/JSWebView.h
  • src/runtime/webview/JSWebViewConstructor.cpp
  • src/runtime/webview/WebKitBackend.cpp
  • test/js/bun/webview/mock-cdp.ts
  • test/js/bun/webview/webview-chrome-disconnect.test.ts
  • test/js/bun/webview/webview-chrome-pipe.test.ts
  • test/js/bun/webview/webview-chrome.test.ts
  • test/js/bun/webview/webview.test.ts

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator Author

Status: implemented, waiting on CI.

Reproduction on main (fake browser from test/js/bun/webview/fake-chrome-fixture.ts, spawned with --exit-delay=20000 so it outlives its pipes like a real browser): closeAll() followed by new Bun.WebView() in the same tick binds the new view to the dying transport, and a later construction throws "Failed to spawn Chrome" until the old process is reaped. With this change the new view navigates at once and the pending promise rejects with "WebView closed by WebView.closeAll()". The WebSocket mode --isolate case runs against a mock CDP server in the test process and sees one connection close before the next file's opens.

@robobun

robobun commented Aug 23, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 12:57 AM PT - Aug 23rd, 2026

❌ @robobun, your commit e76868a has 2 failures in Build #104010 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 40176

That installs a local version of the PR into your bun-40176 executable, so you can run:

bun-40176 --bun

Comment thread src/runtime/webview/WebKitBackend.cpp Resolved
Comment thread docs/runtime/webview.mdx Resolved
@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as stale: this PR conflicts with main, and its last commit is from 2026-08-23.

The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants