fix: serialize V8 isolate creation across threads#222
Open
benpalevsky wants to merge 1 commit into
Open
Conversation
Shadow/worker Environments are constructed on worker threads via create_shadow_environment, concurrently with the main Environment and each other. Unsynchronized first-time v8::Isolate::New races V8's process-global lazy global initialization and SIGSEGVs deep in v8::base::LazyInstanceImpl<Mutex>::InitInstance on optimized builds. Wrap GlobalInitialize::init() and v8::Isolate::New in a process-global Mutex so first-time V8 init runs exactly once across all threads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
GodotJS can start extra V8 "environments" on background threads. For example, the editor's resource loader creates short-lived, pooled "shadow" environments on worker threads while the main environment is also starting up.
The first
v8::Isolate::Newcall in a process triggers one-time initialization inside V8 that is not thread-safe. (GodotJS's ownGlobalInitialize::init()is already a thread-safe magic static; the race is V8-internal, kicked off by the first concurrent isolate creations.) So when several environments create isolates at the same moment on different threads, they race through that lazy init and the engine crashes (segfault) deep inside V8 (inLazyInstanceImpl<Mutex>::InitInstance, on optimized builds).What this changes
Put a single process-wide lock around the first-time V8 setup and the isolate creation in the
Environmentconstructor (bridge/jsb_environment.cpp), so only one thread runs that block at a time. Everything after it (setting callbacks, creating the context, and so on) stays outside the lock and runs normally.The lock is one function-local
staticmutex, which means every thread shares the same single lock — that's what makes the protection process-wide instead of per-environment. Most of the diff is just re-indentation from wrapping the block.How to verify
Rebuild the engine and stress concurrent environment creation — for example a batch reimport that spawns shadow environments on worker threads — and confirm it no longer crashes on startup.
A changeset is included.