Bug 1764921 - Delay full worker shutdown until LockManagerChild is destructed r=asuth,smaug

Differential Revision: https://phabricator.services.mozilla.com/D137792
This commit is contained in:
Kagami Sascha Rosylight 2022-12-07 15:13:57 +00:00
parent 3a1ac67a90
commit c89973144a
5 changed files with 55 additions and 1 deletions

View File

@ -61,6 +61,8 @@ class LockManager final : public nsISupports, public nsWrapperCache {
nsCOMPtr<nsIGlobalObject> mOwner;
RefPtr<locks::LockManagerChild> mActor;
// Revokes itself and triggers LockManagerChild deletion on worker shutdown
// callback.
RefPtr<WeakWorkerRef> mWorkerRef;
};

View File

@ -16,6 +16,13 @@
namespace mozilla::dom::locks {
LockManagerChild::LockManagerChild(nsIGlobalObject* aOwner) : mOwner(aOwner) {
if (!NS_IsMainThread()) {
mWorkerRef = IPCWorkerRef::Create(GetCurrentThreadWorkerPrivate(),
"LockManagerChild");
}
}
void LockManagerChild::NotifyBFCacheOnMainThread(nsPIDOMWindowInner* aInner,
bool aCreated) {
AssertIsOnMainThread();

View File

@ -9,6 +9,7 @@
#include "mozilla/dom/locks/PLockManagerChild.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/WorkerRef.h"
#include "nsIUUIDGenerator.h"
namespace mozilla::dom::locks {
@ -23,7 +24,7 @@ class LockManagerChild final : public PLockManagerChild {
static void NotifyBFCacheOnMainThread(nsPIDOMWindowInner* aInner,
bool aCreated);
explicit LockManagerChild(nsIGlobalObject* aOwner) : mOwner(aOwner){};
explicit LockManagerChild(nsIGlobalObject* aOwner);
nsIGlobalObject* GetParentObject() const { return mOwner; };
@ -37,6 +38,26 @@ class LockManagerChild final : public PLockManagerChild {
~LockManagerChild() = default;
nsCOMPtr<nsIGlobalObject> mOwner;
// This WorkerRef is deleted by destructor.
//
// Here we want to make sure the IPC layer deletes the actor before allowing
// WorkerPrivate deletion, since managed LockRequestChild holds a reference to
// global object (currently via mRequest member variable) and thus deleting
// the worker first causes an assertion failure. Having this ensures that all
// LockRequestChild instances must first have been deleted.
//
// (Because each LockRequestChild's ActorLifecycleProxy will hold
// a strong reference to its manager LockManagerChild's ActorLifecycleProxy,
// which means that the LockManagerChild will only be destroyed once all
// LockRequestChild instances have been destroyed. At that point, all
// references to the global should have been dropped, and then the
// LockManagerChild IPCWorkerRef will be dropped and the worker will be able
// to transition to the Killing state.)
//
// ActorDestroy can't release this since it does not guarantee to destruct and
// GC the managees (LockRequestChild) immediately.
RefPtr<IPCWorkerRef> mWorkerRef;
};
} // namespace mozilla::dom::locks

View File

@ -50,6 +50,14 @@ class LockRequestChild final : public PLockRequestChild,
LockManagerChild* CastedManager() const;
const LockRequest mRequest;
// This prevents the worker from being GC'ed when the caller is waiting to
// acquire the lock and when the lock is held.
//
// The StrongWorkerRef is dropped immediately in the shutdown notification
// callback, and thus does not ensure any cleanup before the worker advances
// to the Killing state. That is ensured instead by
// LockManagerChild::mWorkerRef, see also the details there.
RefPtr<StrongWorkerRef> mWorkerRef;
};

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html class="test-wait">
<meta charset="utf-8">
<script>
var worker = new Worker(URL.createObjectURL(new Blob([`
postMessage("hi");
(async () => {
const abort = new AbortController()
await navigator.locks.request("weblock_0", { signal: abort.signal }, () => {})
})()
`])));
worker.onmessage = () => {
worker.terminate();
document.documentElement.classList.remove("test-wait");
};
</script>