Repository navigation
fix(runtime): release async module-graph loads without V8 state off the isolate thread - #619
edusperoni wants to merge 2 commits into
Conversation
…he isolate thread An async module-graph load owns a v8::Global for its context. Stream compile pool threads and NSURLSession threads keep references to the load after posting its fetch completion to the isolate thread. When the isolate thread ran that completion to the end of the walk first, the background thread dropped the last reference, and the Global was destroyed off the isolate thread without a Locker while the isolate could be running JS. A load now resets its context Global on the isolate thread as soon as it completes, so it holds no V8 state whichever thread drops it last. Teardown already did the same for loads still in flight. A stream compile job also releases the load, ticket, fetch result and completion it holds before leaving the compile gate, which is all teardown waits on.
The context Global of a completed module-graph load was reset after its completion callback returned, so a C++ exception escaping the callback would leave a completed load holding V8 state for another thread to release. The callback receives the context as a Local from the caller's handle scope and never reads the load's Global, so the reset now comes first.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
What
An async module-graph load (
AsyncGraphLoadinModuleInternalCallbacks.mm) owns av8::Global<v8::Context>. Background threads keep references to it:StreamCompilePool(large bodies) captures the load, the ticket, the fetch result and the completion, and the pool thread kept the job's closure until the job function returned, after it had posted the completion and afterLeave()on the compile gate;FetchModuleBodyAsynccaptures the load and lives until the NSURLSession thread deletes it, after calling it.When the isolate thread ran the posted completion to the end of the walk first, the background thread dropped the last reference to a completed load, and
~v8::Globalran on that thread without a Locker while the isolate could be running JS.This PR:
AsyncGraphMaybeComplete, beforeonCompleteis called, so no way out of the callback (a C++ exception included) skips it. The callback receives the context as av8::Localfrom the caller's handle scope and never reads the load's Global. A completed load then owns no V8 state, so it does not matter which thread drops it last. Teardown (KillAsyncGraphLoadsForIsolate) already did the same for loads still in flight.Leave()on the compile gate, which is all teardown waits on. The job moves its load, ticket, result and completion into an inner scope that ends beforeLeave(). The captures are init-captures: the load, ticket and result areconst¶meters, and a plain by-copy capture of them is aconstmember thatstd::movecopies instead of releasing.~AsyncGraphLoadcomment, which said the destructor runs on the JS thread on the normal path. It may run on any thread; completion and teardown leave it nothing to release there.Nothing JS-visible changes.
Why
~v8::Globalreturns its slot to the isolate's global handle table. Doing that from a thread that does not hold the isolate's lock could race with the isolate thread's own use of that table. This is reasoning from what~Globaldoes, not an observed failure: the runs below observed the unlocked destruction itself, and no crash or test failure came with it.How it was verified
The window is a few instructions wide, so a passing suite says nothing either way. The check used temporary instrumentation that is not part of this PR:
~AsyncGraphLoadlogged a line when it ran with a non-empty context Global on a thread other than the isolate's own thread (the thread id recorded when the isolate thread starts a load or runs one of its fetch completions), and recorded whether that thread held the isolate's Locker (v8::Locker::IsLocked). Lines were classified as the stream compile pool path or the transport path by a thread-local flag set on pool threads./esm/big.mjs, streamed) and then 60 distinct small modules (/esm/query.mjs) one after another, on top of the full suite, which includes the large-module specs ("survives workers terminated while their large modules compile" among them).Counts are full-suite runs on an iOS 26.3 simulator. "Unlocked" means a non-empty Global destroyed on a thread that was neither the isolate's thread nor holding its Locker.
The pool zero in the release-only row holds because that run had no sleep inside the job; the release narrows the pool window to the instructions between posting and the end of the release scope, it does not close it. Every unlocked destruction logged on main was of a completed load (
completed=1,dead=0); none was of a load still in flight.All of these runs passed the suite, including the ones with 246 unlocked destructions.
The instrumented "this PR" run had the reset right after
onCompletereturned. It now comes right before the call, which only makes the load inert sooner; the instrumented runs were not repeated for that change.In the fixed run with sleeps, 57 completed loads were still dropped last on a pool thread and 190 on a transport thread, all with an empty Global: the window was still hit, and was harmless. The run with only the job release shows that the release alone moves the last drop from the pool thread to the transport thread; resetting on completion is what removes it.
On main without sleeps, nothing unlocked was logged: the defect could occur there, but these runs did not hit it. Every run without the reset on completion also logged one destruction on a background thread that held the Locker: an
import()from a GCD thread whose disk-only graph completes inline on that thread, which is not this defect. The runs with the reset logged none.Without the instrumentation:
-Wall -Werror, iOS Simulator arm64 and x86_64): succeeded.Not covered
HasPendingAsyncModuleGraphWorkstill counts a load until its last reference is dropped, which can be on a background thread shortly after the load completed. Its only reader, the boot pump inrunMainApplication, pumps 10 ms run loop slices under a deadline, so a late decrement costs at most one extra slice and is never early. Left as it is.~EventLoopalready allows a last drop after shutdown on another thread.Summary by CodeRabbit