Repository navigation
fix: crash when DevTools LoadCompleted IPC arrives after WebContents teardown - #52166
Conversation
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
|
We hit this crash signature in the field today – Electron 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 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 |
|
Release Notes Persisted
|
|
I have automatically backported this PR to "44-x-y", please check out #52509 |
|
I have automatically backported this PR to "43-x-y", please check out #52510 |
|
I have automatically backported this PR to "42-x-y", please check out #52511 |
|
I have automatically backported this PR to "41-x-y", please check out #52512 |
Requested by David Sanders · Slack thread
Description of Change
Before: Opening DevTools and then quickly closing them while the owning
WebContentsis 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 owningelectron::api::WebContentshas already been destroyed by the time such a queued message dispatches, the dispatch reaches abase::Unretained(this)delegate and dereferences freed memory — the crash surfaces as a use-after-free probingowner_window_(abase::WeakPtr<NativeWindow>).After: Both delegate interfaces the embedder-message dispatch can call back into are cleared up front when the
WebContentsis destroyed, so any queued DevTools message that arrives after teardown becomes a guarded no-op instead of touching freed memory.How
electron::api::WebContentsregisters itself as two delegates on itsInspectableWebContents:InspectableWebContentsViewDelegate(DevToolsOpened()/DevToolsClosed()/DevToolsFocused()), set viaGetView()->SetDelegate(this), andInspectableWebContentsDelegate(theDevTools*File/*FileSystem/ reload / eyedropper handlers), set viainspectable_web_contents_->SetDelegate(this).The merged fix for #19985 (PR #47435) cleared the view delegate in
~WebContents()so a lateDevToolsOpened()becomes a no-op. The siblingInspectableWebContentsdelegate was left dangling, however: its handlers inshell/browser/ui/inspectable_web_contents.cc(lines ~768–883) all calldelegate_->DevTools*()viabase::Unretainedand were never reset on teardown, so a queued embedder message routed there after theWebContentsis 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). Everydelegate_->call site is already guarded byif (delegate_), so nulling it makes a late callback a safe no-op. The dispatch entry point (HandleMessageFromDevToolsFrontend/SendMessageAck) is already bound throughInspectableWebContents's ownweak_factory_, so a message arriving afterInspectableWebContentsitself 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
WebContentsre-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 theMenuItem toggleDevToolssuite inspec/api-menu-item-spec.ts.Checklist
npm testpassesRelease 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