Index: components/browser_context_keyed_service/browser_context_keyed_service_factory.cc |
diff --git a/components/browser_context_keyed_service/browser_context_keyed_service_factory.cc b/components/browser_context_keyed_service/browser_context_keyed_service_factory.cc |
index 8f2bc729f06b474421eb06b26e3e7322e8ff4e3d..c9d6646e03c279f6caa15fe0082324c6a4ca4cfb 100644 |
--- a/components/browser_context_keyed_service/browser_context_keyed_service_factory.cc |
+++ b/components/browser_context_keyed_service/browser_context_keyed_service_factory.cc |
@@ -20,6 +20,14 @@ void BrowserContextKeyedServiceFactory::SetTestingFactory( |
// destruction. |
bool add_context = ArePreferencesSetOn(context); |
+#ifndef NDEBUG |
+ // Ensure that |context| is not marked as stale (e.g., due to it aliasing an |
+ // instance that was destroyed in an earlier test) in order to avoid accesses |
+ // to |context| in |BrowserContextShutdown| from causing |
+ // |AssertBrowserContextWasntDestroyed| to raise an error. |
+ dependency_manager_->MarkBrowserContextLiveForTesting(context); |
+#endif |
+ |
// We have to go through the shutdown and destroy mechanisms because there |
// are unit tests that create a service on a context and then change the |
// testing service mid-test. |