Bug 1149842 - Release the mutex for NS_OpenAnonymousTemporaryFile to prevent the deadlock. r=roc, a=jocheng
authorBenjamin Chen <bechen@mozilla.com>
Fri, 15 May 2015 17:52:45 -0400
changeset 238442 06873707c80b8025ddec6a7ba3ed14fe5036eac7
parent 238441 e0c74bd3e489aec570a0581996b480f47e1128c4
child 238443 c7182dcaee4277f0ae0ce30a2863c16cc38c865e
push id624
push userryanvm@gmail.com
push dateThu, 21 May 2015 01:28:21 +0000
treeherdermozilla-b2g37_v2_2@6e4eaf59efda [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc, jocheng
bugs1149842
milestone37.0
Bug 1149842 - Release the mutex for NS_OpenAnonymousTemporaryFile to prevent the deadlock. r=roc, a=jocheng
dom/media/EncodedBufferCache.cpp
--- a/dom/media/EncodedBufferCache.cpp
+++ b/dom/media/EncodedBufferCache.cpp
@@ -15,19 +15,34 @@ void
 EncodedBufferCache::AppendBuffer(nsTArray<uint8_t> & aBuf)
 {
   MutexAutoLock lock(mMutex);
   mDataSize += aBuf.Length();
 
   mEncodedBuffers.AppendElement()->SwapElements(aBuf);
 
   if (!mTempFileEnabled && mDataSize > mMaxMemoryStorage) {
-    nsresult rv = NS_OpenAnonymousTemporaryFile(&mFD);
+    nsresult rv;
+    PRFileDesc* tempFD = nullptr;
+    {
+      // Release the mMutex because there is a sync dispatch to mainthread in
+      // NS_OpenAnonymousTemporaryFile.
+      MutexAutoUnlock unlock(mMutex);
+      rv = NS_OpenAnonymousTemporaryFile(&tempFD);
+    }
     if (!NS_FAILED(rv)) {
-      mTempFileEnabled = true;
+      // Check the mDataSize again since we release the mMutex before.
+      if (mDataSize > mMaxMemoryStorage) {
+        mFD = tempFD;
+        mTempFileEnabled = true;
+      } else {
+        // Close the tempFD because the data had been taken during the
+        // MutexAutoUnlock.
+        PR_Close(tempFD);
+      }
     }
   }
 
   if (mTempFileEnabled) {
     // has created temporary file, write buffer in it
     for (uint32_t i = 0; i < mEncodedBuffers.Length(); i++) {
       int32_t amount = PR_Write(mFD, mEncodedBuffers.ElementAt(i).Elements(), mEncodedBuffers.ElementAt(i).Length());
       if (amount < 0 || size_t(amount) < mEncodedBuffers.ElementAt(i).Length()) {