[tbb-commits] [tor-browser] 13/15: Bug 1770630 - Worker stream readers should contribute to busy count. r=jstutte, a=RyanVM

gitolite role git at cupani.torproject.org
Thu Aug 18 22:31:55 UTC 2022


This is an automated email from the git hooks/post-receive script.

richard pushed a commit to branch tor-browser-91.13.0esr-11.5-1
in repository tor-browser.

commit 25d82a1d4c4353e01a6c7936ef8c8683cbbecabf
Author: Andrew Sutherland <asutherland at asutherland.org>
AuthorDate: Tue Aug 9 10:20:09 2022 -0400

    Bug 1770630 - Worker stream readers should contribute to busy count. r=jstutte, a=RyanVM
---
 dom/base/BodyStream.cpp         |  9 +++++---
 dom/base/BodyStream.h           |  5 +++--
 dom/fetch/FetchStreamReader.cpp | 14 ++++++-------
 dom/fetch/FetchStreamReader.h   |  5 +++--
 dom/fetch/FetchUtil.cpp         |  5 ++---
 dom/file/FileReader.cpp         | 46 ++++++++++-------------------------------
 dom/file/FileReader.h           |  1 +
 7 files changed, 33 insertions(+), 52 deletions(-)

diff --git a/dom/base/BodyStream.cpp b/dom/base/BodyStream.cpp
index 05ab478a5b23f..50781bba29583 100644
--- a/dom/base/BodyStream.cpp
+++ b/dom/base/BodyStream.cpp
@@ -115,8 +115,8 @@ void BodyStream::Create(JSContext* aCx, BodyStreamHolder* aStreamHolder,
     WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
     MOZ_ASSERT(workerPrivate);
 
-    RefPtr<WeakWorkerRef> workerRef =
-        WeakWorkerRef::Create(workerPrivate, [stream]() { stream->Close(); });
+    RefPtr<StrongWorkerRef> workerRef =
+        StrongWorkerRef::Create(workerPrivate, "BodyStream", [stream]() { stream->Close(); });
 
     if (NS_WARN_IF(!workerRef)) {
       aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
@@ -215,6 +215,7 @@ void BodyStream::requestData(JSContext* aCx, JS::HandleObject aStream,
     ErrorPropagation(aCx, lock, aStream, rv);
     return;
   }
+  mAsyncWaitWorkerRef = mWorkerRef;
 
   // All good.
 }
@@ -254,6 +255,7 @@ void BodyStream::writeIntoReadRequestBuffer(JSContext* aCx,
     ErrorPropagation(aCx, lock, aStream, rv);
     return;
   }
+  mAsyncWaitWorkerRef = mWorkerRef;
 
   // All good.
 }
@@ -362,6 +364,7 @@ NS_IMETHODIMP
 BodyStream::OnInputStreamReady(nsIAsyncInputStream* aStream) {
   AssertIsOnOwningThread();
   MOZ_DIAGNOSTIC_ASSERT(aStream);
+  mAsyncWaitWorkerRef = nullptr;
 
   // Acquire |mMutex| in order to safely inspect |mState| and use |mGlobal|.
   Maybe<MutexAutoLock> lock;
@@ -511,7 +514,7 @@ void BodyStream::ReleaseObjects(const MutexAutoLock& aProofOfLock) {
     // Let's dispatch a WorkerControlRunnable if the owning thread is a worker.
     if (mWorkerRef) {
       RefPtr<WorkerShutdown> r =
-          new WorkerShutdown(mWorkerRef->GetUnsafePrivate(), this);
+          new WorkerShutdown(mWorkerRef->Private(), this);
       Unused << NS_WARN_IF(!r->Dispatch());
       return;
     }
diff --git a/dom/base/BodyStream.h b/dom/base/BodyStream.h
index 33b1166006d86..05c418d1a189f 100644
--- a/dom/base/BodyStream.h
+++ b/dom/base/BodyStream.h
@@ -27,7 +27,7 @@ class ErrorResult;
 namespace dom {
 
 class BodyStream;
-class WeakWorkerRef;
+class StrongWorkerRef;
 
 class BodyStreamHolder : public nsISupports {
   friend class BodyStream;
@@ -168,7 +168,8 @@ class BodyStream final : public nsIInputStreamCallback,
   nsCOMPtr<nsIInputStream> mOriginalInputStream;
   nsCOMPtr<nsIAsyncInputStream> mInputStream;
 
-  RefPtr<WeakWorkerRef> mWorkerRef;
+  RefPtr<StrongWorkerRef> mWorkerRef;
+  RefPtr<StrongWorkerRef> mAsyncWaitWorkerRef;
 };
 
 }  // namespace dom
diff --git a/dom/fetch/FetchStreamReader.cpp b/dom/fetch/FetchStreamReader.cpp
index 7e733b91d7243..ded9706d8a665 100644
--- a/dom/fetch/FetchStreamReader.cpp
+++ b/dom/fetch/FetchStreamReader.cpp
@@ -71,16 +71,14 @@ nsresult FetchStreamReader::Create(JSContext* aCx, nsIGlobalObject* aGlobal,
     WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
     MOZ_ASSERT(workerPrivate);
 
-    RefPtr<WeakWorkerRef> workerRef =
-        WeakWorkerRef::Create(workerPrivate, [streamReader]() {
+    RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
+        workerPrivate, "FetchStreamReader", [streamReader]() {
           MOZ_ASSERT(streamReader);
           MOZ_ASSERT(streamReader->mWorkerRef);
 
-          WorkerPrivate* workerPrivate = streamReader->mWorkerRef->GetPrivate();
-          MOZ_ASSERT(workerPrivate);
-
-          streamReader->CloseAndRelease(workerPrivate->GetJSContext(),
-                                        NS_ERROR_DOM_INVALID_STATE_ERR);
+          streamReader->CloseAndRelease(
+              streamReader->mWorkerRef->Private()->GetJSContext(),
+              NS_ERROR_DOM_INVALID_STATE_ERR);
         });
 
     if (NS_WARN_IF(!workerRef)) {
@@ -191,6 +189,7 @@ void FetchStreamReader::StartConsuming(JSContext* aCx, JS::HandleObject aStream,
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
+  mAsyncWaitWorkerRef = mWorkerRef;
 }
 
 // nsIOutputStreamCallback interface
@@ -201,6 +200,7 @@ FetchStreamReader::OnOutputStreamReady(nsIAsyncOutputStream* aStream) {
   MOZ_ASSERT(aStream == mPipeOut);
   MOZ_ASSERT(mReader);
 
+  mAsyncWaitWorkerRef = nullptr;
   if (mStreamClosed) {
     return NS_OK;
   }
diff --git a/dom/fetch/FetchStreamReader.h b/dom/fetch/FetchStreamReader.h
index 6b28136f8f551..80f1be2f056fd 100644
--- a/dom/fetch/FetchStreamReader.h
+++ b/dom/fetch/FetchStreamReader.h
@@ -17,7 +17,7 @@
 namespace mozilla {
 namespace dom {
 
-class WeakWorkerRef;
+class StrongWorkerRef;
 
 class FetchStreamReader final : public nsIOutputStreamCallback,
                                 public PromiseNativeHandler {
@@ -59,7 +59,8 @@ class FetchStreamReader final : public nsIOutputStreamCallback,
 
   nsCOMPtr<nsIAsyncOutputStream> mPipeOut;
 
-  RefPtr<WeakWorkerRef> mWorkerRef;
+  RefPtr<StrongWorkerRef> mWorkerRef;
+  RefPtr<StrongWorkerRef> mAsyncWaitWorkerRef;
 
   JS::Heap<JSObject*> mReader;
 
diff --git a/dom/fetch/FetchUtil.cpp b/dom/fetch/FetchUtil.cpp
index 330ef6dda6a9a..81369711a2688 100644
--- a/dom/fetch/FetchUtil.cpp
+++ b/dom/fetch/FetchUtil.cpp
@@ -259,14 +259,13 @@ class WorkerStreamOwner final {
       nsIAsyncInputStream* aStream, WorkerPrivate* aWorker) {
     RefPtr<WorkerStreamOwner> self = new WorkerStreamOwner(aStream);
 
-    self->mWorkerRef = WeakWorkerRef::Create(aWorker, [self]() {
+    self->mWorkerRef = StrongWorkerRef::Create(aWorker, "JSStreamConsumer", [self]() {
       if (self->mStream) {
         // If this Close() calls JSStreamConsumer::OnInputStreamReady and drops
         // the last reference to the JSStreamConsumer, 'this' will not be
         // destroyed since ~JSStreamConsumer() only enqueues a Destroyer.
         self->mStream->Close();
         self->mStream = nullptr;
-        self->mWorkerRef = nullptr;
       }
     });
 
@@ -299,7 +298,7 @@ class WorkerStreamOwner final {
   // Read from any thread but only set/cleared on the worker thread. The
   // lifecycle of WorkerStreamOwner prevents concurrent read/clear.
   nsCOMPtr<nsIAsyncInputStream> mStream;
-  RefPtr<WeakWorkerRef> mWorkerRef;
+  RefPtr<StrongWorkerRef> mWorkerRef;
 };
 
 class JSStreamConsumer final : public nsIInputStreamCallback {
diff --git a/dom/file/FileReader.cpp b/dom/file/FileReader.cpp
index 1884d9a69794d..a76b9eafb50bc 100644
--- a/dom/file/FileReader.cpp
+++ b/dom/file/FileReader.cpp
@@ -615,15 +615,15 @@ FileReader::Notify(nsITimer* aTimer) {
 // InputStreamCallback
 NS_IMETHODIMP
 FileReader::OnInputStreamReady(nsIAsyncInputStream* aStream) {
-  if (mReadyState != LOADING || aStream != mAsyncStream) {
-    return NS_OK;
-  }
-
   // We use this class to decrease the busy counter at the end of this method.
   // In theory we can do it immediatelly but, for debugging reasons, we want to
   // be 100% sure we have a workerRef when OnLoadEnd() is called.
   FileReaderDecreaseBusyCounter RAII(this);
 
+  if (mReadyState != LOADING || aStream != mAsyncStream) {
+    return NS_OK;
+  }
+
   uint64_t count;
   nsresult rv = aStream->Available(&count);
 
@@ -722,14 +722,7 @@ void FileReader::Abort() {
 
   MOZ_ASSERT(mReadyState == LOADING);
 
-  ClearProgressEventTimer();
-
-  if (mAsyncWaitRunnable) {
-    mAsyncWaitRunnable->Cancel();
-    mAsyncWaitRunnable = nullptr;
-  }
-
-  mReadyState = DONE;
+  Cleanup();
 
   // XXX The spec doesn't say this
   mError = DOMException::Create(NS_ERROR_DOM_ABORT_ERR);
@@ -738,30 +731,12 @@ void FileReader::Abort() {
   SetDOMStringToNull(mResult);
   mResultArrayBuffer = nullptr;
 
-  // If we have the stream and the busy-count is not 0, it means that we are
-  // waiting for an OnInputStreamReady() call. Let's abort the current
-  // AsyncWait() calling it again with a nullptr callback. See
-  // nsIAsyncInputStream.idl.
-  if (mAsyncStream && mBusyCount) {
-    mAsyncStream->AsyncWait(/* callback */ nullptr,
-                            /* aFlags*/ 0,
-                            /* aRequestedCount */ 0, mTarget);
-    DecreaseBusyCounter();
-    MOZ_ASSERT(mBusyCount == 0);
-
-    mAsyncStream->Close();
-  }
-
-  mAsyncStream = nullptr;
   mBlob = nullptr;
 
-  // Clean up memory buffer
-  FreeFileData();
-
   // Dispatch the events
   DispatchProgressEvent(nsLiteralString(ABORT_STR));
   DispatchProgressEvent(nsLiteralString(LOADEND_STR));
-}  // namespace dom
+}
 
 nsresult FileReader::IncreaseBusyCounter() {
   if (mWeakWorkerRef && mBusyCount++ == 0) {
@@ -791,7 +766,7 @@ void FileReader::DecreaseBusyCounter() {
   }
 }
 
-void FileReader::Shutdown() {
+void FileReader::Cleanup() {
   mReadyState = DONE;
 
   if (mAsyncWaitRunnable) {
@@ -807,11 +782,12 @@ void FileReader::Shutdown() {
   ClearProgressEventTimer();
   FreeFileData();
   mResultArrayBuffer = nullptr;
+}
 
-  if (mWeakWorkerRef && mBusyCount != 0) {
-    mStrongWorkerRef = nullptr;
+void FileReader::Shutdown() {
+  Cleanup();
+  if (mWeakWorkerRef) {
     mWeakWorkerRef = nullptr;
-    mBusyCount = 0;
   }
 }
 
diff --git a/dom/file/FileReader.h b/dom/file/FileReader.h
index 6b48c54043fa7..4c86886b894b6 100644
--- a/dom/file/FileReader.h
+++ b/dom/file/FileReader.h
@@ -156,6 +156,7 @@ class FileReader final : public DOMEventTargetHelper,
   nsresult IncreaseBusyCounter();
   void DecreaseBusyCounter();
 
+  void Cleanup();
   void Shutdown();
 
   char* mFileData;

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the tbb-commits mailing list