Repository navigation
fix: crash in gin::PerIsolateData::NotifyDisposed when a session is created during quit - #52836
Conversation
deepak1556
left a comment
There was a problem hiding this comment.
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();
}
|
@deepak1556 ty, that reads much better - reworked in e616882: |
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.
e616882 to
6e99a08
Compare
|
Release Notes Persisted
|
|
I was unable to backport this PR to "42-x-y" cleanly; |
|
I have automatically backported this PR to "44-x-y", please check out #52924 |
|
I have automatically backported this PR to "43-x-y", please check out #52925 |
…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]>
…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]>
|
@codebytere has manually backported this PR to "42-x-y", please check out #53132 |
…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)
Description of Change
Session,GlobalShortcutandAutoUpdaterregister asgin::PerIsolateData::DisposeObserverwhen constructed and unregister when they receiveOnBeforeMicrotasksRunnerDispose. That notification went out from gin'sWillDestroyMicrotasksRunner(), beforeCleanedUpAtExit::DoCleanup()tears down the remaining WebContents. JS that runs during that teardown, for example awebContentsdestroyedhandler, can still create one of these objects (even re-readingsession.defaultSessionbuilds a newSessionthere 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 insidev8::Isolate::Dispose(), and theNotifyDisposed()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 afterDoCleanup(), and the gin patch keeps only the "runner destroyed" flag inWillDestroyMicrotasksRunner(), 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 touchesglobalShortcutfrom adestroyedhandler during quit; it aborts on the CHECK without this change and exits cleanly with it.Checklist
npm testpassesRelease Notes
Notes: Fixed a possible main process crash at quit when a session was created from JavaScript that runs during shutdown.