Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make random.Random thread-safe in --disable-gil builds #112071

Closed
Tracked by #108219
colesbury opened this issue Nov 14, 2023 · 1 comment · Fixed by #112128
Closed
Tracked by #108219

Make random.Random thread-safe in --disable-gil builds #112071

colesbury opened this issue Nov 14, 2023 · 1 comment · Fixed by #112128
Assignees
Labels
3.13 bugs and security fixes extension-modules C modules in the Modules dir topic-free-threading type-feature A feature request or enhancement

Comments

@colesbury
Copy link
Contributor

colesbury commented Nov 14, 2023

Feature or enhancement

random.Random has mutable internal state. We should use the critical section API to make it thread-safe in --disable-gil builds.

For context, here is the change from the nogil-3.12 fork. colesbury/nogil-3.12@9bf62ffc4b. Note that we want to do things a bit differently in CPython 3.13:

  1. No need for an extra _PyMutex mutex in RandomObject
  2. Use the critical section API. Locking can be added around operations that use Argument Clinic by adding the @critical_section directive as the first line.
  3. Compared to nogil-3.12, we want to push the locking "up" to the around the methods. (See the above note 2).

Linked PRs

@colesbury colesbury added type-feature A feature request or enhancement 3.13 bugs and security fixes topic-free-threading labels Nov 14, 2023
@AlexWaygood AlexWaygood added the extension-modules C modules in the Modules dir label Nov 14, 2023
chgnrdv added a commit to chgnrdv/cpython that referenced this issue Nov 15, 2023
…ble-gil` builds

Guard `_random.Random` methods with critical section API locks by using `@critical_section` AC directive
@chgnrdv
Copy link
Contributor

chgnrdv commented Nov 15, 2023

The PR is done.

@colesbury colesbury assigned colesbury and chgnrdv and unassigned colesbury Nov 15, 2023
corona10 added a commit that referenced this issue Nov 28, 2023
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes extension-modules C modules in the Modules dir topic-free-threading type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants