Bug 1048500 - Wait longer to get native stack for ANR; r=blassey
authorJim Chen <nchen@mozilla.com>
Tue, 05 Aug 2014 12:41:10 -0400
changeset 197958 392cc38c7981f6b1fd553e308bf33e17ed9e87e5
parent 197957 e2dc50b68014db43c4d7d837a829f69fc3ba47f4
child 197959 0a317ec96651b5a1a11c6907fbf1e42531f54fa7
push id47257
push usernchen@mozilla.com
push dateTue, 05 Aug 2014 16:42:11 +0000
treeherdermozilla-inbound@392cc38c7981 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey
bugs1048500
milestone34.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1048500 - Wait longer to get native stack for ANR; r=blassey
widget/android/AndroidJNI.cpp
--- a/widget/android/AndroidJNI.cpp
+++ b/widget/android/AndroidJNI.cpp
@@ -25,16 +25,17 @@
 #include "nsThreadUtils.h"
 
 #ifdef MOZ_CRASHREPORTER
 #include "nsICrashReporter.h"
 #include "nsExceptionHandler.h"
 #endif
 
 #include "mozilla/unused.h"
+#include "mozilla/UniquePtr.h"
 
 #include "mozilla/dom/SmsMessage.h"
 #include "mozilla/dom/mobilemessage/Constants.h"
 #include "mozilla/dom/mobilemessage/Types.h"
 #include "mozilla/dom/mobilemessage/PSms.h"
 #include "mozilla/dom/mobilemessage/SmsParent.h"
 #include "mozilla/layers/APZCTreeManager.h"
 #include "nsIMobileMessageDatabaseService.h"
@@ -980,28 +981,40 @@ Java_org_mozilla_gecko_ANRReporter_reque
 
 NS_EXPORT jstring JNICALL
 Java_org_mozilla_gecko_ANRReporter_getNativeStack(JNIEnv* jenv, jclass)
 {
     if (!profiler_is_active()) {
         // Maybe profiler support is disabled?
         return nullptr;
     }
-    char *profile = profiler_get_profile();
-    while (profile && !strlen(profile)) {
+
+    // Timeout if we don't get a profiler sample after 5 seconds.
+    const PRIntervalTime timeout = PR_SecondsToInterval(5);
+    const PRIntervalTime startTime = PR_IntervalNow();
+
+    typedef struct { void operator()(void* p) { free(p); } } ProfilePtrPolicy;
+    // Pointer to a profile JSON string
+    typedef mozilla::UniquePtr<char, ProfilePtrPolicy> ProfilePtr;
+
+    ProfilePtr profile(profiler_get_profile());
+
+    while (profile && !strstr(profile.get(), "\"samples\":[{")) {
         // no sample yet?
+        if (PR_IntervalNow() - startTime >= timeout) {
+            return nullptr;
+        }
         sched_yield();
-        profile = profiler_get_profile();
+        profile = ProfilePtr(profiler_get_profile());
     }
-    jstring result = nullptr;
+
     if (profile) {
-        result = jenv->NewStringUTF(profile);
-        free(profile);
+        return jenv->NewStringUTF(profile.get());
     }
-    return result;
+    return nullptr;
 }
 
 NS_EXPORT void JNICALL
 Java_org_mozilla_gecko_ANRReporter_releaseNativeStack(JNIEnv* jenv, jclass)
 {
     if (!profiler_is_active()) {
         // Maybe profiler support is disabled?
         return;