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: make SeedSequence.spawn thread safe by shbhmbnsl1 · Pull Request #32889 · numpy/numpy

Conversation

@shbhmbnsl1

@shbhmbnsl1 shbhmbnsl1 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

PR summary

SeedSequence.spawn has a bug described in gh-32063 because of which multiple threads spawning from the same SeedSequence can observe same starting index and hence return children with identical spawn keys. This PR fixes that by introducing a lock as part of SeedSequence class. To preserve pickling of SeedSequence objects I have implemented a __reduce_ex__ function in its class which serialized the logical state without the lock.

First time contributor introduction

I am here to contribute as part of the NumFOCUS Volunteer Sprints Fall 2026 cohort. I recently got some of my contributions merged in (https://github.com/numpy/numpy/pulls?q=is%3Apr+state%3Aclosed+author%3Ashbhmbnsl1).

AI Disclosure

I used AI (OpenAI Codex specifically GPT-5.6-Sol) to get deeper understanding of the issue and how SeedSequence.spawn works internally. I also used it to suggest test case to test the duplicate spawning key using multi-threads.

Sorry, something went wrong.

Uh oh!

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

All reactions
@shbhmbnsl1
BUG: make SeedSequence.spawn thread safe
9a5845d
@github-actions github-actions Bot added the 00 - Bug label Oct 5, 2026
@shbhmbnsl1
shbhmbnsl1 marked this pull request as ready for review October 5, 2026 21:05
@ganesh-k13 ganesh-k13 added this to NF Sustaining Open Source Series 2026 Oct 6, 2026
@ganesh-k13 ganesh-k13 added the sustain-2026 Issues reserved for NumFOCUS Sustaining Open Source Series 2026 label Oct 6, 2026
@shbhmbnsl1

shbhmbnsl1 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@seberg @ngoldbaum Hi, I am looking for some reviews here. Please let me know if you are able to. Thanks in advance.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ngoldbaum

ngoldbaum commented Oct 7, 2026

Copy link
Copy Markdown
Member

I'm on vacation this week and will get to this soon. But in the meantime ping @rkern.

shbhmbnsl1 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

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

lakshayg
lakshayg reviewed Oct 10, 2026
View reviewed changes
Comment thread numpy/random/bit_generator.pyx Resolved

Uh oh!

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

Comment thread numpy/random/bit_generator.pyx Resolved

Uh oh!

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

@shbhmbnsl1
Changing to threading.lock instead of RLock
aa6fece
seberg
seberg reviewed Oct 13, 2026
View reviewed changes

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

Thanks, this looks good and the unpickling follows the existing pattern, so that also seems good.

I'll wait a bit, the only comment I have right now: It would be nice to use the run_threaded() helper to shorten that test a bit.

But if e.g. @ngoldbaum thinks that using atomics is nicer (and being OK with advanding the n_children_spawned on error) that is also fine to me.

(FWIW, in principle, I think it probably is better to pass the children to the thread rather than spawning in a threaded manner.)

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread numpy/random/bit_generator.pyx Resolved

Uh oh!

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

Comment thread numpy/random/bit_generator.pyx Resolved

Uh oh!

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

Comment thread numpy/random/tests/test_direct.py Resolved

Uh oh!

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

shbhmbnsl1 added 2 commits October 13, 2026 16:21
@shbhmbnsl1
Switching to only lock reserving n_children_spawned
3fc6dcf
@shbhmbnsl1
Refactor
b5f458c
@ngoldbaum ngoldbaum mentioned this pull request Oct 13, 2026
[ENH] Add wrappers for C11 atomics API cython/cython#8051
Open
shbhmbnsl1 added 3 commits October 13, 2026 18:19
@shbhmbnsl1
Removed lock from reduce_ex
37a4af0
@shbhmbnsl1
Use run_threaded utility
cd79a10
@shbhmbnsl1
Merge branch 'main' into bug/seedsequence-spawn-thread-safety
ce58c8a
@shbhmbnsl1
shbhmbnsl1 requested review from lakshayg, ngoldbaum and seberg October 14, 2026 01:45
@shbhmbnsl1

shbhmbnsl1 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

@lakshayg @seberg @ngoldbaum Thank you for all the amazing feedback. I have addressed the comments. Looking forward to another round of review.

All reactions

Sorry, something went wrong.

Uh oh!

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

ngoldbaum
ngoldbaum requested changes Oct 14, 2026
View reviewed changes
Comment thread numpy/random/tests/test_direct.py Resolved

Uh oh!

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

Comment thread numpy/random/bit_generator.pyx Resolved

Uh oh!

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

@shbhmbnsl1
Moving try-finally to run_threaded utility
31882a3
@InessaPawson InessaPawson moved this from Needs Review to In Progress in NF Sustaining Open Source Series 2026 Oct 15, 2026
@ngoldbaum

ngoldbaum commented Oct 16, 2026

Copy link
Copy Markdown
Member

The test failures look real to me.

All reactions

Sorry, something went wrong.

Uh oh!

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

@shbhmbnsl1

shbhmbnsl1 commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

The test failures look real to me.

@ngoldbaum sorry I was away for a vacation. I will fix these today.

All reactions

Sorry, something went wrong.

Uh oh!

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

shbhmbnsl1 added 4 commits October 22, 2026 15:46
@shbhmbnsl1
BUG: preserve old SeedSequence pickles
73012ff
@shbhmbnsl1
Merge branch 'main' into bug/seedsequence-spawn-thread-safety
6e2e031
@shbhmbnsl1
Fix lint error
95bcc12
@shbhmbnsl1
TYP: update run_threaded switch interval signature
edcb98e
@shbhmbnsl1
shbhmbnsl1 requested a review from ngoldbaum October 22, 2026 21:35
@shbhmbnsl1
TST: add legacy SeedSequence pickle regression test
444ea6d
@shbhmbnsl1

shbhmbnsl1 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

The test failures look real to me.

All tests pass now!

All reactions

Sorry, something went wrong.

Uh oh!

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

ngoldbaum
ngoldbaum reviewed Oct 23, 2026
View reviewed changes

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

This is a good direction but there are still some corner cases in the reducer you added.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread numpy/random/bit_generator.pyx Resolved

Uh oh!

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

Comment thread numpy/random/bit_generator.pyx Resolved

Uh oh!

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

Comment thread numpy/random/_pickle.py Resolved

Uh oh!

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

@shbhmbnsl1
BUG: preserve SeedSequence subclass pickle state
1d8d4ae
@shbhmbnsl1
shbhmbnsl1 requested a review from ngoldbaum October 26, 2026 16:52
@shbhmbnsl1

shbhmbnsl1 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Looks like these are intermittent failures in the CI.
@ngoldbaum looking for your review here.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ngoldbaum

ngoldbaum commented Oct 27, 2026

Copy link
Copy Markdown
Member

looking for your review here

You pinged me yesterday, please don't ping twice within such a short interval.

I'm happy to review your code and pings do help me keep track of things that slip through the cracks, but please allow at least two weeks between pings. I'd use that as a rule of thumb for any open-source contribution. Sometimes things take a while and that's ok.

All reactions

Sorry, something went wrong.

Uh oh!

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

@shbhmbnsl1

shbhmbnsl1 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

looking for your review here

You pinged me yesterday, please don't ping twice within such a short interval.

I'm happy to review your code and pings do help me keep track of things that slip through the cracks, but please allow at least two weeks between pings. I'd use that as a rule of thumb for any open-source contribution. Sometimes things take a while and that's ok.

Sorry, noted!
Didn't mean to introduce a sense of urgency here if it felt like that. Was mostly revisiting the tests failure in the CI and left a comment.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ganesh-k13 ganesh-k13 moved this from In Progress to Needs Review in NF Sustaining Open Source Series 2026 Oct 28, 2026
ngoldbaum
ngoldbaum reviewed Oct 29, 2026
View reviewed changes

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

Just two minor comments.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread numpy/random/tests/data/seedsequence_cython.pkl.gz Resolved

Uh oh!

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

Comment thread numpy/random/bit_generator.pyx Resolved

Uh oh!

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

@ngoldbaum

ngoldbaum commented Oct 29, 2026

Copy link
Copy Markdown
Member

also merging with main should avoid the CI flakes you hit

shbhmbnsl1 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

shbhmbnsl1 added 3 commits October 29, 2026 21:19
@shbhmbnsl1
TST: inline legacy SeedSequence pickle data
6e3d8ce
@shbhmbnsl1
Merge branch 'main' into bug/seedsequence-spawn-thread-safety
1897674
@shbhmbnsl1
Use __reduce__ instead __reduce_ex__
c8d351c
@shbhmbnsl1
shbhmbnsl1 requested a review from ngoldbaum November 3, 2026 14:49
ngoldbaum
ngoldbaum approved these changes Nov 3, 2026
View reviewed changes

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

Looks good now, thanks for your persistence here @shbhmbnsl1 and taking my feedback about pings.

Sorry, something went wrong.

Uh oh!

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

shbhmbnsl1 reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@ngoldbaum
ngoldbaum merged commit 651234f into numpy:main Nov 3, 2026
93 checks passed

Uh oh!

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

@github-project-automation github-project-automation Bot moved this from Needs Review to Complete in NF Sustaining Open Source Series 2026 Nov 3, 2026
@ngoldbaum ngoldbaum mentioned this pull request Nov 3, 2026
BUG: np.random.set_bit_generator and SeedSequence.spawn are not thread-safe #32063
Open
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Nov 4, 2026
@charris charris mentioned this pull request Nov 5, 2026
BUG: make SeedSequence.spawn thread safe (#32889) #33225
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Nov 5, 2026
charris pushed a commit that referenced this pull request Nov 6, 2026
@shbhmbnsl1 @charris
BUG: make SeedSequence.spawn thread safe (#32889)
6135947
charris added a commit that referenced this pull request Nov 6, 2026
@charris
Merge pull request #33225 from charris/backport-32889
dc5436c
BUG: make SeedSequence.spawn thread safe (#32889)
This was referenced Nov 7, 2026
BUG: SeedSequence backwards/forwards compatibility check #33244
Closed
MAINT: CI/testing failures against latest NumPy main branch, related to SeedSequence struct size change scipy/scipy#26640
Closed
@shbhmbnsl1 shbhmbnsl1 mentioned this pull request Nov 9, 2026
BUG: revert SeedSequence thread safety change to restore compatibility #33263
Merged
charris added a commit that referenced this pull request Nov 10, 2026
@charris
Merge pull request #33265 from numpy/revert-33225-backport-32889
a062c18
REV: "BUG: make SeedSequence.spawn thread safe (#32889)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ngoldbaum ngoldbaum ngoldbaum approved these changes
@lakshayg lakshayg Awaiting requested review from lakshayg
@seberg seberg Awaiting requested review from seberg
+1 more reviewer
@da-woods da-woods da-woods left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

00 - Bug sustain-2026 Issues reserved for NumFOCUS Sustaining Open Source Series 2026

Projects

NF Sustaining Open Source Series 2026
Status: Complete

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.

7 participants

@shbhmbnsl1 @ngoldbaum @seberg @lakshayg @da-woods @charris @ganesh-k13
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.