Skip to content

fix: crash when destroying node env with pending promises - #33280

Merged
codebytere merged 2 commits into
mainfrom
robo/fix_node_env_crash
Mar 16, 2022
Merged

codebytere merged 2 commits into
mainfrom
robo/fix_node_env_crash

Conversation

@deepak1556

@deepak1556 deepak1556 commented Mar 15, 2022 •

Copy link
Copy Markdown
Member

Description of Change

Fixes #33164

Checklist

Release Notes

Notes: fix crash in the render process on reload with pending node fs.promises

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Mar 15, 2022
@deepak1556
deepak1556 requested review from codebytere and zcbenz March 15, 2022 12:24
@deepak1556 deepak1556 added semver/patch backwards-compatible bug fixes target/15-x-y labels Mar 15, 2022

@codebytere codebytere 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.

We should add a test - we should just need a very big file to load via fs.promises.read

@deepak1556
deepak1556 force-pushed the robo/fix_node_env_crash branch from 4d92d7f to f4fa04a Compare March 16, 2022 07:22
@deepak1556
deepak1556 requested a review from codebytere March 16, 2022 07:22
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Mar 16, 2022
@codebytere
codebytere merged commit b2c5623 into main Mar 16, 2022
@codebytere
codebytere deleted the robo/fix_node_env_crash branch March 16, 2022 17:54
@release-clerk

release-clerk Bot commented Mar 16, 2022

Copy link
Copy Markdown

Release Notes Persisted

fix crash in the render process on reload with pending node fs.promises

@trop

trop Bot commented Mar 16, 2022

Copy link
Copy Markdown
Contributor

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

@trop

trop Bot commented Mar 16, 2022

Copy link
Copy Markdown
Contributor

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

@trop

trop Bot commented Mar 16, 2022

Copy link
Copy Markdown
Contributor

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

@trop trop Bot removed the target/18-x-y label Mar 16, 2022
@trop

trop Bot commented Mar 16, 2022

Copy link
Copy Markdown
Contributor

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

@deepak1556

Copy link
Copy Markdown
Member Author

/trop run backport

@trop

trop Bot commented Mar 18, 2022

Copy link
Copy Markdown
Contributor

The backport process for this PR has been manually initiated - here we go! :D

@trop

trop Bot commented Mar 18, 2022

Copy link
Copy Markdown
Contributor

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

bavulapati pushed a commit to bavulapati/electron that referenced this pull request Apr 29, 2022
…3280)

* fix: crash when destroying node env with pending promises

* chore: add spec
khalwa pushed a commit to solarwindscloud/electron that referenced this pull request Feb 22, 2023
…3280)

* fix: crash when destroying node env with pending promises

* chore: add spec
codebytere added a commit that referenced this pull request Aug 19, 2026
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 added a commit that referenced this pull request Aug 20, 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.
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)
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Blank page on location change / reload if a filesystem asynchronous call was pending

2 participants