diff --git a/chrome/browser/ui/web_applications/tabbed_web_app_navigation_throttle.cc b/chrome/browser/ui/web_applications/tabbed_web_app_navigation_throttle.cc index 5d6ba6d779be9..4d8521ec004ab 100644 --- a/chrome/browser/ui/web_applications/tabbed_web_app_navigation_throttle.cc +++ b/chrome/browser/ui/web_applications/tabbed_web_app_navigation_throttle.cc @@ -4,6 +4,8 @@ #include "chrome/browser/ui/web_applications/tabbed_web_app_navigation_throttle.h" +#include "base/functional/bind.h" +#include "base/task/sequenced_task_runner.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser_window/public/browser_window_interface.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" @@ -137,7 +139,18 @@ TabbedWebAppNavigationThrottle::WillStartRequest() { if (browser_window->GetTabStripModel()->count() > 1 && !web_contents->GetLastCommittedURL().is_valid()) { DVLOG(1) << "TabbedWebAppNavigationThrottle: Closing blank tab"; - web_contents->ClosePage(); + // ClosePage() can synchronously destroy `web_contents` (e.g. when the + // primary main frame's renderer is not live or the page is otherwise + // ready to be closed), which in turn destroys this throttle. Per the + // NavigationThrottle contract, defer the destruction. + base::SequencedTaskRunner::GetCurrentDefault()->PostTask( + FROM_HERE, base::BindOnce( + [](base::WeakPtr contents) { + if (contents) { + contents->ClosePage(); + } + }, + web_contents->GetWeakPtr())); } DVLOG(1) << "TabbedWebAppNavigationThrottle: Redirecting to FocusHomeTab"; return FocusHomeTab(*app_controller, *browser_window->GetTabStripModel());