fix hanging - #1112
Open
Sajish06 wants to merge 2 commits into
Open
fix hanging#1112Sajish06 wants to merge 2 commits into
Sajish06 wants to merge 2 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
@google-bot I signed it! |
|
This AI-generated PR (at least the description, good lord) is supposed to fix #1107 btw, since it's not linked |
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.
Fix deadlock and thread-safety issues during concurrent calls to
sample()Description
This PR resolves an issue where programs hang indefinitely when making concurrent calls to
CompiledMeasurementSampler.sample()(and related sampling methods) across multiple Python threads.Root Causes
Deadlock between C++ static initialization lock and Python GIL:
When multiple threads invoke
.sample()concurrently for the first time without prior NumPy array initialization in the process,pybind11::array_tlazily initializes its NumPy C-API bindings viapybind11::detail::npy_api::get().npy_api::get()initializes a function-local static (static const npy_api api = lookup();), which acquires an internal C++ compiler static initialization lock (_Init_thread_headeron MSVC /__cxa_guard_acquireon GCC/Clang).lookup()executespybind11::module_::import("numpy.core.multiarray"). In Python 3.12 / NumPy 2.x, if this module is not already loaded insys.modules,importlibruns.importlibyields or releases the GIL. Another thread waiting on the GIL runs, callssample(), and entersnpy_api::get(), where it blocks on the C++ compiler's static initialization lock while holding the Python GIL.Unsynchronized RNG & Moved-from State in
CompiledMeasurementSampler:In
compiled_measurement_sampler.pybind.h, the sampler holds a mutablestd::mt19937_64 rnginstance. Whensample_batch_measurementsis called, it doesstd::move(rng)into a localFrameSimulator, leaving the sampler'srngin an unspecified moved-from state while the circuit simulates. Concurrent calls on the same sampler instance race onrng, causing internal state corruption of the Mersenne Twister engine and unpredictable execution.Missing GIL Release in
CompiledMeasurementSampler:Unlike
CompiledDetectorSampler,CompiledMeasurementSampler::sample_to_numpy()did not release the GIL (pybind11::gil_scoped_release), unnecessarily serializing simulation workloads across separate sampler instances.Changes
glue/python/src/stim/__init__.py: Pre-importednumpyandnumpy.core.multiarrayduring initial import on the main thread so thatsys.modulesis populated before worker threads are spawned.src/stim/py/stim.pybind.cc: Added an explicit import ofnumpyinPYBIND11_MODULEfor cases where the compiled C extension is imported directly.rngand released GIL inCompiledMeasurementSampler:src/stim/py/compiled_measurement_sampler.pybind.h: Addedstd::unique_ptr<std::mutex> rng_mutexto preserve move-constructibility.src/stim/py/compiled_measurement_sampler.pybind.cc: Protectedrngwithstd::lock_guard<std::mutex>and released the GIL viapybind11::gil_scoped_releaseduringsample_batch_measurementsandsample_write.frame_siminCompiledDetectorSampler:src/stim/py/compiled_detector_sampler.pybind.handsrc/stim/py/compiled_detector_sampler.pybind.cc: Guardedframe_simwith a mutex duringsample_to_numpyandsample_writeto ensure thread-safe usage when multiple threads share an instance.test_concurrent_measurement_sampler()insrc/stim/py/compiled_measurement_sampler_pybind_test.pyusingThreadPoolExecutoracross both shared and independent sampler instances.Verification
ThreadPoolExecutor; completed successfully with expected array shapes and data integrity.src/stim/py/; all 24 tests passed.