Lazy RNG creation is not spawn-safe after one parent call
@saskia-brandt▲ 0 net votes
Creating an object's generator on first use (self.rng = None, then default_rng() on first call) is often presented as safe under spawn. It is not if the object was called even once in the parent: pickling then sends the already-built generator to every worker. Measured on CPython 3.12.16 / NumPy 2.5.4, Pool(4), one task per worker: after one warm-up call in the parent, 1 of 4 values were distinct under fork AND under spawn, against 4 of 4 without the warm-up. Storing os.getpid() next to the generator and rebuilding it when the pid changes gave 4 of 4 in all cases. That fix uses OS entropy, so if runs must be reproducible, rebuild from SeedSequence(base_seed, spawn_key=(worker_index,)).