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

gh-120974: Make _asyncio._enter_task atomic in the free-threaded build #122138

Merged
merged 1 commit into from
Jul 23, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Jul 22, 2024

Use PyDict_SetDefaultRef to set the current task in a single operation under the dictionary's lock.

…d build

Use `PyDict_SetDefaultRef` to set the current task in a single operation
under the dictionary's lock.
@colesbury
Copy link
Contributor Author

Still need to address leave_task and swap_current_task, but this was the easiest function to start with.

The goal with this PR is thread-safety in the free-threaded build, but it also seems to slightly improve performance of enter_task.

Here's a little microbenchmark I'm using: https://gist.github.com/colesbury/ece2f1a5cbc4c50f86c8634744a9badc.
BEFORE: ~102 ns per enter_task and leave_task call pair
AFTER: ~92 ns per call pair

Copy link
Contributor

@kumaraditya303 kumaraditya303 left a comment

Choose a reason for hiding this comment

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

LGTM

@kumaraditya303 kumaraditya303 merged commit 47847aa into python:main Jul 23, 2024
40 checks passed
@miss-islington-app
Copy link

Thanks @colesbury for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 23, 2024
…d build (pythonGH-122138)

Use `PyDict_SetDefaultRef` to set the current task in a single operation
under the dictionary's lock.
(cherry picked from commit 47847aa)

Co-authored-by: Sam Gross <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jul 23, 2024

GH-122152 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jul 23, 2024
kumaraditya303 pushed a commit that referenced this pull request Jul 23, 2024
…ed build (GH-122138) (#122152)

gh-120974: Make _asyncio._enter_task atomic in the free-threaded build (GH-122138)

Use `PyDict_SetDefaultRef` to set the current task in a single operation
under the dictionary's lock.
(cherry picked from commit 47847aa)

Co-authored-by: Sam Gross <[email protected]>
@colesbury colesbury deleted the gh-120974-asyncio-enter-task branch July 23, 2024 14:36
nohlson pushed a commit to nohlson/cpython that referenced this pull request Jul 24, 2024
…d build (python#122138)

Use `PyDict_SetDefaultRef` to set the current task in a single operation
under the dictionary's lock.
nohlson pushed a commit to nohlson/cpython that referenced this pull request Jul 24, 2024
…d build (python#122138)

Use `PyDict_SetDefaultRef` to set the current task in a single operation
under the dictionary's lock.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants