Skip to content

Make queue.SimpleQueue thread-safe in --disable-gil builds #113884

Description

@mpage

Feature or enhancement

Proposal:

Instances of queue.SimpleQueue have mutable state that is not safe to mutate without the GIL. The previous implementation (in 3.12) was colesbury/nogil-3.12@7e60a01aee.

@colesbury - You mentioned in chat that porting the 3.12 approach wouldn't be straightforward. Can you say more?

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. colesbury commented on Jan 9, 2024

    @colesbury
    Contributor

    @mpage, after looking it over again, I think the nogil-3.12 approach is mostly the right direction with a few modifications:

    1. Replace the manual _PyMutex_lock()/unlock() calls with critical sections and remove the _PyMutex mutex field. Since, the code already uses Argument Clinic (AC), this is mostly just a matter of adding the @critical_section to the AC input. This will avoid the extra lock operations in the default build, which can rely on the GIL.
    2. The parking lot API changed slightly; it's not a huge difference. For example, the unpark operation now takes a callback

    This means we will go from a SimpleQueue implementation built around list to run that more explicitly manages a buffer. I think that's worthwhile here; otherwise I think handling the synchronization across both the list operations and the containing SimpleQueue will be too messy.

  2. added a commit that references this issue on Jan 19, 2024
  3. added a commit that references this issue on Jan 22, 2024
  4. added a commit that references this issue on Jan 23, 2024
  5. erlend-aasland commented on Jan 23, 2024

    @erlend-aasland
    Contributor

    Thank you, @mpage and @colesbury 🙏

  6. added 2 commits that reference this issue on Feb 11, 2024
  7. added 2 commits that reference this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions