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-113884: Make queue.SimpleQueue thread-safe in --disable-gil builds #114161

Merged
merged 10 commits into from
Jan 23, 2024

Conversation

mpage
Copy link
Contributor

@mpage mpage commented Jan 17, 2024

Make queue.SimpleQueue thread-safe when the GIL is disabled. Queue state is protected by the per-object lock; thread suspension now uses the low-level PyParkingLot abstraction.

@erlend-aasland
Copy link
Contributor

Could you split it up so the ring buffer refactoring is done first, then a follow-up PR to make it thread-safe?

@mpage
Copy link
Contributor Author

mpage commented Jan 17, 2024

Could you split it up so the ring buffer refactoring is done first, then a follow-up PR to make it thread-safe?

@erlend-aasland I think it makes the most sense for these commits to be merged together as part of a single PR. The commits in this PR are structured in a way that they can be reviewed independently. How about we start by reviewing the first commit, which is the ring buffer refactoring, then go from there?

@mpage
Copy link
Contributor Author

mpage commented Jan 17, 2024

Tagging @colesbury

Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

Other than the threshold for shrinking and a few minor comments, this looks good to me.

Modules/_queuemodule.c Outdated Show resolved Hide resolved
Modules/_queuemodule.c Outdated Show resolved Hide resolved
Modules/_queuemodule.c Outdated Show resolved Hide resolved
Modules/_queuemodule.c Outdated Show resolved Hide resolved
Modules/_queuemodule.c Outdated Show resolved Hide resolved
Modules/_queuemodule.c Outdated Show resolved Hide resolved
@rhettinger rhettinger removed their request for review January 18, 2024 00:53
@mpage mpage requested a review from colesbury January 18, 2024 02:45
@erlend-aasland
Copy link
Contributor

I think it makes the most sense for these commits to be merged together as part of a single PR. The commits in this PR are structured in a way that they can be reviewed independently. How about we start by reviewing the first commit, which is the ring buffer refactoring, then go from there?

AFAICS, the ring buffer refactoring makes for a nice stand-alone PR. It will result in a cleaner Git history, and it will make bisecting easier when hunting for bugs.

@mpage
Copy link
Contributor Author

mpage commented Jan 18, 2024

AFAICS, the ring buffer refactoring makes for a nice stand-alone PR.

I disagree but will split the PR in the interest of moving things forward.

Do you have any suggestions for the appropriate "size" of a PR? This will help me avoid having to split PRs in the future. I've been operating under the assumption that a PR should roughly correspond to a single, self-contained "feature" that stands on its own. I don't think the refactoring fits this model, as it wouldn't make sense to do without the other changes in the PR.

@mpage
Copy link
Contributor Author

mpage commented Jan 18, 2024

Split the ring buffer refactoring out into #114259. Putting this in draft until that PR is merged.

@mpage mpage marked this pull request as draft January 18, 2024 21:01
@erlend-aasland
Copy link
Contributor

Do you have any suggestions for the appropriate "size" of a PR? This will help me avoid having to split PRs in the future. I've been operating under the assumption that a PR should roughly correspond to a single, self-contained "feature" that stands on its own. I don't think the refactoring fits this model, as it wouldn't make sense to do without the other changes in the PR.

We normally do not mix refactorings and features. Most refactorings are done in order to make the code more readable, and/or structure the code in a more maintainable way. Those are qualities that stand on their own, and can justify a single (or multiple) PRs.

@erlend-aasland
Copy link
Contributor

As for PR size, I think it is good to try and keep the diff as small as possible (we don't like churn), and keep the number of changed lines within a couple of hundred lines. (Of course, generated files do not count.)

@mpage
Copy link
Contributor Author

mpage commented Jan 19, 2024

I've rebased this against main after the ring buffer refactoring was merged. @colesbury and @erlend-aasland would you please have another look?

Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

@mpage - you probably want to mark the PR as "Ready for review" (i.e., not a "Draft")

This LGTM other than one minor comment.

Modules/_queuemodule.c Outdated Show resolved Hide resolved
@mpage mpage marked this pull request as ready for review January 19, 2024 23:53
Copy link
Contributor

@erlend-aasland erlend-aasland left a comment

Choose a reason for hiding this comment

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

Thanks. The parking lot API still feels little bit uncomfortable for me, but AFAICS, this looks good. Perhaps a comment explaining how the hand-off data structure relates to the parking lot API could be beneficial for future readers of the code. I also find the maybe_unparked_thread oddly named. OTOH, I don't have a better suggestion :)

Modules/_queuemodule.c Show resolved Hide resolved
Modules/_queuemodule.c Outdated Show resolved Hide resolved
Modules/_queuemodule.c Show resolved Hide resolved
@erlend-aasland
Copy link
Contributor

@mpage, I applied my minor suggestions. Let me know what you think of #114161 (comment).

erlend-aasland and others added 2 commits January 23, 2024 12:05
Rename parking lot callback to better reflect what it does.
More precise type for whether handoff occurred.
@mpage
Copy link
Contributor Author

mpage commented Jan 23, 2024

@erlend-aasland - I think I've addressed all the comments now, can you take another look?

Copy link
Contributor

@erlend-aasland erlend-aasland left a comment

Choose a reason for hiding this comment

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

Thank you so much, this is very nice!

@erlend-aasland erlend-aasland merged commit 925907e into python:main Jan 23, 2024
34 checks passed
@mpage mpage deleted the gh-113884-ft-simplequeue branch January 29, 2024 18:06
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…isabled (python#114161)

* use the ParkingLot API to manage waiting threads
* use Argument Clinic's critical section directive to protect queue methods
* remove unnecessary overflow check

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@@ -164,7 +167,7 @@ RingBuf_Put(RingBuf *buf, PyObject *item)
return -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to decref item here isn't it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think so

Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…isabled (python#114161)

* use the ParkingLot API to manage waiting threads
* use Argument Clinic's critical section directive to protect queue methods
* remove unnecessary overflow check

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants