Repository navigation
fix: crash when a node-integrated iframe is removed before its loop starts - #53833
Conversation
…tarts A nodeIntegrationInSubFrames frame's NodeBindings only initializes its dummy_uv_handle_ from the task that first runs the frame's loop. A frame removed before that task ran destroyed a uv_async_t that never went through uv_async_init(), so uv_close() ran on uninitialized memory. UvHandle now value-initializes its handle, and reset() frees one that was never initialized instead of closing it. Fixes #53789.
6b396f4 to
710611a
Compare
|
@claude review |
There was a problem hiding this comment.
Looks correct — I traced the crash path through shell/common/node_bindings.cc and confirmed the fix matches the description: ~NodeBindings() only explicitly resets dummy_uv_handle_ when embed_thread_prepared_ is true (i.e. PrepareEmbedThread() ran and called uv_async_init); when a frame is torn down before that, the member's own destructor still calls reset() on the never-initialized handle. The old code unconditionally called uv_close() there, which is what crashed. Value-initializing the wrapped struct (new T{}) makes an uninitialized handle's type field read as UV_UNKNOWN_HANDLE, and reset() now just deletes in that case instead of calling uv_close().
What was reviewed: the UvHandle constructor/reset changes in shell/common/node_bindings.h, the NodeBindings::~NodeBindings/PrepareEmbedThread interaction in shell/common/node_bindings.cc that the fix depends on, and the other UvHandle<...> call sites (shell/common/api/electron_bindings.h, shell/app/uv_task_runner.cc) to confirm they all init immediately after construction and are unaffected. The new spec test sits inside the existing describe('environments sharing a renderer', ...) block with afterEach(closeAllWindows), so the test window is cleaned up, and it avoids asserting inside the render-process-gone listener, using Promise.race instead.
Extended reasoning...
Overview
The diff is a small, self-contained bug fix in shell/common/node_bindings.h (the UvHandle<T> helper template used to wrap libuv handles) plus a regression test (spec/node-spec.ts) and a tiny preload fixture (spec/fixtures/module/preload-remove-own-frame.js). No public API, docs, or patches are touched.
Security risks
None identified. This is main-process C++ handle-lifecycle bookkeeping around libuv, not reachable from renderer-controlled input, and not part of context-isolation/sandbox/IPC argument validation. The class is a plain heap-new/delete-managed non-GC wrapper (not cppgc-managed), so the Oilpan rules in CLAUDE.md aren't implicated.
Level of scrutiny
This is exactly the kind of small, mechanical, well-tested fix that merits approval: it's a two-branch change to a reset() method with a clear, traceable root cause. I verified the fix's premise directly against NodeBindings::~NodeBindings() and PrepareEmbedThread() in shell/common/node_bindings.cc (lines ~554-567 and ~1095-1121): the explicit dummy_uv_handle_.reset() call is guarded by embed_thread_prepared_, so a frame destroyed before PrepareEmbedThread() first runs leaves the handle to be reset only via the member destructor — which is exactly the path the old uv_close()-always code crashed on. Value-initializing the wrapped struct and branching on UV_UNKNOWN_HANDLE in reset() is a sound, minimal fix, and I checked the other two UvHandle<...> use sites in the repo (shell/common/api/electron_bindings.h, shell/app/uv_task_runner.cc) to confirm they always call the corresponding uv_*_init immediately after construction, so this change doesn't alter their behavior.
Other factors
The new test follows existing conventions in the same file (uses the describe's existing afterEach(closeAllWindows), avoids asserting inside an event listener, uses Promise.race against render-process-gone for a readable failure mode instead of a bare timeout). The PR author states it reproduced the crash 21/21 without the fix and passed 10/10 with it, which is consistent with the race-condition nature of the bug. No CODEOWNERS entries cover the changed files. I could not verify libuv-internal details (e.g. uv_handle_get_type, the numeric value of UV_UNKNOWN_HANDLE) against a synced Chromium/libuv checkout since this appears to be a standalone clone, but these are long-standed, widely documented parts of libuv's public API, and the code's local logic is internally consistent and directly explains the reported crash.
|
Release Notes Persisted
|
|
I have automatically backported this PR to "45-x-y", please check out #53883 |
|
I have automatically backported this PR to "43-x-y", please check out #53884 |
|
I have automatically backported this PR to "44-x-y", please check out #53885 |
|
I have automatically backported this PR to "42-x-y", please check out #53886 |
Fixes #53789.
#52964 gave each
nodeIntegrationInSubFramesframe its ownNodeBindings, whosedummy_uv_handle_is only initialized by the task that first runs the frame's loop. A frame removed before that task runs (Meta Pixel's hidden form POST iframes hit this) destroyed auv_async_tthat never went throughuv_async_init(), souv_close()ran on uninitialized memory: a DCHECK in testing builds, heap corruption and an eventual renderer crash in release.UvHandlevalue-initializes its handle, andreset()frees one that was never initialized (stillUV_UNKNOWN_HANDLE) instead of closing it.Notes: Fixed a renderer crash when an iframe was removed right after loading with
nodeIntegrationInSubFramesenabled andsandboxdisabled, e.g. on pages that embed Meta Pixel.