Bug 1527173 - [autoconfig] Fix race condition in addOneFinishedObserver. r=Neil a=jorgk
authorBen Bucksch <ben.bucksch@beonex.com>
Wed, 13 Feb 2019 12:43:54 +0100
changeset 34349 bf09891616a4
parent 34348 3a5638b42d2d
child 34350 ad9ab2307818
push id389
push userclokep@gmail.com
push dateMon, 18 Mar 2019 19:01:53 +0000
reviewersNeil, jorgk
bugs1527173
Bug 1527173 - [autoconfig] Fix race condition in addOneFinishedObserver. r=Neil a=jorgk
mail/components/accountcreation/content/util.js
--- a/mail/components/accountcreation/content/util.js
+++ b/mail/components/accountcreation/content/util.js
@@ -373,54 +373,49 @@ ParallelCall.prototype = {
  *     {Array of Exception} allErrors - The exceptions from all calls.
  */
 function PriorityOrderAbortable(successCallback, errorCallback) {
   assert(typeof(successCallback) == "function");
   assert(typeof(errorCallback) == "function");
   ParallelAbortable.call(this); // call super constructor
 
   this.addOneFinishedObserver(finishedCall => {
-    let haveHigherPending = false;
     let haveHigherSuccess = false;
     for (let call of this._calls) {
       if (!call.finished) {
         if (haveHigherSuccess) {
           // abort
           if (call.callerAbortable) {
             call.callerAbortable.cancel(NoLongerNeededException("Another higher call succeeded"));
           }
           continue;
         }
-        // it's pending. ignore it for now and wait.
-        haveHigherPending = true;
-        continue;
+        // It's pending. do nothing and wait for it.
+        return;
       }
       if (!call.succeeded) {
         // it failed. ignore it.
         continue;
       }
       if (haveHigherSuccess) {
         // another successful call was higher. ignore it.
         continue;
       }
-      haveHigherSuccess = true;
-      if (!haveHigherPending) {
-        // this is the winner
-        try {
-          successCallback(call.result, call);
-        } catch (e) {
-          console.error(e);
-          // if the handler failed with this data, treat this call as failed
-          call.e = e;
-          call.succeeded = false;
-          haveHigherSuccess = false;
-        }
+      // This is the winner.
+      try {
+        successCallback(call.result, call);
+        haveHigherSuccess = true;
+      } catch (e) {
+        console.error(e);
+        // If the handler failed with this data, treat this call as failed.
+        call.e = e;
+        call.succeeded = false;
       }
     }
-    if (!haveHigherPending && !haveHigherSuccess) {
+    if (!haveHigherSuccess) {
       // all failed
       errorCallback(this._calls[0].e, this._calls.map(call => call.e)); // see docs above
     }
   });
 }
 PriorityOrderAbortable.prototype = Object.create(ParallelAbortable.prototype);
 PriorityOrderAbortable.prototype.constructor = PriorityOrderAbortable;