GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: ensure lock is held when accessing or writing to RNG state by ngoldbaum · Pull Request #32061 · numpy/numpy

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Jul 22, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

PR summary

Fixes #32059.

#30360 was supposed to fix these issues but clearly this is harder than I thought.

With this PR the lock is being held everywhere we access the RNG state. Also adds new tests for the case from the issue as well as some other spots I noticed while auditing the RNG codebase for similar issues.

I was able to reproduce the race in a slightly modified version of the script in the issue and it no longer triggers after this PR.

AI Disclosure

I used an AI model to find places where we access RNG state without holding the lock. It also helped with the new tests.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ngoldbaum
BUG: ensure lock is held when accessing or writing to RNG state
9e2c280
@github-actions github-actions Bot added the 00 - Bug label Jul 22, 2026
@ngoldbaum ngoldbaum mentioned this pull request Jul 22, 2026
BUG: Data race reading and writing np.random.seed #32059
Closed
@ngoldbaum ngoldbaum assigned kumaraditya303 Jul 22, 2026
@jorenham jorenham added the component: numpy.random label Jul 22, 2026
@ngoldbaum ngoldbaum added the 09 - Backport-Candidate PRs tagged should be backported label Jul 22, 2026
@ngoldbaum
BUG: make get_state access state atomically
ccb5eb8
@ngoldbaum ngoldbaum mentioned this pull request Jul 22, 2026
BUG: fully reset cached RNG state for non-MT19937 RNGs #32062
Merged
ngoldbaum
ngoldbaum commented Jul 22, 2026
View reviewed changes
Comment thread numpy/random/mtrand.pyx
Comment on lines 303 to 307
if st['bit_generator'] != 'MT19937' and legacy:
warnings.warn('get_state and legacy can only be used with the '
'MT19937 BitGenerator. To silence this warning, '
'set `legacy` to False.', RuntimeWarning)
legacy = False

@ngoldbaum ngoldbaum Jul 22, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might as well move the state access and the warning into the lock's critical section

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
rkern
rkern approved these changes Jul 22, 2026
View reviewed changes

@rkern rkern left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@charris
charris merged commit c6b36d9 into numpy:main Jul 22, 2026
89 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@charris

charris commented Jul 22, 2026

Copy link
Copy Markdown
Member

The errors are unrelated. Thanks Nathan.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303

kumaraditya303 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

The fix is correct and it covers all places. In future I would like PRs to be left open for atleast a day so that I get time to review it before merge. Thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@charris charris mentioned this pull request Jul 27, 2026
BUG: ensure lock is held when accessing or writing to RNG state (#32061) #32122
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 27, 2026
@charris charris mentioned this pull request Jul 27, 2026
BUG: fully reset cached RNG state for non-MT19937 RNGs (#32062) #32123
Merged
charris pushed a commit that referenced this pull request Jul 27, 2026
@ngoldbaum @charris
BUG: ensure lock is held when accessing or writing to RNG state (#32061)
dd23170
charris added a commit that referenced this pull request Jul 27, 2026
@charris
Merge pull request #32122 from charris/backport-32061
adc5cdc
BUG: ensure lock is held when accessing or writing to RNG state (#32061)
@Parth1353 Parth1353 mentioned this pull request Aug 30, 2026
BUG: fix SeedSequence.spawn and set_bit_generator thread-safety (gh-32063) #32463
Closed
@bashtage bashtage mentioned this pull request Nov 2, 2026
ENH: Hold the bit generator lock around state, seed and advance bashtage/randomgen#456
Merged
bashtage added a commit to vigna/randomgen that referenced this pull request Nov 3, 2026
@bashtage @claude
ENH: Hold the bit generator lock around state, seed and advance
fc2eedd
Port NumPy's lock changes (numpy/numpy#30360, numpy/numpy#32061) so
bit generators are safe to share across threads on free-threaded Python.

- Always use an RLock, since NumPy < 2.5 provides a plain Lock that
  deadlocks when locked calls nest; RDRAND's RaisingLock wraps an RLock
- Lock state get/set, seeding (including key=/counter= paths), advance
  and jump in every bit generator and in UserBitGenerator
- Validate state before writing in the AES, Philox and ThreeFry setters
  so a bad state no longer leaves the generator half updated
- Lock the scalar path of ExtendedGenerator.complex_normal
- Declare the extensions free-threading compatible when built with
  Cython >= 3.1 so importing randomgen does not re-enable the GIL
- Add threaded tests covering seed/state races, reentrancy and the GIL

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@rkern rkern rkern approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@kumaraditya303 kumaraditya303

Labels

00 - Bug component: numpy.random

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

BUG: Data race reading and writing np.random.seed

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@ngoldbaum @charris @kumaraditya303 @rkern @jorenham
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.