Microtask queue execution after Isolate::TerminateExecution() timeout
0 reputation · 30 Dec 2021, 02:20 UTC
0 reputation · 30 Dec 2021, 02:20 UTC
When an embedder enforces a script timeout by calling Isolate::TerminateExecution(), V8 sets an internal flag that causes the next interrupt check to throw a special Terminated exception. The embedder catches this exception via TryCatch and must call CancelTerminateExecution() to reset the isolate for reuse.
The documented behavior states that termination is "best effort" and does not guarantee immediate stop. A specific unresolved aspect is microtask queue handling: if termination occurs while a promise reaction is running, already-queued microtasks may continue to execute before the Terminated exception propagates. This can produce observable side effects—such as promise resolutions, global mutations, or additional async scheduling—after the logical timeout deadline has passed.
The V8 source indicates interrupt checks occur at specific points in generated code and certain runtime calls, but the exact placement varies by version and optimization tier. WebAssembly code without explicit interrupt checks and long-running native operations (regex, JSON.parse) can further delay termination delivery.
Terminated exception unwinds the stack, or only microtasks already queued at the moment of the interrupt check?CancelTerminateExecution() when the embedder attempts to reuse the isolate?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.