Bug 1130066 - Be careful about environment changes during early or failed BrowserHealthRecorder init. r=mcomella, a=ritu
authorRichard Newman <rnewman@mozilla.com>
Tue, 21 Jul 2015 13:26:34 -0700
changeset 269013 fb522d1a01a844f267cbaa7a446a99fa523d11c2
parent 269012 8ab54987779cfc265ce7f04848cc751318c1de81
child 269014 85643cc8b5610a6a1db49ed24413f6d466eedee1
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-esr52@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella, ritu
bugs1130066
milestone41.0a2
Bug 1130066 - Be careful about environment changes during early or failed BrowserHealthRecorder init. r=mcomella, a=ritu
mobile/android/base/health/BrowserHealthRecorder.java
--- a/mobile/android/base/health/BrowserHealthRecorder.java
+++ b/mobile/android/base/health/BrowserHealthRecorder.java
@@ -258,16 +258,22 @@ public class BrowserHealthRecorder imple
 
     /**
      * If `startNewSession` is false, it means no new session should begin
      * (e.g., because we're about to restart, and we don't want to create
      * an orphan).
      */
     @Override
     public synchronized void onEnvironmentChanged(final boolean startNewSession, final String sessionEndReason) {
+        if (!(state == State.INITIALIZING ||
+              state == State.INITIALIZED)) {
+            Log.w(LOG_TAG, "Not initialized. Ignoring environment change. State is " + state);
+            return;
+        }
+
         final int previousEnv = this.env;
         this.env = -1;
         try {
             profileCache.completeInitialization();
         } catch (java.io.IOException e) {
             Log.e(LOG_TAG, "Error completing profile cache initialization.", e);
             this.state = State.INITIALIZATION_FAILED;
             return;
@@ -290,17 +296,17 @@ public class BrowserHealthRecorder imple
                 }
             }
         });
     }
 
     protected synchronized int ensureEnvironment() {
         if (!(state == State.INITIALIZING ||
               state == State.INITIALIZED)) {
-            throw new IllegalStateException("Not initialized.");
+            throw new IllegalStateException("Not initialized. State is " + state);
         }
 
         if (this.env != -1) {
             return this.env;
         }
         if (this.storage == null) {
             // Oh well.
             return -1;
@@ -561,20 +567,28 @@ public class BrowserHealthRecorder imple
                 if (desc != null && desc.valid) {
                     profileCache.setDistributionString(desc.id, desc.version);
                 }
 
                 // Now rebuild.
                 try {
                     profileCache.completeInitialization();
 
-                    if (state == State.INITIALIZING) {
-                        initializeStorage();
-                    } else {
-                        onEnvironmentChanged();
+                    synchronized (BrowserHealthRecorder.this) {
+                        switch (state) {
+                            case INITIALIZING:
+                                initializeStorage();
+                                break;
+                            case INITIALIZED:
+                                onEnvironmentChanged();
+                                break;
+                            default:
+                                Log.e(LOG_TAG, "Unexpected state " + state + " when handling late distribution.");
+                                return;
+                        }
                     }
                 } catch (Exception e) {
                     // Well, we tried.
                     Log.e(LOG_TAG, "Couldn't complete profile cache init.", e);
                 }
             }
         });
     }