Repository navigation
fix: defer Wrappable destruction in SecondWeakCallback to a posted task - #50688
Merged
Merged
Conversation
MarshallOfSound
force-pushed
the
fix-disallow-js-execution-crash
branch
from
April 5, 2026 00:40
65b2fa8 to
3d4c3a9
Compare
deepak1556
reviewed
Apr 5, 2026
deepak1556
left a comment
Member
There was a problem hiding this comment.
LGTM
Those destruction events are a problem even when we move the class to cppgc, will see how that goes.
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
force-pushed
the
fix-disallow-js-execution-crash
branch
from
April 5, 2026 05:46
3d4c3a9 to
f89c8ef
Compare
deepak1556
approved these changes
Apr 5, 2026
|
Release Notes Persisted
|
Contributor
|
I have automatically backported this PR to "40-x-y", please check out #50693 |
Contributor
|
I have automatically backported this PR to "41-x-y", please check out #50694 |
Contributor
|
I have automatically backported this PR to "42-x-y", please check out #50695 |
4 tasks done
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 Electrongin_helper::Wrappablesubclasses (WebContentsin particular) emit JS events from their destructors ('will-destroy'/'destroyed'), so whenSecondWeakCallbacksynchronouslydeletes the C++ object inside that scope, the destructor'sEmit()→node::MakeCallback→v8::Function::Callaborts with: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::SecondWeakCallbackandDeprecatedWrappableBase::SecondWeakCallbacknow post the deletion viabase::SequencedTaskRunner::GetCurrentDefault()->{DeleteSoon,PostTask}so the destructor (and anyEmit()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 onmainby settingv8.setFlagsFromString('--stress-incremental-marking')(which forces foreground GC tasks on every allocation step) and leaking threeWebContentsViewinstances beforeapp.quit(). Verified on a Linux testing build:All 23 existing crash-case fixtures continue to pass.
Checklist
npm run lintpassesspec/fixtures/crash-cases/wrappable-gc-weak-callback-emit)spec/fixtures/crash-cases/*passFixes #47420
Fixes #45416
Notes: Fixed an intermittent
Invoke in DisallowJavascriptExecutionScopecrash on application quit when aWebContents(or other JS-emitting native object) is garbage-collected during shutdown.