Skip to content

BUG: fix free-threaded races in RandomState - #30360

Merged
mattip merged 4 commits into
numpy:mainfrom
ngoldbaum:randomstate-critical-sections
Dec 3, 2025
Merged

BUG: fix free-threaded races in RandomState#30360
mattip merged 4 commits into
numpy:mainfrom
ngoldbaum:randomstate-critical-sections

Conversation

@ngoldbaum

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.

@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
@ngoldbaum
ngoldbaum force-pushed the randomstate-critical-sections branch from 03132a0 to 95e36ee Compare December 2, 2025 22:55
@ngoldbaum

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...

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

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.

@ngoldbaum
ngoldbaum marked this pull request as draft December 2, 2025 23:39
@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

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.

@rkern

rkern commented Dec 3, 2025

Copy link
Copy Markdown
Member

Seems sound to me.

@mattip
mattip merged commit 24dcae9 into numpy:main Dec 3, 2025
71 of 72 checks passed
@mattip

mattip commented Dec 3, 2025

Copy link
Copy Markdown
Member

Thanks @ngoldbaum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

Development

Successfully merging this pull request may close these issues.

4 participants