GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — BUG: fix SeedSequence.spawn and set_bit_generator thread-safety (gh-32063) by Parth1353 · Pull Request #32463 · numpy/numpy Conversation
|
| max threads inside the critical section | |
|---|---|
| holding the outgoing lock only | 4 (3/3 runs) |
| with this patch | 1 |
The fix gives RandomState a lock handle whose identity never changes. It
acquires whichever lock is current, re-checks afterwards, and retries if a swap
won the race — that retry fired 12042 times in a 3000-swap stress run. Only one
lock is ever held, so it cannot deadlock against code holding a bit generator's
lock directly.
The handle is installed lazily, on the first swap, since the singleton is the
only RandomState that set_bit_generator can reach. Everything else keeps
using the bare lock. A/B/A benchmark, alternating builds to control for drift:
| case | no handle | with handle | delta |
|---|---|---|---|
RandomState.random_sample() scalar |
145.9 ns | 144.8 ns | -0.8% |
RandomState.random_sample(10) |
371.9 ns | 370.0 ns | -0.5% |
RandomState.random_sample(100_000) |
337.8 us | 337.2 us | -0.2% |
RandomState.standard_normal() scalar |
188.2 ns | 186.8 ns | -0.7% |
RandomState.randint(0, 100) scalar |
1126.7 ns | 1127.1 ns | +0.0% |
Generator.random() scalar |
249.2 ns | 247.2 ns | -0.8% |
against a 1.3-2.4% noise floor between two identical builds. Installing the
handle unconditionally instead cost 15% on scalar RandomState.random_sample(),
which is why it is lazy.
test_parallel_seed_sequence_spawn — fails 3/3 without the spawn fix, passes 10/10 with it.test_failed_swap_leaves_singleton_usable (4 cases) and test_set_bit_generator_waits_for_lock — all fail without the mtrand fix.test_parallel_singleton_bit_generator_swap — hammers swaps against concurrent draws.Full suite: 47815 passed, 0 failed. spin lint clean. Built and tested on
macOS 15.7 arm64, Python 3.12, Accelerate.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
SeedSequence.spawn read n_children_spawned, constructed the children, and only then advanced the counter. Constructing a child runs Python code -- np.concatenate() inside get_assembled_entropy(), plus any subclass __init__ -- so the GIL can be released partway through. Two threads spawning from the same SeedSequence therefore observed the same starting index and returned children with identical spawn keys, i.e. bit generators producing identical streams. BitGenerator.spawn and Generator.spawn delegate here, so they inherited the bug. Reserve the whole block of spawn keys under a lock before building any child. Only the reservation is serialized; the children, which are the expensive part, are still built outside the lock. The lock is module-level rather than per-instance because adding an attribute to the SeedSequence cdef class changes the checksum that Cython's auto-generated unpickler validates, which makes every existing pickle of a SeedSequence, BitGenerator or Generator fail to load with "PickleError: Incompatible checksums". As a side effect an out-of-range n_children now raises OverflowError immediately instead of first trying to build billions of children. See numpygh-32063. Co-Authored-By: Claude Opus 5 <[email protected]>
_initialize_bit_generator() assigned self._bit_generator before checking the capsule, so a rejected argument dropped the last reference to the bit generator that self._bitgen.state still points into -- freeing it -- and left the RandomState wedged with an invalid _bit_generator. Validate first, so a failed set_bit_generator() is a no-op. The swap also rewrote the cached bitgen_t struct (function pointers plus the state pointer) with no synchronization, while distribution methods read that struct under the bit generator's lock with the GIL released. Hold the lock of the generator being replaced across the swap, so a draw already in flight finishes first. This does not yet give full mutual exclusion across a swap: a thread that read the old lock before the swap but acquires it afterwards can run concurrently with a thread holding the new lock. Closing that needs a guard that is not itself replaced, as discussed in numpygh-32063. See numpygh-32063. Co-Authored-By: Claude Opus 5 <[email protected]>
Holding the outgoing lock across the swap stops a draw already in flight from seeing a half-written bitgen_t, but it does not give mutual exclusion. RandomState uses the lock of its bit generator, so set_bit_generator replaces that lock, and call sites read self.lock well before they acquire it -- cont(), disc() and friends allocate the output array in between. A thread that read the old lock therefore ends up in the critical section at the same moment as one that acquired the new lock, and both use the same cached self._bitgen. Instrumenting double_fill() to count threads inside the critical section showed up to 4 at once while another thread swapped bit generators, in every run. Give RandomState a lock handle whose identity never changes. It acquires whichever lock is current, re-checks it afterwards, and retries if a swap won the race; the same instrumented run then never showed more than one thread inside the critical section. Only one lock is ever held, so this cannot deadlock against code that holds a bit generator's lock directly. The handle is installed lazily, on the first swap, because the singleton is the only RandomState that set_bit_generator can reach. Every other instance keeps using the bare lock, so the extra indirection is not on anybody's path unless they hot-swap. An A/B/A benchmark puts the change between -0.8% and 0.0% on scalar and array draws, against a 1.3-2.4% noise floor between identical builds; installing the handle unconditionally instead cost 15% on scalar RandomState.random_sample(). See numpygh-32063. Co-Authored-By: Claude Opus 5 <[email protected]>
|
You ignored the PR template, so I'm going to assume that this PR has been made with agentic AI. This violates our AI policy; we prefer to talk to humans. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.