Skip to content

fix: crash when a preload removes its own iframe asynchronously - #53838

Merged
MarshallOfSound merged 4 commits into
mainfrom
sam/defer-preload-microtasks-and-ticks
Sep 18, 2026
Merged

MarshallOfSound merged 4 commits into
mainfrom
sam/defer-preload-microtasks-and-ticks

Conversation

@MarshallOfSound

@MarshallOfSound MarshallOfSound commented Sep 12, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #53833, and based on it.

An iframe with nodeIntegrationInSubFrames whose preload removed its own frame asynchronously crashed the renderer, whether the removal ran from a promise reaction, process.nextTick(), setImmediate() or another Node.js callback. This predates #52964: 42.3.1 crashes the same way.

  • The frame observer holds off microtask checkpoints, Node.js's explicit ones included, while Blink and content are inside DidClearWindowObject() / DidInstallConditionalFeatures(). A preload's promise reactions used to run from any native call it made, underneath those callbacks, and one that detached the frame freed the RenderFrame and window proxy they return to. They now run once those callbacks return, still before page scripts.
  • process.nextTick() callbacks queued during bootstrap no longer run as bootstrap's callback scope closes, for the same reason. A microtask queued behind the preload's own promise reactions runs them, keeping the existing order.
  • A frame released from inside one of its own Node.js callbacks has its environment and loop freed from a fresh task instead of underneath that callback, and cannot call back into JS in between.
  • New specs cover removal from each of those, and the order of preload promise reactions, nextTick callbacks and page scripts.

A preload that removes its own frame synchronously still crashes: that runs inside the same Blink and content callbacks and needs a different fix.

Notes: Fixed a renderer crash when a nodeIntegrationInSubFrames preload removed its own iframe from a promise, process.nextTick() or other Node.js callback.

@MarshallOfSound MarshallOfSound added the semver/patch backwards-compatible bug fixes label Sep 12, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Sep 12, 2026
@MarshallOfSound

Copy link
Copy Markdown
Member Author

@claude review

…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.
Preload scripts run inside the callbacks Blink and content use to set up a
frame's script contexts. A preload promise reaction or process.nextTick()
callback that removed its own frame ran underneath those callbacks and
crashed the renderer.

- The frame observer holds off microtask checkpoints until those callbacks
  return.
- process.nextTick() callbacks queued during bootstrap run from a microtask
  afterwards, keeping their existing order after promise reactions.
- A frame released from inside one of its own Node.js callbacks has its
  environment and loop freed from a fresh task.
@MarshallOfSound
MarshallOfSound force-pushed the sam/defer-preload-microtasks-and-ticks branch from e9ace6c to f67d4ef Compare September 12, 2026 00:44
@MarshallOfSound
MarshallOfSound force-pushed the sam/fix-subframe-uv-handle-teardown branch from 6b396f4 to 710611a Compare September 12, 2026 00:44

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed this PR and didn't find any bugs. Because it's a subtle change to renderer-process Node.js environment lifecycle and microtask/teardown ordering (exactly the kind of crash-fix code where a use-after-free or ordering bug would be easy to miss), a human look would still be worthwhile.

What was reviewed: the new DeferMicrotasksScope wrapping DidClearWindowObject()/DidInstallConditionalFeatures() in electron_render_frame_observer.cc; the InternalCallbackScope-wrapped LoadEnvironment() call and the new RunTicksQueuedDuringSetup() microtask in electron_renderer_client.cc; and the deferred FreeReleasedEnvironments() path in WillReleaseScriptContext() guarding teardown from inside an environment's own callback. Also checked whether the deferred-free guard's own_node_bindings condition could skip the shared/primary-loop frame, and whether FreeFrameEnvironment() needed to enter env->context() explicitly before resetting — both were examined and did not turn up an independently confirmable defect beyond what the inline findings (if any) already cover.

Extended reasoning...

Overview

This PR modifies renderer-process Node.js environment lifecycle handling across three C++ files: shell/renderer/electron_render_frame_observer.cc, shell/renderer/electron_renderer_client.cc, and its header. It adds a stack-allocated DeferMicrotasksScope around Blink's DidClearWindowObject()/DidInstallConditionalFeatures() callbacks, wraps LoadEnvironment() in a node::InternalCallbackScope that skips async hooks/task queues so bootstrap-queued process.nextTick() callbacks don't run underneath Blink's own setup, and reworks WillReleaseScriptContext() to defer freeing a frame's Node environment to a fresh task when the frame is released from inside its own Node.js callback (via a new released_environments_ vector and FreeReleasedEnvironments()/FreeFrameEnvironment()). Test coverage is extended in spec/node-spec.ts with new fixtures for frame self-removal and task ordering.

Security risks

The change is squarely about preventing a use-after-free: a preload script that removes its own iframe asynchronously (from a promise reaction, process.nextTick(), or setImmediate()) previously could free the RenderFrame/window proxy while Blink/content code above it on the stack was still using them. The fix's own correctness is the security surface here — an incomplete fix could leave a narrower UAF window rather than closing it. No new externally-reachable API surface, IPC, or contextBridge exposure is introduced.

Level of scrutiny

This warrants a full, careful human read: it changes ordering guarantees around V8 microtask checkpoints and Node.js's InternalCallbackScope/CallbackScope, touches when/how a node::Environment and its uv loop are torn down, and interacts with GC/lifetime concerns of the kind this repo's CLAUDE.md flags as high-risk (careful ownership around WeakPtr, task-posted teardown, can_call_into_js(false)). It is exactly the category of complex, lifecycle-sensitive renderer code the review guidelines say should not be rubber-stamped by an automated pass alone, even with no findings from this run's bug hunt.

Other factors

The bug-hunting run reported no confirmed bugs, and two candidate concerns (whether FreeFrameEnvironment() needs to enter env->context() before resetting, and whether the own_node_bindings guard could miss the frame sharing the primary/process-wide loop) were investigated and ruled out without independent confirmation of a defect. New spec coverage (preload-remove-own-frame.js, preload-task-order.js, task-order.html, and additions to node-spec.ts) exercises the frame-removal and task-ordering scenarios the PR describes, which is a positive signal, but does not substitute for a human review of the C++ lifecycle logic itself.

This review covers commit e9ace6c, which is no longer the latest commit on this pull request; later commits are not covered by it.

@MarshallOfSound MarshallOfSound removed the new-pr 🌱 PR opened recently label Sep 12, 2026
Base automatically changed from sam/fix-subframe-uv-handle-teardown to main September 13, 2026 06:52
A document Blink finishes inside the commit (about:blank, a media or
plain-text response) reaches RunScriptsAtDocumentStart in the same task
that created the frame's environment, before any microtask checkpoint,
so the nextTick callbacks queued during setup are still pending. The
callback scope in EmitEvent then runs them outside any MicrotasksScope,
which trips V8's scoped-microtasks check in dcheck builds. Mirror
RunScriptsAtDocumentEnd and hold a MicrotasksScope around the emit, and
cover the about:blank case in the spec.
@release-clerk

release-clerk Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Release note looks good now.

@claude

claude Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

windows-x64 / test (win, 2) is red on 12f6a74 with a single failure, iframe sandbox external protocols > blocks navigation to external protocol from a sandboxed iframe, which isn't this PR's: the spec takes the first console-message, and since #53970 the main frame's "Electron Security Warning (Insecure Content-Security-Policy)" message races the iframe's "Navigation to external protocol blocked by sandbox" one on Windows. The same spec failed twice and passed on its third retry in #53970's own Windows shard 2 (job); here it lost all four attempts. No fix is on main yet; the obvious one is to wait for the matching message (e.g. emittedUntil(w.webContents, 'console-message', ({ message }) => !message.startsWith('Electron Security Warning'))), either here or in a separate spec PR. macos-arm64 / build died in the "Set Chromium Git Cookie" setup step (curl exit 6) before building; I'll re-run the failed jobs once the run finishes.


Generated by Claude Code

@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) September 18, 2026 06:24
@MarshallOfSound
MarshallOfSound merged commit 3e34513 into main Sep 18, 2026
122 of 125 checks passed
@MarshallOfSound
MarshallOfSound deleted the sam/defer-preload-microtasks-and-ticks branch September 18, 2026 06:24
@release-clerk

release-clerk Bot commented Sep 18, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed a renderer crash when a nodeIntegrationInSubFrames preload removed its own iframe from a promise, process.nextTick() or other Node.js callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

46-x-y no-backport semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants