BUG: preserve BitGenerator state after failed reinitialization - #32787
Open
Johnny-Kao wants to merge 1 commit into
Open
Johnny-Kao wants to merge 1 commit into
Johnny-Kao wants to merge 1 commit into
Conversation
Validate seed inputs before mutating BitGenerator state so a failed reinitialization cannot leave a valid generator with a NULL state pointer. Validate Philox counter/key inputs before calling the base initializer for the same reason. Closes numpygh-28784.
Author
|
The only failing check is This PR does not modify the limited-API test or wheel CI configuration, and all other checks pass. Could a maintainer please rerun this failed job? |
This branch has not been deployed
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.
PR summary
Closes #28784
This PR fixes a segmentation fault that can occur when an existing NumPy random
BitGeneratoris reinitialized with invalid input and the reinitialization fails.Previously,
BitGenerator.__init__()could reset parts of the existing instance before the new seed had been fully validated. As a result, a failed reinitialization could raise the expected Python exception while still leaving the existingBitGeneratorwith an invalid internal state. A subsequent call torandom_raw()could then dereference that invalid state and crash the interpreter.This PR changes initialization so that seed validation is completed before the existing
BitGeneratorstate is mutated. If reinitialization fails, the exception is raised while the previously valid generator state is preserved.Philoxrequires additional handling because itscounterandkeyarguments are validated separately from the base seed. Their conversion and validation are therefore also performed before callingBitGenerator.__init__(), preventing an invalidcounterorkeyfrom partially reinitializing an existing Philox instance.Regression tests verify that failed reinitialization preserves the previous random stream for:
MT19937PCG64PCG64DXSMSFC64PhiloxAdditional tests cover failed Philox reinitialization with invalid
counterandkeyvalues.Before the fix:
After the fix:
Standard successful initialization behavior is unchanged. The additional validation occurs during initialization/reinitialization and does not add checks to the random-number generation hot path.
First time contributor introduction
This is my first contribution to NumPy. I have previously contributed to SciPy and Python-Blosc2, and I am interested in continuing to contribute to the Scientific Python ecosystem.
AI Disclosure
Tool used: Anthropic's Claude Code
I used AI-assisted tooling to investigate and draft this fix; I've reviewed, tested, and can explain every change above.