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: fix free-threaded races in RandomState by ngoldbaum · Pull Request #30360 · numpy/numpy

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Dec 2, 2025

Copy link
Copy Markdown
Member

Fixes one of the races in #30085.

I'd appreciate it if one of @lysnikolaou, @kumaraditya303, or @crusaderky could look this over, since you're all doing work related to this change.

I verified that the test I added generates TSan races without the cython change to add critical sections.

As far as I can tell, all other reads and writes to internal state are either atomic (e.g. in get_state) or are done with a threading.lock held and don't need any additional synchronization.

Sorry, something went wrong.

Uh oh!

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

All reactions
@ngoldbaum ngoldbaum added 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703) 09 - Backport-Candidate PRs tagged should be backported labels Dec 2, 2025
@github-actions github-actions Bot added the 00 - Bug label Dec 2, 2025
@ngoldbaum
BUG: fix free-threaded races in RandomState
95e36ee
@ngoldbaum
ngoldbaum force-pushed the randomstate-critical-sections branch from 03132a0 to 95e36ee Compare December 2, 2025 22:55
@ngoldbaum
MAINT: bump minimum required Cython version
6c14af1
@ngoldbaum

ngoldbaum commented Dec 2, 2025

Copy link
Copy Markdown
Member Author

Looks like this also requires updating the minimum Cython version, so I'm going to remove the backport label...

All reactions

Sorry, something went wrong.

Uh oh!

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

@ngoldbaum ngoldbaum removed the 09 - Backport-Candidate PRs tagged should be backported label Dec 2, 2025
@ngoldbaum

ngoldbaum commented Dec 2, 2025

Copy link
Copy Markdown
Member Author

Well, looks like that didn't work on the TSan CI job. Likely will need a different approach. Converting to a draft PR for now.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ngoldbaum
ngoldbaum marked this pull request as draft December 2, 2025 23:39
ngoldbaum added 2 commits December 2, 2025 16:47
@ngoldbaum
MAINT: try with an RLock instead
258a202
@ngoldbaum
Revert "MAINT: bump minimum required Cython version"
638b4fb
This reverts commit 6c14af1.
@ngoldbaum ngoldbaum added the 09 - Backport-Candidate PRs tagged should be backported label Dec 3, 2025
@ngoldbaum
ngoldbaum marked this pull request as ready for review December 3, 2025 03:28
@ngoldbaum

ngoldbaum commented Dec 3, 2025

Copy link
Copy Markdown
Member Author

Nice! Just using the generator lock seems to work. Re-added the backport label since this doesn't require a Cython version bump using the lock.

Ping @rkern too.

All reactions

Sorry, something went wrong.

Uh oh!

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

@rkern

rkern commented Dec 3, 2025

Copy link
Copy Markdown
Member

Seems sound to me.

All reactions

Sorry, something went wrong.

Uh oh!

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

@mattip
mattip merged commit 24dcae9 into numpy:main Dec 3, 2025
@mattip

mattip commented Dec 3, 2025

Copy link
Copy Markdown
Member

Thanks @ngoldbaum

All reactions

Sorry, something went wrong.

Uh oh!

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

@charris charris mentioned this pull request Dec 9, 2025
BUG: fix free-threaded races in RandomState #30400
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Dec 9, 2025
@ngoldbaum ngoldbaum mentioned this pull request Jul 22, 2026
BUG: ensure lock is held when accessing or writing to RNG state #32061
Merged
@charris charris mentioned this pull request Jul 27, 2026
BUG: ensure lock is held when accessing or writing to RNG state (#32061) #32122
Merged
@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

No reviews

Uh oh!

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

Assignees

No one assigned

Labels

00 - Bug 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

4 participants

@ngoldbaum @rkern @mattip @charris
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.