Skip to content

Make set thread-safe in --disable-gil builds #112069

Description

@colesbury

Feature or enhancement

The set object is not currently thread-safe in --disable-gil builds. We should make it thread-safe by using the "critical section" API. to acquire the per-object locks around operations. There should be no effective change in the default build (i.e., with GIL) because critical sections are no-ops in the default build.

Notes:

  1. Unlike dict and list, I don't think it's worth the complexity to try to "optimistically avoid locking" around any set operation (except set_len). We could consider doing this in the future if there is a performance justification, but not for 3.13.
  2. set_len can avoid locking and instead use relaxed atomics for reading the "used" field. Note that writes to "used" should then also use relaxed atomics.
  3. Some operations require locking two containers (like set_merge). Some of these will need refactorings so that the critical sections macros can be added in the correct places.

For context, here is the change from the nogil-3.12 fork: colesbury/nogil-3.12@4ca2924f0d. Note that the critical section API is slightly changed in 3.13 from nogil-3.12; In 3.13 Py_BEGIN_CRITICAL_SECTION takes a PyObject instead of a PyMutex.

TODO:

Linked PRs

Activity

  1. tomasr8 commented on Nov 16, 2023

    @tomasr8
    Member

    Hi again! This one looks more challenging than the hashlib one but I'd like to try anyway :)

  2. colesbury commented on Nov 16, 2023

    @colesbury
    ContributorAuthor

    Thanks @tomasr8!

  3. DinoV commented on Jan 4, 2024

    @DinoV
    Contributor

    hi @tomasr8, just curious if you're still planning on working on this?

  4. tomasr8 commented on Jan 5, 2024

    @tomasr8
    Member

    Hi! Yes, I took a break for Christmas but I have it almost working, just need to fix some tests. I could open a draft PR this weekend

  5. added a commit that references this issue on Feb 8, 2024
  6. added a commit that references this issue on Feb 14, 2024
  7. added 2 commits that reference this issue on Mar 8, 2024
  8. added a commit that references this issue on Mar 25, 2024
  9. added a commit that references this issue on Apr 16, 2024
  10. corona10 commented on Apr 16, 2024

    @corona10
    Member

    @colesbury I am going to work on _PySet_NextEntry this week, considering the caller-side usage.

  11. self-assigned this
    on Apr 16, 2024
  12. added a commit that references this issue on Apr 16, 2024
  13. added 2 commits that reference this issue on Apr 17, 2024
  14. added 7 commits that reference this issue on Apr 17, 2024
  15. corona10 commented on Apr 25, 2024

    @corona10
    Member

    @colesbury Now we can close the issue right? Or more thing is left?

  16. colesbury commented on Apr 25, 2024

    @colesbury
    ContributorAuthor

    Yeah, I think so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions