Skip to content

fix: defer Wrappable destruction in SecondWeakCallback to a posted task - #50688

Merged
MarshallOfSound merged 1 commit into
mainfrom
fix-disallow-js-execution-crash
Apr 5, 2026
Merged

MarshallOfSound merged 1 commit into
mainfrom
fix-disallow-js-execution-crash

Conversation

@MarshallOfSound

Copy link
Copy Markdown
Member

Description of Change

V8's second-pass weak callbacks run inside a DisallowJavascriptExecutionScope — they may touch the V8 API but must not invoke JavaScript, directly or indirectly. Several Electron gin_helper::Wrappable subclasses (WebContents in particular) emit JS events from their destructors ('will-destroy' / 'destroyed'), so when SecondWeakCallback synchronously deletes the C++ object inside that scope, the destructor's Emit() → node::MakeCallback → v8::Function::Call aborts with:

# Fatal error in ../../v8/src/execution/execution.cc, line 364
# Invoke in DisallowJavascriptExecutionScope

This was previously latent and timing-dependent — it required GC to collect the JS wrapper from a foreground GC task posted by V8's incremental marking finalizer, which only happened under specific heap-layout / allocation-timing conditions. It surfaced as #47420, #45416, podman-desktop/podman-desktop#12409, and intermittent flakes of spec/fixtures/crash-cases/webcontentsview-create-leak-exit.

Fix

Both WrappableBase::SecondWeakCallback and DeprecatedWrappableBase::SecondWeakCallback now post the deletion via base::SequencedTaskRunner::GetCurrentDefault()->{DeleteSoon,PostTask} so the destructor (and any Emit() it does) runs once V8 has left the GC scope. Falls back to synchronous deletion when no task runner is bound (early/late process lifetime).

Regression test

spec/fixtures/crash-cases/wrappable-gc-weak-callback-emit/ reproduces the crash deterministically on main by setting v8.setFlagsFromString('--stress-incremental-marking') (which forces foreground GC tasks on every allocation step) and leaking three WebContentsView instances before app.quit(). Verified on a Linux testing build:

runs crashes
Before fix 5/5 5
After fix 10/10 0

All 23 existing crash-case fixtures continue to pass.

Checklist

  • PR description has a clear explanation of the change
  • npm run lint passes
  • regression test added (spec/fixtures/crash-cases/wrappable-gc-weak-callback-emit)
  • all spec/fixtures/crash-cases/* pass

Fixes #47420
Fixes #45416

Notes: Fixed an intermittent Invoke in DisallowJavascriptExecutionScope crash on application quit when a WebContents (or other JS-emitting native object) is garbage-collected during shutdown.

@MarshallOfSound MarshallOfSound added semver/patch backwards-compatible bug fixes target/40-x-y PR should also be added to the "40-x-y" branch. target/41-x-y PR should also be added to the "41-x-y" branch. target/42-x-y PR should also be added to the "42-x-y" branch. labels Apr 4, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Apr 4, 2026
@MarshallOfSound
MarshallOfSound force-pushed the fix-disallow-js-execution-crash branch from 65b2fa8 to 3d4c3a9 Compare April 5, 2026 00:40

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

LGTM

Those destruction events are a problem even when we move the class to cppgc, will see how that goes.

Comment thread shell/common/gin_helper/wrappable.cc Resolved
V8's second-pass weak callbacks run inside a
DisallowJavascriptExecutionScope: they may touch the V8 API but must
not invoke JS, directly or indirectly. Several Electron Wrappables
(WebContents in particular) emit JS events from their destructors,
so deleting synchronously inside SecondWeakCallback can crash with
"Invoke in DisallowJavascriptExecutionScope" when GC happens to
collect the JS wrapper during a foreground GC task — typically during
shutdown's uv_run drain after a leaked WebContentsView.

This was previously latent and timing-dependent (#47420,
#45416, podman-desktop/podman-desktop#12409). The
esbuild migration's keepNames option (which wraps every function/class
with an Object.defineProperty call) shifted heap layout enough to make
the spec/fixtures/crash-cases/webcontentsview-create-leak-exit case
reliably reproduce it on every run, giving a clean signal for the fix.

Both WrappableBase and DeprecatedWrappableBase SecondWeakCallback now
post the deletion via base::SequencedTaskRunner::GetCurrentDefault()
so the destructor (and any Emit it does) runs once V8 has left the GC
scope. Falls back to synchronous deletion if no task runner is
available (early/late process lifetime).

Fixes #47420.
@MarshallOfSound
MarshallOfSound force-pushed the fix-disallow-js-execution-crash branch from 3d4c3a9 to f89c8ef Compare April 5, 2026 05:46
@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) April 5, 2026 06:00
@MarshallOfSound
MarshallOfSound merged commit 3f8238b into main Apr 5, 2026
64 of 65 checks passed
@MarshallOfSound
MarshallOfSound deleted the fix-disallow-js-execution-crash branch April 5, 2026 07:38
@release-clerk

release-clerk Bot commented Apr 5, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed an intermittent Invoke in DisallowJavascriptExecutionScope crash on application quit when a WebContents (or other JS-emitting native object) is garbage-collected during shutdown.

@trop

trop Bot commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added in-flight/40-x-y and removed target/40-x-y PR should also be added to the "40-x-y" branch. labels Apr 5, 2026
@trop

trop Bot commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

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

@trop

trop Bot commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added the in-flight/41-x-y label Apr 5, 2026
@trop trop Bot added in-flight/42-x-y merged/40-x-y PR was merged to the "40-x-y" branch. merged/42-x-y PR was merged to the "42-x-y" branch. merged/41-x-y PR was merged to the "41-x-y" branch. and removed target/41-x-y PR should also be added to the "41-x-y" branch. target/42-x-y PR should also be added to the "42-x-y" branch. in-flight/40-x-y in-flight/42-x-y in-flight/41-x-y labels Apr 5, 2026

@ahamedy6 ahamedy6 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ahamed

benceruleanlu added a commit to Comfy-Org/Comfy-Desktop that referenced this pull request Nov 15, 2026
* fix(quit): stop the app from crashing while it quits

On quit, Desktop destroys its windows and drops its references to their
WebContentsViews (title bar, ComfyUI view, panel, popups, backdrops)
while the views' webContents are still alive. If V8 garbage-collects one
of those views before the process is gone, Electron 40.4.1 destroys its
webContents inside the collector, the webContents destructor emits a
JavaScript event, and V8 aborts the process with "Invoke in
DisallowJavascriptExecutionScope" (electron/electron#47420, fixed
upstream in electron/electron#50688, which 40.4.1 does not have).

The quit itself had already completed, so the only effect on most
machines is a crash report. On the Linux CI runners the aborted process
then spends minutes piping a core of its 1.4 TB address space to
systemd-coredump and keeps its stdio open, so Playwright's
ElectronApplication.close() never returns. That is the afterAll timeout
seen in e2e/picker-cluster.test.ts on CI run 43643528202: 15 of 1200
repeated teardowns of that spec hit it, each with the same fatal error.

Every WebContentsView is now created through createWebContentsView,
which holds the view until its webContents has been destroyed, so the
collector can no longer reach a view whose webContents is alive. A lint
rule keeps new code from constructing a view directly. The checkout
close button, the one view that relied on being collected to free its
webContents, now closes it when its window closes.

The new E2E test quits the app with a garbage collection forced during
shutdown and requires exit code 0.

* test(e2e): report the app's exit status even when the profile cannot be removed

Run without the fix, the quit test failed on Windows and macOS with EBUSY
and ENOTEMPTY from removing the profile folder: the main process had
crashed, its child processes were still holding files, and that cleanup
error replaced the exit-status assertion. The assertion is now soft so
it is always reported, and the test waits for the child's close event
(the process and everything holding its stdio gone) before cleaning up.

* fix(quit): wait for destroyed windows' webContents before shutting down

Holding the views Desktop creates was not enough: the quit test still
crashed on all three platforms, with the same stack through
~WebContentsView. The view being collected is the one every
BrowserWindow owns internally, which JavaScript cannot reach.

BrowserWindow.destroy() deletes the native window at once. Its
destructor only schedules the destruction of the window's webContents
and then releases the internal view, so for one task the view is
unreachable while its webContents is alive. Desktop destroys its windows
inside before-quit, and destroying the last one runs window-all-closed,
which calls app.quit() again, so Electron's shutdown starts in that same
turn. A collection before the scheduled task then destroys the
webContents inside the collector and V8 aborts.

before-quit now holds the quit while a destroyed window still has a live
webContents and quits again once they are all gone, so shutdown never
starts inside that gap. It stops waiting after two seconds, so the worst
case is the old behaviour, not an app that will not quit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged/40-x-y PR was merged to the "40-x-y" branch. merged/41-x-y PR was merged to the "41-x-y" branch. merged/42-x-y PR was merged to the "42-x-y" branch. new-pr 🌱 PR opened recently semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DisallowJavascriptExecutionScope crash Electron 34.x mysteriously crashes with Invoke in DisallowJavascriptExecutionScope

3 participants