commit 855240736e9cbcc354020571f1ad1638b896fad7
parent 5a9be968ebdd1116ffba8b4df6353c88b321a523
Author: Matthew Gaudet <mgaudet@mozilla.com>
Date: Thu, 4 Dec 2025 22:24:20 +0000
Bug 2003956 - Ignore JS microtasks when they do not run r=smaug
Differential Revision: https://phabricator.services.mozilla.com/D275093
Diffstat:
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/xpcom/base/CycleCollectedJSContext.cpp b/xpcom/base/CycleCollectedJSContext.cpp
@@ -984,6 +984,15 @@ static void MOZ_CAN_RUN_SCRIPT RunMicroTask(
return;
}
+ // After this point, if we fail to run, we
+ //
+ // 1. Know we have JS microtask
+ // 2. Can freely ignore it if we cannot execute it.
+ //
+ // Create a ScopeExit to handle this.
+ auto ignoreMicroTasks = mozilla::MakeScopeExit(
+ [&aMicroTask]() { aMicroTask.get().IgnoreJSMicroTask(); });
+
// Avoid the overhead of GetFlowIdFromJSMicroTask in the common case
// of not having the profiler enabled.
mozilla::Maybe<AutoProfilerTerminatingFlowMarkerFlowOnly> terminatingMarker;
@@ -1080,12 +1089,13 @@ static void MOZ_CAN_RUN_SCRIPT RunMicroTask(
"promise callback" /* Some tests care about this string. */,
dom::CallbackObject::eReportExceptions);
if (!setup.GetContext()) {
- // We can't run, so we must ignore here!
- aMicroTask.get().IgnoreJSMicroTask();
-
return;
}
+ // At this point we will definitely consume the task, so we
+ // no longer need the scope exit.
+ ignoreMicroTasks.release();
+
// Note: We're dropping the return value on the floor here, however
// cleanup and exception handling are done as part of the CallSetup
// destructor if necessary.