Bug 1149842 - Release the mutex for NS_OpenAnonymousTemporaryFile to prevent the deadlock. r=roc, a=sledru
authorBenjamin Chen <bechen@mozilla.com>
Fri, 15 May 2015 17:52:45 -0400
changeset 260533 06bdddc6463d
parent 260532 daa1f205525a
child 260534 cda517b321ee
push id810
push userryanvm@gmail.com
push date2015-05-19 21:14 +0000
treeherdermozilla-release@06bdddc6463d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc, sledru
bugs1149842
milestone38.0.5
Bug 1149842 - Release the mutex for NS_OpenAnonymousTemporaryFile to prevent the deadlock. r=roc, a=sledru
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()) {