Bug 1268559 - Wait to finish nested event loops before shutdown. r=bsmedberg, a=gchang
authorBill McCloskey <billm@mozilla.com>
Fri, 03 Jun 2016 17:22:08 -0700
changeset 339649 0cb189f7380fcdf025f30601b9eb825a8f670213
parent 339648 9f182283211dbebccb57bbc9f4c8b9f7be8b91a0
child 339650 8d0231d59d0f4ecb7ccd25e3795537b816a92c8c
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg, gchang
bugs1268559
milestone49.0a2
Bug 1268559 - Wait to finish nested event loops before shutdown. r=bsmedberg, a=gchang
dom/ipc/ContentChild.cpp
--- a/dom/ipc/ContentChild.cpp
+++ b/dom/ipc/ContentChild.cpp
@@ -3070,16 +3070,34 @@ ContentChild::StartForceKillTimer()
 ContentChild::ForceKillTimerCallback(nsITimer* aTimer, void* aClosure)
 {
   ProcessChild::QuickExit();
 }
 
 bool
 ContentChild::RecvShutdown()
 {
+  // If we receive the shutdown message from within a nested event loop, we want
+  // to wait for that event loop to finish. Otherwise we could prematurely
+  // terminate an "unload" or "pagehide" event handler (which might be doing a
+  // sync XHR, for example).
+  nsCOMPtr<nsIThread> thread;
+  nsresult rv = NS_GetMainThread(getter_AddRefs(thread));
+  if (NS_SUCCEEDED(rv) && thread) {
+    RefPtr<nsThread> mainThread(thread.forget().downcast<nsThread>());
+    if (mainThread->RecursionDepth() > 1) {
+      // We're in a nested event loop. Let's delay for an arbitrary period of
+      // time (100ms) in the hopes that the event loop will have finished by
+      // then.
+      MessageLoop::current()->PostDelayedTask(
+        NewRunnableMethod(this, &ContentChild::RecvShutdown), 100);
+      return true;
+    }
+  }
+
   if (mPolicy) {
     mPolicy->Deactivate();
     mPolicy = nullptr;
   }
 
   nsCOMPtr<nsIObserverService> os = services::GetObserverService();
   if (os) {
     os->NotifyObservers(static_cast<nsIContentChild*>(this),