Repository navigation
fix: illegal access errors when a same-process child window or subframe closes - #52964
Conversation
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.
…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
deepak1556
left a comment
There was a problem hiding this comment.
To fix this ideally we need a realm api that has (also discussed previously in #38754)
- its own context and global object
- its own module entry and resolution root
- its own module cache
- realm aware binding data
- 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.
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.
|
Release Notes Persisted
|
|
I was unable to backport this PR to "44-x-y" cleanly; |
|
I was unable to backport this PR to "43-x-y" cleanly; |
|
I was unable to backport this PR to "42-x-y" cleanly; |
|
@codebytere has manually backported this PR to "44-x-y", please check out #53040 |
|
@codebytere has manually backported this PR to "43-x-y", please check out #53041 |
|
@codebytere has manually backported this PR to "42-x-y", please check out #53042 |
…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)
…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
…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)
…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
Description of Change
Closes #33868.
Closes #36858.
Closes #40607.
Closes #51860.
Every Node.js environment on the renderer main thread (the main frame,
nodeIntegrationInSubFramesiframes, same-processwindow.open()children) shareduv_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 threwUncaught illegal accessor 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 inInternalCallbackScope(#33868).Each frame's environment now gets its own loop integration, the same way worker environments already do:
NodeBindingscan integrate a given loop or own one, and the renderer keeps aNodeBindings+ElectronBindingspair per frame. A main frame's environment borrows the process-lifetimeuv_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.RenderFrame, which replaces theuv_envhand-off between main-frame environments and the injected-frames bookkeeping.window.open('about:blank')).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 accesserrors and renderer crashes when closing a same-process child window or removing an iframe withnodeIntegrationInSubFramesenabled, and when native modules use napi async work or thread-safe functions from such windows and frames.