GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Make collections.deque thread-safe in `--disable-gil` builds · Issue #112050 · python/cpython

Make collections.deque thread-safe in --disable-gil builds #112050

New issue
Copy link
New issue
Copy link
Closed
Closed
Make collections.deque thread-safe in --disable-gil builds#112050
Copy link
Assignees
mpageHels15
Labels
3.13only security fixesonly security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement

Description

@colesbury
colesbury
opened on Nov 13, 2023
Issue body actions

Feature or enhancement

The collections.deque object has mutable internal state that would not be thread-safe without the GIL. We should use the critical section API to make it thread-safe. I think it might be cleanest to first convert most of the deque implementation to Argument Clinic and use the @critical_section directive rather than writing the critical sections manually.

Mostly for my own reference, here is the implementation from nogil-3.12: colesbury/nogil-3.12@f1e4742eaa. That implementation did not use the critical section API, which made it more complicated.

Depends on: #111903

Linked PRs

  • gh-112050: Make collections.deque thread-safe in free-threaded builds #113830
  • gh-112050: Convert most methods on collections.deque to use Argument Clinic #113963
Reacted by Erlend E. Aasland

Activity

  1. colesbury
    added
    type-featureA feature request or enhancementA feature request or enhancement
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    3.13only security fixesonly security fixes
    topic-free-threading
    on Nov 13, 2023
  2. Hels15 commented on Nov 21, 2023

    @Hels15
    Hels15
    on Nov 21, 2023
    Contributor
    More actions

    Can I work on this or someone is already assigned? @colesbury

  3. colesbury
    assigned
    Hels15
    on Nov 27, 2023
  4. colesbury commented on Nov 27, 2023

    @colesbury
    colesbury
    on Nov 27, 2023
    ContributorAuthor
    More actions

    Sure - nobody else has started work on this yet.

  5. mpage commented on Dec 18, 2023

    @mpage
    mpage
    on Dec 18, 2023
    Contributor
    More actions

    @Hels15 - Would you mind if I took this over?

  6. Hels15 commented on Dec 18, 2023

    @Hels15
    Hels15
    on Dec 18, 2023
    Contributor
    More actions

    @Hels15 - Would you mind if I took this over?

    Feel free to work on this. I haven't had much time lately, unfortunately.

  7. colesbury
    assigned
    mpage
    on Dec 18, 2023
  8. mpage commented on Dec 21, 2023

    @mpage
    mpage
    on Dec 21, 2023
    Contributor
    More actions

    @colesbury - Have you run into any TOCTTOU / stale data bugs when making containers thread-safe? One thing that comes to mind as I'm converting this is that we have to be careful to revalidate any invariants that were established (or reload any fields) across any operations that may release the container's lock (implicitly if we're using critical sections). I guess these could still happen with the GIL, so maybe we're not actually any worse off.

  9. colesbury commented on Dec 21, 2023

    @colesbury
    colesbury
    on Dec 21, 2023
    ContributorAuthor
    More actions

    @mpage - The most common thing is you want to avoid starting a nested critical section on the same object. That's both slow and can implicitly end the critical section (invalidating assumptions). Other than that, it's basically the same concerns as with the GIL. You have to be careful with, for example, Py_DECREF calls that may implicitly release the gil/release the containers lock.

    I think it's a good strategy to push the critical sections to the outer-most functions. That's easiest with Argument Clinic because you can generate the critical section calls as part of the binding code with the @critical_section directive. It's may be worth first converting deque to use argument clinic -- it'll probably be cleaner and easier to understand that sprinkling Py_BEGIN_CRITICAL_SECTION() calls all over the place.

    Reacted by Erlend E. Aasland
  10. mpage commented on Dec 21, 2023

    @mpage
    mpage
    on Dec 21, 2023
    Contributor
    More actions

    It's may be worth first converting deque to use argument clinic...

    Oh, yeah, that's the route I'm going (as suggested in the issue). I was mostly wondering what new classes of issues we need to pay attention to when making existing code thread-safe without the GIL. With the GIL you still have to be careful about revalidating assumptions across code that could release the GIL. If we push critical sections out to the binding functions using clinic, then I think the places where the critical section / container lock can be released are basically the same as where the GIL could be released, so making existing functions thread-safe using this strategy won't require substantial rewrites to the code. Anyway, I'm sure this is stuff you've already thought about and why you developed critical sections and the clinic strategy, but writing it out helps clarify my understanding, so thanks for humoring me :)

    Reacted by Sam Gross
  11. erlend-aasland
    added a commit that references this issue on Jan 29, 2024

    gh-112050: Adapt collections.deque to Argument Clinic (#113963)

    c87233f
  12. aisk
    added a commit that references this issue on Feb 11, 2024

    pythongh-112050: Adapt collections.deque to Argument Clinic (python#1…

    307afee
  13. erlend-aasland
    added a commit that references this issue on Feb 15, 2024

    gh-112050: Make collections.deque thread-safe in free-threaded builds (…

    dc978f6
  14. erlend-aasland
    closed this as completedon Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @Hels15
    Hels15
  • @mpage
    mpage

Labels

3.13only security fixesonly security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.