diff --git a/dom/workers/loader/WorkerModuleLoader.cpp b/dom/workers/loader/WorkerModuleLoader.cpp index 5ae548434e08..a7a9e49b3c02 100644 --- a/dom/workers/loader/WorkerModuleLoader.cpp +++ b/dom/workers/loader/WorkerModuleLoader.cpp @@ -9,6 +9,7 @@ #include "mozilla/dom/WorkerLoadContext.h" #include "mozilla/dom/WorkerPrivate.h" #include "mozilla/dom/workerinternals/ScriptLoader.h" +#include "mozilla/dom/WorkerScope.h" #include "WorkerModuleLoader.h" #include "nsISupportsImpl.h" @@ -123,4 +124,10 @@ void WorkerModuleLoader::OnModuleLoadComplete(ModuleLoadRequest* aRequest) { } } +bool WorkerModuleLoader::IsModuleEvaluationAborted( + ModuleLoadRequest* aRequest) { + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + return workerPrivate->GlobalScope()->IsDying(); +} + } // namespace mozilla::dom::workerinternals::loader diff --git a/dom/workers/loader/WorkerModuleLoader.h b/dom/workers/loader/WorkerModuleLoader.h index 1d7b94c12a08..87b3fde86244 100644 --- a/dom/workers/loader/WorkerModuleLoader.h +++ b/dom/workers/loader/WorkerModuleLoader.h @@ -67,6 +67,8 @@ class WorkerModuleLoader : public JS::loader::ModuleLoaderBase { JS::MutableHandle aModuleScript) override; void OnModuleLoadComplete(ModuleLoadRequest* aRequest) override; + + bool IsModuleEvaluationAborted(ModuleLoadRequest* aRequest) override; }; } // namespace mozilla::dom::workerinternals::loader diff --git a/js/loader/ModuleLoaderBase.cpp b/js/loader/ModuleLoaderBase.cpp index 0bee9c4283d6..63ae2948d038 100644 --- a/js/loader/ModuleLoaderBase.cpp +++ b/js/loader/ModuleLoaderBase.cpp @@ -1227,16 +1227,11 @@ nsresult ModuleLoaderBase::EvaluateModuleInContext( // unless the user cancels execution. MOZ_ASSERT_IF(ok, !JS_IsExceptionPending(aCx)); - // For long running scripts, the request may be cancelled abruptly. This - // may also happen if the loader is collected before we get here. - if (request->IsCanceled() || !mLoader) { - return NS_ERROR_ABORT; - } - - if (!ok) { + if (!ok || IsModuleEvaluationAborted(request)) { LOG(("ScriptLoadRequest (%p): evaluation failed", aRequest)); // For a dynamic import, the promise is rejected. Otherwise an error is // reported by AutoEntryScript. + rv = NS_ERROR_ABORT; } // ModuleEvaluate returns a promise unless the user cancels the execution in @@ -1248,7 +1243,11 @@ nsresult ModuleLoaderBase::EvaluateModuleInContext( } if (request->IsDynamicImport()) { - FinishDynamicImport(aCx, request, NS_OK, evaluationPromise); + if (NS_FAILED(rv)) { + FinishDynamicImportAndReject(request, rv); + } else { + FinishDynamicImport(aCx, request, NS_OK, evaluationPromise); + } } else { // If this is not a dynamic import, and if the promise is rejected, // the value is unwrapped from the promise value. diff --git a/js/loader/ModuleLoaderBase.h b/js/loader/ModuleLoaderBase.h index 6047909392e8..2f7dae884f76 100644 --- a/js/loader/ModuleLoaderBase.h +++ b/js/loader/ModuleLoaderBase.h @@ -246,6 +246,10 @@ class ModuleLoaderBase : public nsISupports { // Called when a module script has been loaded, including imports. virtual void OnModuleLoadComplete(ModuleLoadRequest* aRequest) = 0; + virtual bool IsModuleEvaluationAborted(ModuleLoadRequest* aRequest) { + return false; + } + // Get the error message when resolving failed. The default is to call // nsContentUtils::FormatLoalizedString. But currently // nsContentUtils::FormatLoalizedString cannot be called on a worklet thread,