Skip to content

BUG: avoid BitGenerator segfault after failed re-init (gh-28784) - #47

Draft
cursor[bot] wants to merge 2 commits into
mainfrom
cursor/upstream-numpy-issue-3343
Draft

cursor[bot] wants to merge 2 commits into
mainfrom
cursor/upstream-numpy-issue-3343

Conversation

@cursor

@cursor cursor Bot commented Aug 3, 2026

Copy link
Copy Markdown

PR summary

Fixes numpy/numpy#28784: re-running BitGenerator.__init__ with an invalid seed used to clear _bitgen.state to NULL before seed validation, so a later random_raw() segfaulted.

Approach

  • Resolve/SeedSequence-validate the seed before mutating instance state in BitGenerator.__init__, and stop clearing _bitgen.state early (subclasses install it).
  • Defensive ValueError in random_raw / _benchmark when callbacks/state were never installed.
  • Generator rejects incompletely initialized bit generators (also hardens the incomplete-subclass case from BUG: np.random.Generator(MyBitGenerator).random() segfaults every time numpy/numpy#25023).
  • Validate Philox counter/key before applying them on (re)init.

Test plan

  • spin test -v -t numpy/random/tests/test_regression.py
  • spin test -v -t numpy/random/tests
  • Full spin test -v: 48741 passed, 345 skipped, 3647 deselected, 56 xfailed, 4 xpassed
  • spin lint: passed

Upstream issue: numpy#28784

First time committer introduction

Automated fork contribution targeting a high-impact Cython crash fix in numpy.random.

AI Disclosure

This pull request was prepared by a Cursor Cloud Agent (Cursor Grok 4.5). The Cython/Python code changes, regression tests, and PR text were AI-generated and then verified locally with spin build, targeted random tests, full spin test -v, and spin lint.

Open in Web View Automation 

cursoragent and others added 2 commits August 3, 2026 18:05
Validate the seed before mutating BitGenerator state so a failed
re-__init__ cannot leave a NULL _bitgen.state. Add defensive checks in
random_raw/Generator and validate Philox counter/key before applying.

Co-authored-by: Kushagra Agarwal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: segfault from random_raw from instance initialized from invalid value

1 participant