Skip to content

fix: crash in gin::PerIsolateData::NotifyDisposed when a session is created during quit - #52836

Merged
codebytere merged 2 commits into
mainfrom
fix-gin-dispose-observer-at-quit
Aug 17, 2026
Merged

codebytere merged 2 commits into
mainfrom
fix-gin-dispose-observer-at-quit

Conversation

@codebytere

@codebytere codebytere commented Aug 16, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Session, GlobalShortcut and AutoUpdater register as gin::PerIsolateData::DisposeObserver when constructed and unregister when they receive OnBeforeMicrotasksRunnerDispose. That notification went out from gin's WillDestroyMicrotasksRunner(), before CleanedUpAtExit::DoCleanup() tears down the remaining WebContents. JS that runs during that teardown, for example a webContents destroyed handler, can still create one of these objects (even re-reading session.defaultSession builds a new Session there because the weak cell was already invalidated). Such an object never got the notification, stayed in the observer list, was finalized with the cppgc heap inside v8::Isolate::Dispose(), and the NotifyDisposed() pass right after walked the freed observer and hit its CHECK, crashing the browser process at exit (gin::PerIsolateData::NotifyDisposed <- gin::IsolateHolder::~IsolateHolder <- JavascriptEnvironment::~JavascriptEnvironment).

JavascriptEnvironment::DestroyMicrotasksRunner() now sends the notification itself after DoCleanup(), and the gin patch keeps only the "runner destroyed" flag in WillDestroyMicrotasksRunner(), so observers created during cleanup are disposed and unregistered like the rest while the browser contexts are still alive. Adds a crash case that creates a session and touches globalShortcut from a destroyed handler during quit; it aborts on the CHECK without this change and exits cleanly with it.

Checklist

  • PR description included and stakeholders cc'd
  • npm test passes
  • tests are changed or added
  • PR release notes describe the change in a way relevant to app developers

Release Notes

Notes: Fixed a possible main process crash at quit when a session was created from JavaScript that runs during shutdown.

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 16, 2026
@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 16, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Aug 17, 2026

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

Thanks for the test case, the proposed fix will avoid native resource cleanup for the new objects created after OnBeforeMicrotasksRunnerDispose. We can address this lifetime ordering by ensuring all legacy wrappers are deleted first before the notification is passed. (i-e)


// shell

void JavascriptEnvironment::DestroyMicrotasksRunner() {
  DCHECK(microtasks_runner_);
  // Should be called before running gin_helper::CleanedUpAtExit::DoCleanup.
  // This helps to signal wrappable finalizer callbacks to not act on freed
  // parameters.
  isolate_holder_->MarkMicrotasksRunnerDestroyed();
  {
    v8::HandleScope scope{isolate()};
    gin_helper::CleanedUpAtExit::DoCleanup();
  }
  isolate_holder_->NotifyBeforeMicrotasksRunnerDispose();
  base::CurrentThread::Get()->RemoveTaskObserver(microtasks_runner_.get());
  microtasks_runner_.reset();
}

// gin

void IsolateHolder::MarkMicrotasksRunnerDestroyed() {
  DCHECK(g_initialized_microtasks_runner);
  g_destroyed_microtasks_runner = true;
}

void IsolateHolder::NotifyBeforeMicrotasksRunnerDispose() {
  isolate_data_->NotifyBeforeMicrotasksRunnerDispose();
}

@codebytere
codebytere requested a review from a team as a code owner August 17, 2026 12:28
@codebytere

Copy link
Copy Markdown
Member Author

@deepak1556 ty, that reads much better - reworked in e616882: WillDestroyMicrotasksRunner() only sets the flag now and DestroyMicrotasksRunner() notifies after DoCleanup(), so the ctor/dtor changes are gone again. i called PerIsolateData::NotifyBeforeMicrotasksRunnerDispose() directly rather than adding an IsolateHolder wrapper to keep the gin patch a line smaller, lmk if you'd prefer the wrapper.

@codebytere
codebytere requested a review from deepak1556 August 17, 2026 12:29

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

Thanks!

@codebytere
codebytere enabled auto-merge (squash) August 17, 2026 16:16
Session, GlobalShortcut and AutoUpdater add themselves as
gin::PerIsolateData::DisposeObserver on construction but only removed
themselves from inside OnBeforeMicrotasksRunnerDispose. An instance
created after that notification has run (for example from a
webContents 'destroyed' handler that fires during exit cleanup) stayed
in the observer list, was finalized together with the cppgc heap inside
v8::Isolate::Dispose(), and the NotifyDisposed() pass that follows then
walked a freed CheckedObserver and hit its CHECK.

Remove the observer in the destructor instead, which covers both the
notified and the late-created case, and make Session::Dispose() key on
browser_context_ rather than keep_alive_ (persistents are already
cleared when finalizers run at heap teardown) so a late session also
drops its DownloadManager observer before the browser context goes away.
Send OnBeforeMicrotasksRunnerDispose from
JavascriptEnvironment::DestroyMicrotasksRunner() once DoCleanup() has
run, instead of from gin's WillDestroyMicrotasksRunner() before it, so
that observers created by JS running during cleanup are notified and
unregister themselves like the rest. This replaces unregistering
Session, GlobalShortcut and AutoUpdater in their destructors.
@codebytere
codebytere force-pushed the fix-gin-dispose-observer-at-quit branch from e616882 to 6e99a08 Compare August 17, 2026 20:05
@codebytere
codebytere merged commit ceec345 into main Aug 17, 2026
78 checks passed
@codebytere
codebytere deleted the fix-gin-dispose-observer-at-quit branch August 17, 2026 21:42
@release-clerk

release-clerk Bot commented Aug 17, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed a possible main process crash at quit when a session was created from JavaScript that runs during shutdown.

@trop

trop Bot commented Aug 17, 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 17, 2026

Copy link
Copy Markdown
Contributor

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

@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot removed the target/44-x-y PR should also be added to the "44-x-y" branch. label Aug 17, 2026
@trop trop Bot added in-flight/43-x-y and removed target/43-x-y PR should also be added to the "43-x-y" branch. labels Aug 17, 2026
MarshallOfSound pushed a commit that referenced this pull request Aug 18, 2026
…reated during quit (#52925)

fix: crash in gin::PerIsolateData::NotifyDisposed when a session is created during quit (#52836)

* fix: unregister isolate dispose observers in their destructors

Session, GlobalShortcut and AutoUpdater add themselves as
gin::PerIsolateData::DisposeObserver on construction but only removed
themselves from inside OnBeforeMicrotasksRunnerDispose. An instance
created after that notification has run (for example from a
webContents 'destroyed' handler that fires during exit cleanup) stayed
in the observer list, was finalized together with the cppgc heap inside
v8::Isolate::Dispose(), and the NotifyDisposed() pass that follows then
walked a freed CheckedObserver and hit its CHECK.

Remove the observer in the destructor instead, which covers both the
notified and the late-created case, and make Session::Dispose() key on
browser_context_ rather than keep_alive_ (persistents are already
cleared when finalizers run at heap teardown) so a late session also
drops its DownloadManager observer before the browser context goes away.

* fix: notify isolate dispose observers after CleanedUpAtExit cleanup

Send OnBeforeMicrotasksRunnerDispose from
JavascriptEnvironment::DestroyMicrotasksRunner() once DoCleanup() has
run, instead of from gin's WillDestroyMicrotasksRunner() before it, so
that observers created by JS running during cleanup are notified and
unregister themselves like the rest. This replaces unregistering
Session, GlobalShortcut and AutoUpdater in their destructors.

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <[email protected]>
@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 18, 2026
MarshallOfSound pushed a commit that referenced this pull request Aug 18, 2026
…reated during quit (#52924)

fix: crash in gin::PerIsolateData::NotifyDisposed when a session is created during quit (#52836)

* fix: unregister isolate dispose observers in their destructors

Session, GlobalShortcut and AutoUpdater add themselves as
gin::PerIsolateData::DisposeObserver on construction but only removed
themselves from inside OnBeforeMicrotasksRunnerDispose. An instance
created after that notification has run (for example from a
webContents 'destroyed' handler that fires during exit cleanup) stayed
in the observer list, was finalized together with the cppgc heap inside
v8::Isolate::Dispose(), and the NotifyDisposed() pass that follows then
walked a freed CheckedObserver and hit its CHECK.

Remove the observer in the destructor instead, which covers both the
notified and the late-created case, and make Session::Dispose() key on
browser_context_ rather than keep_alive_ (persistents are already
cleared when finalizers run at heap teardown) so a late session also
drops its DownloadManager observer before the browser context goes away.

* fix: notify isolate dispose observers after CleanedUpAtExit cleanup

Send OnBeforeMicrotasksRunnerDispose from
JavascriptEnvironment::DestroyMicrotasksRunner() once DoCleanup() has
run, instead of from gin's WillDestroyMicrotasksRunner() before it, so
that observers created by JS running during cleanup are notified and
unregister themselves like the rest. This replaces unregistering
Session, GlobalShortcut and AutoUpdater in their destructors.

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <[email protected]>
@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 18, 2026
@trop

trop Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

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

codebytere added a commit that referenced this pull request Aug 23, 2026
…reated during quit (#53132)

fix: crash in gin::PerIsolateData::NotifyDisposed when a session is created during quit (#52836)

* fix: unregister isolate dispose observers in their destructors

Session, GlobalShortcut and AutoUpdater add themselves as
gin::PerIsolateData::DisposeObserver on construction but only removed
themselves from inside OnBeforeMicrotasksRunnerDispose. An instance
created after that notification has run (for example from a
webContents 'destroyed' handler that fires during exit cleanup) stayed
in the observer list, was finalized together with the cppgc heap inside
v8::Isolate::Dispose(), and the NotifyDisposed() pass that follows then
walked a freed CheckedObserver and hit its CHECK.

Remove the observer in the destructor instead, which covers both the
notified and the late-created case, and make Session::Dispose() key on
browser_context_ rather than keep_alive_ (persistents are already
cleared when finalizers run at heap teardown) so a late session also
drops its DownloadManager observer before the browser context goes away.

* fix: notify isolate dispose observers after CleanedUpAtExit cleanup

Send OnBeforeMicrotasksRunnerDispose from
JavascriptEnvironment::DestroyMicrotasksRunner() once DoCleanup() has
run, instead of from gin's WillDestroyMicrotasksRunner() before it, so
that observers created by JS running during cleanup are notified and
unregister themselves like the rest. This replaces unregistering
Session, GlobalShortcut and AutoUpdater in their destructors.

(cherry picked from commit ceec345)
@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 23, 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

Development

Successfully merging this pull request may close these issues.

3 participants