Skip to content

fix: crash when DevTools LoadCompleted IPC arrives after WebContents teardown - #52166

Merged
ckerr merged 1 commit into
mainfrom
fix/devtools-loadcompleted-lifetime-race
Jul 29, 2026
Merged

ckerr merged 1 commit into
mainfrom
fix/devtools-loadcompleted-lifetime-race

Conversation

@claude

@claude claude Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Requested by David Sanders · Slack thread

Description of Change

Before: Opening DevTools and then quickly closing them while the owning WebContents is being torn down can crash the browser process with a SIGSEGV. DevTools embedder-message IPCs (e.g. "loadCompleted") are delivered as queued mojo tasks. If the owning electron::api::WebContents has already been destroyed by the time such a queued message dispatches, the dispatch reaches a base::Unretained(this) delegate and dereferences freed memory — the crash surfaces as a use-after-free probing owner_window_ (a base::WeakPtr<NativeWindow>).

After: Both delegate interfaces the embedder-message dispatch can call back into are cleared up front when the WebContents is destroyed, so any queued DevTools message that arrives after teardown becomes a guarded no-op instead of touching freed memory.

How

electron::api::WebContents registers itself as two delegates on its InspectableWebContents:

  • the InspectableWebContentsViewDelegate (DevToolsOpened() / DevToolsClosed() / DevToolsFocused()), set via GetView()->SetDelegate(this), and
  • the InspectableWebContentsDelegate (the DevTools*File / *FileSystem / reload / eyedropper handlers), set via inspectable_web_contents_->SetDelegate(this).

The merged fix for #19985 (PR #47435) cleared the view delegate in ~WebContents() so a late DevToolsOpened() becomes a no-op. The sibling InspectableWebContents delegate was left dangling, however: its handlers in shell/browser/ui/inspectable_web_contents.cc (lines ~768–883) all call delegate_->DevTools*() via base::Unretained and were never reset on teardown, so a queued embedder message routed there after the WebContents is freed is still a use-after-free.

This change clears that second delegate alongside the view delegate in WebContents::~WebContents() (shell/browser/api/electron_api_web_contents.cc, ~line 1179). Every delegate_-> call site is already guarded by if (delegate_), so nulling it makes a late callback a safe no-op. The dispatch entry point (HandleMessageFromDevToolsFrontend / SendMessageAck) is already bound through InspectableWebContents's own weak_factory_, so a message arriving after InspectableWebContents itself is gone is already dropped; this closes the remaining outer-delegate window.

Related

Same lineage as #19985 / PR #47435 ("fix: crash when closing devtools after focus"). That fix guarded the inner DevTools WebContents re-entrant destruction and cleared the view delegate; this closes the remaining outer-InspectableWebContents-delegate gap.

Found as an unrelated flake on PR #52165's CI (linux-x64-asan, run 28302777569) in the MenuItem toggleDevTools suite in spec/api-menu-item-spec.ts.

Checklist

Release Notes

Notes: Fixed a crash that could occur when closing DevTools while the host WebContents was being destroyed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Us1vbuJ3MzX5bm6YKP6AW8

A queued DevTools embedder-message IPC (e.g. "loadCompleted") can be
dispatched after the owning WebContents has begun teardown. The
WebContents registers itself as both the InspectableWebContentsView
delegate and the InspectableWebContents delegate, and the embedder
message handlers call back into it via base::Unretained(this). PR #47435
(issue #19985) cleared the view delegate on teardown, but the sibling
InspectableWebContents delegate (DevTools file/filesystem handlers) was
left dangling, so a late callback there is a use-after-free on the freed
WebContents.

Clear the InspectableWebContents delegate alongside the view delegate so
any late DevTools embedder callback becomes a guarded no-op instead of
touching freed memory.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Us1vbuJ3MzX5bm6YKP6AW8
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Jun 27, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Jul 4, 2026
@thomasbachem

Copy link
Copy Markdown
Contributor

We hit this crash signature in the field today – Electron 43.1.1, macOS 26.5 arm64, browser process. The app auto-opens docked DevTools for its WebContentsView tabs right after their first real navigation commits (tabs boot on a data: placeholder). About 1s after launch, with the page itself still loading, the DevTools frontend load completed and the browser process died:

EXC_BAD_ACCESS / KERN_INVALID_ADDRESS at 0x160 (browser process, uptime 1s)
electron::api::WebContents::SetOwnerWindow(content::WebContents*, electron::NativeWindow*)
electron::api::WebContents::DevToolsOpened()
electron::InspectableWebContents::LoadCompleted()
… DevToolsEmbedderMessageDispatcher ("loadCompleted" frontend message)

The mechanism looks slightly different from the teardown race fixed here though. Disassembling our binary at the faulting PC – a build that already has the GetView()->SetDelegate(nullptr) clear in ~WebContents() and the GetDevToolsWebContents() null check – shows the crash is the if (owner_window_) load at the top of SetOwnerWindow() executing with this == nullptr (x0 = 0, fault address 0x160 = the owner_window_ offset). The only caller in that stack is handle->SetOwnerWindow(dtwc, owner_window()) in DevToolsOpened(), so FromOrCreate(isolate, dtwc) appears to have returned an empty handle that sailed through devtools_web_contents_.Reset() unnoticed – everything else on that path was live: dtwc passed its null check, the tab's own owner_window_ check passed right before the call, and no window or tab teardown was in flight per our app log.

Older dumps show the same stack three more times since early May, on Electron 41.x, 42.3.x and 42.5.x builds – always the browser process within seconds of launch, always the same null base fault on the owner_window_ check (the 41.x build even shows base::internal::WeakReference::IsValid() as top frame). It's also the only recurring browser process crash in our dumps at all – we've symbolicated everything back to April by now. No repro unfortunately – happy to share the four minidumps. Might be worth guarding the empty handle case in DevToolsOpened() too while at it.

@jkleinsc
jkleinsc marked this pull request as ready for review July 20, 2026 19:56
@jkleinsc jkleinsc added semver/patch backwards-compatible bug fixes target/41-x-y PR should also be added to the "41-x-y" branch. target/42-x-y PR should also be added to the "42-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. labels Jul 20, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Jul 20, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Jul 21, 2026
@ckerr
ckerr merged commit 20710d1 into main Jul 29, 2026
102 checks passed
@ckerr
ckerr deleted the fix/devtools-loadcompleted-lifetime-race branch July 29, 2026 02:43
@release-clerk

release-clerk Bot commented Jul 29, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed a crash that could occur when closing DevTools while the host WebContents was being destroyed.

@trop

trop Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "44-x-y", please check out #52509

@trop

trop Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "43-x-y", please check out #52510

@trop trop Bot removed the target/44-x-y PR should also be added to the "44-x-y" branch. label Jul 29, 2026
@trop

trop Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "42-x-y", please check out #52511

@trop

trop Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "41-x-y", please check out #52512

@trop trop Bot added in-flight/42-x-y in-flight/41-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. merged/41-x-y PR was merged to the "41-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. and removed target/43-x-y PR should also be added to the "43-x-y" branch. target/42-x-y PR should also be added to the "42-x-y" branch. target/41-x-y PR should also be added to the "41-x-y" branch. in-flight/42-x-y in-flight/43-x-y in-flight/41-x-y in-flight/44-x-y labels Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

45-x-y merged/41-x-y PR was merged to the "41-x-y" branch. merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

5 participants