Skip to content

fix: illegal access errors when a same-process child window or subframe closes - #52964

Merged
codebytere merged 7 commits into
mainfrom
node-env-per-frame-uv-loop
Aug 20, 2026
Merged

codebytere merged 7 commits into
mainfrom
node-env-per-frame-uv-loop

Conversation

@codebytere

@codebytere codebytere commented Aug 18, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Closes #33868.
Closes #36858.
Closes #40607.
Closes #51860.

Every Node.js environment on the renderer main thread (the main frame, nodeIntegrationInSubFrames iframes, same-process window.open() children) shared uv_default_loop(). Freeing one of them runs that loop until its handles close, with JavaScript disallowed on the isolate, so whatever immediates, timers, fs or threadpool callbacks the surviving page had pending fired inside that window and either threw Uncaught illegal access or aborted the renderer. The shared loop was also pumped under the main frame's context only, so napi async work and thread-safe functions used from a child window or subframe aborted in InternalCallbackScope (#33868).

Each frame's environment now gets its own loop integration, the same way worker environments already do: NodeBindings can integrate a given loop or own one, and the renderer keeps a NodeBindings + ElectronBindings pair per frame. A main frame's environment borrows the process-lifetime uv_default_loop() integration while it is free, so native modules that use the default loop keep working there; subframes and any further main frames own their loop. Tearing an environment down parks that loop's embed thread and frees it without touching other environments' handles.

  • Environments are keyed by RenderFrame, which replaces the uv_env hand-off between main-frame environments and the injected-frames bookkeeping.
  • A new environment gets its first loop run from a fresh task, since child contexts can be created from inside a script (window.open('about:blank')).
  • Isolate-level Node.js setup (NodeBindings::SetUpIsolate) now runs once per isolate instead of once per environment, so renderer frames no longer stack a V8 message listener each; the about:blank subframe skip's comment gives its remaining reason (sandboxed preload startup data) instead of this bug.

Checklist

Release Notes

Notes: Fixed Uncaught illegal access errors and renderer crashes when closing a same-process child window or removing an iframe with nodeIntegrationInSubFrames enabled, and when native modules use napi async work or thread-safe functions from such windows and frames.

Same-process child windows and node-integrated subframes each get a
Node.js environment, and until now all of them shared uv_default_loop()
on the renderer main thread. Freeing one (FreeEnvironment runs the loop
until that environment's handles close, with JavaScript disallowed on
the isolate) therefore dispatched the other environments' pending
immediates, timers, fs and threadpool callbacks in that window, which
threw "Uncaught illegal access" in the surviving page or aborted the
renderer (InternalCallbackScope / ToLocalChecked checks).

NodeBindings can now integrate a given loop or own a fresh one (as the
worker path already did), and the renderer keeps one NodeBindings +
ElectronBindings pair per frame environment: the first borrows the
process-lifetime uv_default_loop() integration so native modules that
use the default loop keep working, later ones own their loop. Tearing an
environment down parks that loop's embed thread and frees it without
touching anyone else's handles. This also replaces the uv_env hand-off
between main-frame environments and the injected-frames bookkeeping with
a RenderFrame-keyed map, and gives each new environment its first loop
run from a fresh task since child contexts can be created from inside a
script.

Fixes #36858
Fixes #40607
CreateEnvironment adds Electron's V8 message listener for renderer and
worker environments, and V8 keeps every listener it is given, so each
navigation or child frame added another copy and uncaught errors ran the
async-hook cleanup once per environment ever created on the isolate.
Remove the previous registration before adding.

Also restate why about:blank subframes are skipped for Node.js
integration: freeing their environment is no longer a problem, but they
commit synchronously in the renderer and so never receive the preload
startup data the sandboxed bundle needs.
@codebytere codebytere added semver/patch backwards-compatible bug fixes 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 Aug 18, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 18, 2026
…enderer environments

Adds an async export to the echo fixture (a libuv threadpool round trip
that calls back into JS, the napi path that aborted in
InternalCallbackScope when it completed under another environment's
context) and uses it to check that same-process child windows, including
a synchronously created about:blank one, run immediates, fs and napi work
on their own loop, that a child keeps working while its opener reloads,
and that the survivor's napi callbacks still arrive when a sibling
environment is freed.
A thread-safe function called from another thread reaches JS through a
uv_async_t on its environment's loop and an AsyncResource::CallbackScope,
so in a same-process child window it used to run under the opener's
context and abort in InternalCallbackScope. Give the echo fixture a
thread-safe variant and exercise it alongside the async work.

Refs #33868
@codebytere
codebytere marked this pull request as ready for review August 18, 2026 20:10
@codebytere
codebytere requested a review from deepak1556 August 19, 2026 09:12

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To fix this ideally we need a realm api that has (also discussed previously in #38754)

  1. its own context and global object
  2. its own module entry and resolution root
  3. its own module cache
  4. realm aware binding data
  5. per realm cleanup hooks

so that a thread model like below is possible.

one renderer thread
  -> one Environment + one uv loop
      ├── frame Realm A
      ├── frame Realm B
      └── frame Realm C

Current available AssignToContext and ShadowRealm doesn't seem to be sufficient. So, this seems to be a reasonable fix. Left a few comments before this can land.

Comment thread shell/common/node_bindings.cc Resolved
Pull the IsolateSettings / SetIsolateUpForNode / host-import-callback block
out of NodeBindings::CreateEnvironment into NodeBindings::SetUpIsolate, and
call it once from each owner: the renderer client's one-time init, the
WebWorkerObserver for its thread's isolate, and the browser and utility
processes right after their single CreateEnvironment (node's snapshot path
resets the isolate error handlers inside CreateEnvironment, so it has to
follow it there). Renderer frames no longer re-run isolate setup or stack a
message listener per environment.
Subframe environments always get a loop of their own; a main frame borrows
the process's uv_default_loop() integration while no other environment holds
it, so native modules that use the default loop see a consistent main-frame
environment.
Restore the explicit microtasks policy around freeing a frame's Node.js
environment (its loop runs during teardown, see #33280), and only tear down
when the released context is the environment's own. Teardown can now be
reached from inside script (an iframe removed with element.remove()), so
ExplicitMicrotasksScope no longer asserts a zero microtasks scope depth in
the renderer; explicit checkpoints are no-ops there until the enclosing
scope unwinds.
@codebytere
codebytere requested a review from deepak1556 August 19, 2026 16:15
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Aug 19, 2026
@codebytere
codebytere merged commit ec147de into main Aug 20, 2026
78 checks passed
@codebytere
codebytere deleted the node-env-per-frame-uv-loop branch August 20, 2026 08:19
@release-clerk

release-clerk Bot commented Aug 20, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed Uncaught illegal access errors and renderer crashes when closing a same-process child window or removing an iframe with nodeIntegrationInSubFrames enabled, and when native modules use napi async work or thread-safe functions from such windows and frames.

@trop

trop Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "44-x-y" cleanly;
you will need to perform this backport manually.

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

trop Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "43-x-y" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "42-x-y" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

@codebytere has manually backported this PR to "44-x-y", please check out #53040

@trop

trop Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

@codebytere has manually backported this PR to "43-x-y", please check out #53041

@trop

trop Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

@codebytere has manually backported this PR to "42-x-y", please check out #53042

jkleinsc pushed a commit that referenced this pull request Aug 20, 2026
…me closes (#53040)

fix: illegal access errors when a same-process child window or subframe closes (#52964)

* fix: give each renderer Node.js environment its own uv loop

Same-process child windows and node-integrated subframes each get a
Node.js environment, and until now all of them shared uv_default_loop()
on the renderer main thread. Freeing one (FreeEnvironment runs the loop
until that environment's handles close, with JavaScript disallowed on
the isolate) therefore dispatched the other environments' pending
immediates, timers, fs and threadpool callbacks in that window, which
threw "Uncaught illegal access" in the surviving page or aborted the
renderer (InternalCallbackScope / ToLocalChecked checks).

NodeBindings can now integrate a given loop or own a fresh one (as the
worker path already did), and the renderer keeps one NodeBindings +
ElectronBindings pair per frame environment: the first borrows the
process-lifetime uv_default_loop() integration so native modules that
use the default loop keep working, later ones own their loop. Tearing an
environment down parks that loop's embed thread and frees it without
touching anyone else's handles. This also replaces the uv_env hand-off
between main-frame environments and the injected-frames bookkeeping with
a RenderFrame-keyed map, and gives each new environment its first loop
run from a fresh task since child contexts can be created from inside a
script.

Fixes #36858
Fixes #40607

* fix: don't stack an error message listener per renderer environment

CreateEnvironment adds Electron's V8 message listener for renderer and
worker environments, and V8 keeps every listener it is given, so each
navigation or child frame added another copy and uncaught errors ran the
async-hook cleanup once per environment ever created on the isolate.
Remove the previous registration before adding.

Also restate why about:blank subframes are skipped for Node.js
integration: freeing their environment is no longer a problem, but they
commit synchronously in the renderer and so never receive the preload
startup data the sandboxed bundle needs.

* test: cover child-window loops, opener reloads and napi work across renderer environments

Adds an async export to the echo fixture (a libuv threadpool round trip
that calls back into JS, the napi path that aborted in
InternalCallbackScope when it completed under another environment's
context) and uses it to check that same-process child windows, including
a synchronously created about:blank one, run immediates, fs and napi work
on their own loop, that a child keeps working while its opener reloads,
and that the survivor's napi callbacks still arrive when a sibling
environment is freed.

* test: cover napi thread-safe functions in child window environments

A thread-safe function called from another thread reaches JS through a
uv_async_t on its environment's loop and an AsyncResource::CallbackScope,
so in a same-process child window it used to run under the opener's
context and abort in InternalCallbackScope. Give the echo fixture a
thread-safe variant and exercise it alongside the async work.

Refs #33868

* refactor: set the isolate up for Node.js once per isolate

Pull the IsolateSettings / SetIsolateUpForNode / host-import-callback block
out of NodeBindings::CreateEnvironment into NodeBindings::SetUpIsolate, and
call it once from each owner: the renderer client's one-time init, the
WebWorkerObserver for its thread's isolate, and the browser and utility
processes right after their single CreateEnvironment (node's snapshot path
resets the isolate error handlers inside CreateEnvironment, so it has to
follow it there). Renderer frames no longer re-run isolate setup or stack a
message listener per environment.

* fix: only lend uv_default_loop() to main frame environments

Subframe environments always get a loop of their own; a main frame borrows
the process's uv_default_loop() integration while no other environment holds
it, so native modules that use the default loop see a consistent main-frame
environment.

* fix: free a frame's environment under the explicit microtasks policy

Restore the explicit microtasks policy around freeing a frame's Node.js
environment (its loop runs during teardown, see #33280), and only tear down
when the released context is the environment's own. Teardown can now be
reached from inside script (an iframe removed with element.remove()), so
ExplicitMicrotasksScope no longer asserts a zero microtasks scope depth in
the renderer; explicit checkpoints are no-ops there until the enclosing
scope unwinds.

(cherry picked from commit ec147de)
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. and removed in-flight/44-x-y labels Aug 20, 2026
MarshallOfSound pushed a commit that referenced this pull request Aug 22, 2026
…me closes (#53042)

* fix: illegal access errors when a same-process child window or subframe closes (#52964)

* fix: give each renderer Node.js environment its own uv loop

Same-process child windows and node-integrated subframes each get a
Node.js environment, and until now all of them shared uv_default_loop()
on the renderer main thread. Freeing one (FreeEnvironment runs the loop
until that environment's handles close, with JavaScript disallowed on
the isolate) therefore dispatched the other environments' pending
immediates, timers, fs and threadpool callbacks in that window, which
threw "Uncaught illegal access" in the surviving page or aborted the
renderer (InternalCallbackScope / ToLocalChecked checks).

NodeBindings can now integrate a given loop or own a fresh one (as the
worker path already did), and the renderer keeps one NodeBindings +
ElectronBindings pair per frame environment: the first borrows the
process-lifetime uv_default_loop() integration so native modules that
use the default loop keep working, later ones own their loop. Tearing an
environment down parks that loop's embed thread and frees it without
touching anyone else's handles. This also replaces the uv_env hand-off
between main-frame environments and the injected-frames bookkeeping with
a RenderFrame-keyed map, and gives each new environment its first loop
run from a fresh task since child contexts can be created from inside a
script.

Fixes #36858
Fixes #40607

* fix: don't stack an error message listener per renderer environment

CreateEnvironment adds Electron's V8 message listener for renderer and
worker environments, and V8 keeps every listener it is given, so each
navigation or child frame added another copy and uncaught errors ran the
async-hook cleanup once per environment ever created on the isolate.
Remove the previous registration before adding.

Also restate why about:blank subframes are skipped for Node.js
integration: freeing their environment is no longer a problem, but they
commit synchronously in the renderer and so never receive the preload
startup data the sandboxed bundle needs.

* test: cover child-window loops, opener reloads and napi work across renderer environments

Adds an async export to the echo fixture (a libuv threadpool round trip
that calls back into JS, the napi path that aborted in
InternalCallbackScope when it completed under another environment's
context) and uses it to check that same-process child windows, including
a synchronously created about:blank one, run immediates, fs and napi work
on their own loop, that a child keeps working while its opener reloads,
and that the survivor's napi callbacks still arrive when a sibling
environment is freed.

* test: cover napi thread-safe functions in child window environments

A thread-safe function called from another thread reaches JS through a
uv_async_t on its environment's loop and an AsyncResource::CallbackScope,
so in a same-process child window it used to run under the opener's
context and abort in InternalCallbackScope. Give the echo fixture a
thread-safe variant and exercise it alongside the async work.

Refs #33868

* refactor: set the isolate up for Node.js once per isolate

Pull the IsolateSettings / SetIsolateUpForNode / host-import-callback block
out of NodeBindings::CreateEnvironment into NodeBindings::SetUpIsolate, and
call it once from each owner: the renderer client's one-time init, the
WebWorkerObserver for its thread's isolate, and the browser and utility
processes right after their single CreateEnvironment (node's snapshot path
resets the isolate error handlers inside CreateEnvironment, so it has to
follow it there). Renderer frames no longer re-run isolate setup or stack a
message listener per environment.

* fix: only lend uv_default_loop() to main frame environments

Subframe environments always get a loop of their own; a main frame borrows
the process's uv_default_loop() integration while no other environment holds
it, so native modules that use the default loop see a consistent main-frame
environment.

* fix: free a frame's environment under the explicit microtasks policy

Restore the explicit microtasks policy around freeing a frame's Node.js
environment (its loop runs during teardown, see #33280), and only tear down
when the released context is the environment's own. Teardown can now be
reached from inside script (an iframe removed with element.remove()), so
ExplicitMicrotasksScope no longer asserts a zero microtasks scope depth in
the renderer; explicit checkpoints are no-ops there until the enclosing
scope unwinds.

(cherry picked from commit ec147de)

* chore: fix lint
@trop trop Bot added merged/42-x-y PR was merged to the "42-x-y" branch. and removed in-flight/42-x-y labels Aug 22, 2026
codebytere added a commit that referenced this pull request Aug 24, 2026
…me closes (#52964)

* fix: give each renderer Node.js environment its own uv loop

Same-process child windows and node-integrated subframes each get a
Node.js environment, and until now all of them shared uv_default_loop()
on the renderer main thread. Freeing one (FreeEnvironment runs the loop
until that environment's handles close, with JavaScript disallowed on
the isolate) therefore dispatched the other environments' pending
immediates, timers, fs and threadpool callbacks in that window, which
threw "Uncaught illegal access" in the surviving page or aborted the
renderer (InternalCallbackScope / ToLocalChecked checks).

NodeBindings can now integrate a given loop or own a fresh one (as the
worker path already did), and the renderer keeps one NodeBindings +
ElectronBindings pair per frame environment: the first borrows the
process-lifetime uv_default_loop() integration so native modules that
use the default loop keep working, later ones own their loop. Tearing an
environment down parks that loop's embed thread and frees it without
touching anyone else's handles. This also replaces the uv_env hand-off
between main-frame environments and the injected-frames bookkeeping with
a RenderFrame-keyed map, and gives each new environment its first loop
run from a fresh task since child contexts can be created from inside a
script.

Fixes #36858
Fixes #40607

* fix: don't stack an error message listener per renderer environment

CreateEnvironment adds Electron's V8 message listener for renderer and
worker environments, and V8 keeps every listener it is given, so each
navigation or child frame added another copy and uncaught errors ran the
async-hook cleanup once per environment ever created on the isolate.
Remove the previous registration before adding.

Also restate why about:blank subframes are skipped for Node.js
integration: freeing their environment is no longer a problem, but they
commit synchronously in the renderer and so never receive the preload
startup data the sandboxed bundle needs.

* test: cover child-window loops, opener reloads and napi work across renderer environments

Adds an async export to the echo fixture (a libuv threadpool round trip
that calls back into JS, the napi path that aborted in
InternalCallbackScope when it completed under another environment's
context) and uses it to check that same-process child windows, including
a synchronously created about:blank one, run immediates, fs and napi work
on their own loop, that a child keeps working while its opener reloads,
and that the survivor's napi callbacks still arrive when a sibling
environment is freed.

* test: cover napi thread-safe functions in child window environments

A thread-safe function called from another thread reaches JS through a
uv_async_t on its environment's loop and an AsyncResource::CallbackScope,
so in a same-process child window it used to run under the opener's
context and abort in InternalCallbackScope. Give the echo fixture a
thread-safe variant and exercise it alongside the async work.

Refs #33868

* refactor: set the isolate up for Node.js once per isolate

Pull the IsolateSettings / SetIsolateUpForNode / host-import-callback block
out of NodeBindings::CreateEnvironment into NodeBindings::SetUpIsolate, and
call it once from each owner: the renderer client's one-time init, the
WebWorkerObserver for its thread's isolate, and the browser and utility
processes right after their single CreateEnvironment (node's snapshot path
resets the isolate error handlers inside CreateEnvironment, so it has to
follow it there). Renderer frames no longer re-run isolate setup or stack a
message listener per environment.

* fix: only lend uv_default_loop() to main frame environments

Subframe environments always get a loop of their own; a main frame borrows
the process's uv_default_loop() integration while no other environment holds
it, so native modules that use the default loop see a consistent main-frame
environment.

* fix: free a frame's environment under the explicit microtasks policy

Restore the explicit microtasks policy around freeing a frame's Node.js
environment (its loop runs during teardown, see #33280), and only tear down
when the released context is the environment's own. Teardown can now be
reached from inside script (an iframe removed with element.remove()), so
ExplicitMicrotasksScope no longer asserts a zero microtasks scope depth in
the renderer; explicit checkpoints are no-ops there until the enclosing
scope unwinds.

(cherry picked from commit ec147de)
MarshallOfSound pushed a commit that referenced this pull request Aug 25, 2026
…me closes (#53041)

* fix: illegal access errors when a same-process child window or subframe closes (#52964)

* fix: give each renderer Node.js environment its own uv loop

Same-process child windows and node-integrated subframes each get a
Node.js environment, and until now all of them shared uv_default_loop()
on the renderer main thread. Freeing one (FreeEnvironment runs the loop
until that environment's handles close, with JavaScript disallowed on
the isolate) therefore dispatched the other environments' pending
immediates, timers, fs and threadpool callbacks in that window, which
threw "Uncaught illegal access" in the surviving page or aborted the
renderer (InternalCallbackScope / ToLocalChecked checks).

NodeBindings can now integrate a given loop or own a fresh one (as the
worker path already did), and the renderer keeps one NodeBindings +
ElectronBindings pair per frame environment: the first borrows the
process-lifetime uv_default_loop() integration so native modules that
use the default loop keep working, later ones own their loop. Tearing an
environment down parks that loop's embed thread and frees it without
touching anyone else's handles. This also replaces the uv_env hand-off
between main-frame environments and the injected-frames bookkeeping with
a RenderFrame-keyed map, and gives each new environment its first loop
run from a fresh task since child contexts can be created from inside a
script.

Fixes #36858
Fixes #40607

* fix: don't stack an error message listener per renderer environment

CreateEnvironment adds Electron's V8 message listener for renderer and
worker environments, and V8 keeps every listener it is given, so each
navigation or child frame added another copy and uncaught errors ran the
async-hook cleanup once per environment ever created on the isolate.
Remove the previous registration before adding.

Also restate why about:blank subframes are skipped for Node.js
integration: freeing their environment is no longer a problem, but they
commit synchronously in the renderer and so never receive the preload
startup data the sandboxed bundle needs.

* test: cover child-window loops, opener reloads and napi work across renderer environments

Adds an async export to the echo fixture (a libuv threadpool round trip
that calls back into JS, the napi path that aborted in
InternalCallbackScope when it completed under another environment's
context) and uses it to check that same-process child windows, including
a synchronously created about:blank one, run immediates, fs and napi work
on their own loop, that a child keeps working while its opener reloads,
and that the survivor's napi callbacks still arrive when a sibling
environment is freed.

* test: cover napi thread-safe functions in child window environments

A thread-safe function called from another thread reaches JS through a
uv_async_t on its environment's loop and an AsyncResource::CallbackScope,
so in a same-process child window it used to run under the opener's
context and abort in InternalCallbackScope. Give the echo fixture a
thread-safe variant and exercise it alongside the async work.

Refs #33868

* refactor: set the isolate up for Node.js once per isolate

Pull the IsolateSettings / SetIsolateUpForNode / host-import-callback block
out of NodeBindings::CreateEnvironment into NodeBindings::SetUpIsolate, and
call it once from each owner: the renderer client's one-time init, the
WebWorkerObserver for its thread's isolate, and the browser and utility
processes right after their single CreateEnvironment (node's snapshot path
resets the isolate error handlers inside CreateEnvironment, so it has to
follow it there). Renderer frames no longer re-run isolate setup or stack a
message listener per environment.

* fix: only lend uv_default_loop() to main frame environments

Subframe environments always get a loop of their own; a main frame borrows
the process's uv_default_loop() integration while no other environment holds
it, so native modules that use the default loop see a consistent main-frame
environment.

* fix: free a frame's environment under the explicit microtasks policy

Restore the explicit microtasks policy around freeing a frame's Node.js
environment (its loop runs during teardown, see #33280), and only tear down
when the released context is the environment's own. Teardown can now be
reached from inside script (an iframe removed with element.remove()), so
ExplicitMicrotasksScope no longer asserts a zero microtasks scope depth in
the renderer; explicit checkpoints are no-ops there until the enclosing
scope unwinds.

(cherry picked from commit ec147de)

* chore: fix lint
@trop trop Bot added merged/43-x-y PR was merged to the "43-x-y" branch. and removed in-flight/43-x-y labels Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

45-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/44-x-y PR was merged to the "44-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

Status: No status

2 participants