bug 1314818 - Get and dispatch telemetry accumulation to the main thread directly. r=froydnj
authorChris H-C <chutten@mozilla.com>
Tue, 08 Nov 2016 14:39:42 -0500
changeset 351873 fcd1172e075a043e2ea47122cacece0299b9b2c8
parent 351872 0e9e97d11bbb81e0f576e4e92927500831e1d650
child 351874 a66aa3e7dd91cd9f3c3333bc186bc177a0c05cf5
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1314818
milestone52.0a1
bug 1314818 - Get and dispatch telemetry accumulation to the main thread directly. r=froydnj NS_DispatchToMainThread can fail catastrophically during shutdown. Simply listening for shutdown isn't enough to prevent catastrophe. So directly fetch the main thread and, if you can't, then don't try to use it. MozReview-Commit-ID: 7iFQEqqQekB
toolkit/components/telemetry/TelemetryHistogram.cpp
--- a/toolkit/components/telemetry/TelemetryHistogram.cpp
+++ b/toolkit/components/telemetry/TelemetryHistogram.cpp
@@ -345,16 +345,28 @@ StringEndsWith(const std::string& name, 
 {
   if (name.size() < suffix.size()) {
     return false;
   }
 
   return name.compare(name.size() - suffix.size(), suffix.size(), suffix) == 0;
 }
 
+void internal_DispatchToMainThread(already_AddRefed<nsIRunnable>&& aEvent)
+{
+  nsCOMPtr<nsIRunnable> event(aEvent);
+  nsCOMPtr<nsIThread> thread;
+  nsresult rv = NS_GetMainThread(getter_AddRefs(thread));
+  if (NS_FAILED(rv)) {
+    NS_WARNING("NS_FAILED DispatchToMainThread. Maybe we're shutting down?");
+    return;
+  }
+  thread->Dispatch(event, 0);
+}
+
 } // namespace
 
 
 ////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////
 //
 // PRIVATE: Histogram Get, Add, Clone, Clear functions
 
@@ -1350,17 +1362,17 @@ void internal_armIPCTimer()
 {
   if (gIPCTimerArmed || gIPCTimerArming) {
     return;
   }
   gIPCTimerArming = true;
   if (NS_IsMainThread()) {
     internal_armIPCTimerMainThread();
   } else {
-    NS_DispatchToMainThread(NS_NewRunnableFunction([]() -> void {
+    internal_DispatchToMainThread(NS_NewRunnableFunction([]() -> void {
       StaticMutexAutoLock locker(gTelemetryHistogramMutex);
       internal_armIPCTimerMainThread();
     }));
   }
 }
 
 bool
 internal_RemoteAccumulate(mozilla::Telemetry::ID aId, uint32_t aSample)
@@ -1372,17 +1384,17 @@ internal_RemoteAccumulate(mozilla::Telem
   nsresult rv = internal_GetHistogramByEnumId(aId, &h, GeckoProcessType_Default);
   if (NS_SUCCEEDED(rv) && !h->IsRecordingEnabled()) {
     return true;
   }
   if (!gAccumulations) {
     gAccumulations = new nsTArray<Accumulation>();
   }
   if (gAccumulations->Length() == kAccumulationsArrayHighWaterMark) {
-    NS_DispatchToMainThread(NS_NewRunnableFunction([]() -> void {
+    internal_DispatchToMainThread(NS_NewRunnableFunction([]() -> void {
       TelemetryHistogram::IPCTimerFired(nullptr, nullptr);
     }));
   }
   gAccumulations->AppendElement(Accumulation{aId, aSample});
   internal_armIPCTimer();
   return true;
 }
 
@@ -1399,17 +1411,17 @@ internal_RemoteAccumulate(mozilla::Telem
   MOZ_ASSERT(keyed);
   if (!keyed->IsRecordingEnabled()) {
     return false;
   }
   if (!gKeyedAccumulations) {
     gKeyedAccumulations = new nsTArray<KeyedAccumulation>();
   }
   if (gKeyedAccumulations->Length() == kAccumulationsArrayHighWaterMark) {
-    NS_DispatchToMainThread(NS_NewRunnableFunction([]() -> void {
+    internal_DispatchToMainThread(NS_NewRunnableFunction([]() -> void {
       TelemetryHistogram::IPCTimerFired(nullptr, nullptr);
     }));
   }
   gKeyedAccumulations->AppendElement(KeyedAccumulation{aId, aSample, aKey});
   internal_armIPCTimer();
   return true;
 }