Commit b08afbc3 authored by Perry Jiang's avatar Perry Jiang
Browse files

Bug 1620840 - give RemoteWorkerChild a StrongWorkerRef r=asuth

Allows RemoteWorkerChild to clean up its data appropriately - objects
that must be released on the worker thread should never outlive the worker
thread, and the StrongWorkerRef/WorkerControlRunnable helps guarantee that.

Differential Revision: https://phabricator.services.mozilla.com/D72798
parent fd2e8753
Loading
Loading
Loading
Loading
+12 −3
Original line number Original line Diff line number Diff line
@@ -158,11 +158,11 @@ class MessagePortIdentifierRunnable final : public WorkerRunnable {
// This is used to release WeakWorkerRefs which can only have their refcount
// This is used to release WeakWorkerRefs which can only have their refcount
// modified on the owning thread (worker thread in this case). It also keeps
// modified on the owning thread (worker thread in this case). It also keeps
// alive the associated WorkerPrivate until the WeakWorkerRef is released.
// alive the associated WorkerPrivate until the WeakWorkerRef is released.
class ReleaseWorkerRunnable final : public WorkerRunnable {
class ReleaseWorkerRunnable final : public WorkerControlRunnable {
 public:
 public:
  ReleaseWorkerRunnable(RefPtr<WorkerPrivate>&& aWorkerPrivate,
  ReleaseWorkerRunnable(RefPtr<WorkerPrivate>&& aWorkerPrivate,
                        RefPtr<WeakWorkerRef>&& aWeakRef)
                        RefPtr<WeakWorkerRef>&& aWeakRef)
      : WorkerRunnable(aWorkerPrivate),
      : WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
        mWorkerPrivate(std::move(aWorkerPrivate)),
        mWorkerPrivate(std::move(aWorkerPrivate)),
        mWeakRef(std::move(aWeakRef)) {
        mWeakRef(std::move(aWeakRef)) {
    MOZ_ASSERT(mWorkerPrivate);
    MOZ_ASSERT(mWorkerPrivate);
@@ -512,8 +512,17 @@ void RemoteWorkerChild::InitializeOnWorker() {
    NS_ProxyRelease(__func__, mOwningEventTarget, self.forget());
    NS_ProxyRelease(__func__, mOwningEventTarget, self.forget());
  });
  });


  // Let RemoteWorkerChild own the WorkerPrivate; RemoteWorkerChild's state
  // transitions should guarantee the WorkerPrivate is cleaned up correctly.
  // This also reduces some complexity around thread lifetimes guarantees that
  // RemoteWorkerChild's state transitions rely on (e.g. the worker thread
  // terminating unexpectedly).
  RefPtr<StrongWorkerRef> strongRef =
      StrongWorkerRef::Create(workerPrivate, __func__);

  RefPtr<WeakWorkerRef> workerRef = WeakWorkerRef::Create(
  RefPtr<WeakWorkerRef> workerRef = WeakWorkerRef::Create(
      workerPrivate, [selfWeakRef = std::move(selfWeakRef)]() mutable {
      workerPrivate, [selfWeakRef = std::move(selfWeakRef),
                      strongRef = std::move(strongRef)]() mutable {
        RefPtr<RemoteWorkerChild> self(selfWeakRef);
        RefPtr<RemoteWorkerChild> self(selfWeakRef);


        if (NS_WARN_IF(!self)) {
        if (NS_WARN_IF(!self)) {