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 `list` objects thread-safe in `--disable-gil` builds · Issue #112087 · python/cpython

Make list objects thread-safe in --disable-gil builds #112087

New issue
Copy link
New issue
Copy link
Closed
Closed
Make list objects thread-safe in --disable-gil builds#112087
Copy link
Assignees
corona10
Labels
3.13only security fixesonly security fixestopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement

Description

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

Feature or enhancement

I expect this to be implemented across multiple PRs.

For context, here is the change from the nogil-3.12 fork, but things might be done a bit differently in CPython 3.13: colesbury/nogil-3.12@df4c51f82b.

  • Most operations should acquire the list object lock using the critical section API.
  • Accessing a single element should optimistically avoid locking for performance
  • Iterators need some special handling
  • list.sort

Iterators

For performance reasons, we don't want to acquire locks while iterating over lists. This means that list iterators have a lesser notion of thread-safety: multiple threads concurrently using the same iterator should not crash but may revisit elements. We should make clear in the documentation that iterators are not generally thread-safe. From an implementation point of view, we should use relaxed atomics to update the it_index variable.

Additionally, we don't want to clear the it_seq reference to the list when the iterator is exhausted in --disable-gil builds. Doing so would pose thread-safety issues (in the "may segfault sense").

I don't expect this to pose any issues for real code. While it's fairly common to share lists between threads, it's rare to share the same iterator object between threads.

Linked PRs

  • gh-112087: Update list.{pop,clear,reverse,remove} to use CS #113764
  • gh-112087: Update list impl to be thread-safe with manual CS #113863
  • gh-112087: Remove duplicated critical_section #114268
  • gh-112087: Make list_repr and list_length to be thread-safe #114582
  • gh-112087: Make PyList_{Append,Size,GetSlice} to be thread-safe #114651
  • gh-112087: Make __sizeof__ and listiter_{len, next} to be threadsafe #114843
  • gh-112087: Make list_{count, index, contains} to be thread-safe. #114916
  • [3.12] gh-112087: Fix reduce logic for the empty reverse iterator for list #115471
  • [3.11] gh-112087: Fix reduce logic for the empty reverse iterator for list #115472
  • gh-112087: Make list_{concat, repeat, inplace_repeat, ass_item) to be thread-safe #115605
  • gh-112087: Make list_{subscript, ass_slice, slice} to be thread-safe #115854
  • gh-112087: Use QSBR technique for list_new/clear for free-thread build #115875
  • gh-112087: Make list_{slice, ass_slice, subscript} to be threadsafe #116233
  • gh-112087: Test fix for test_sched leak #116237
  • gh-112087: Update list_get_item_ref to optimistically avoid locking #116353
  • gh-112087: Store memory allocation information into _PyListArray #116529
  • gh-112087: Make list.sort to be thread-safe for PEP 703. #116553
  • gh-112087: Make list.extend(dict) behave atomically #117438

Activity

Next
  1. colesbury
    added
    type-featureA feature request or enhancementA feature request or enhancement
    3.13only security fixesonly security fixes
    topic-free-threading
    on Nov 14, 2023
  2. Lincoln-developer commented on Nov 16, 2023

    @Lincoln-developer
    Lincoln-developer
    on Nov 16, 2023
    Contributor
    More actions

    Hey, I would love to work on this issue if you don't mind, thanks.

  3. colesbury commented on Nov 16, 2023

    @colesbury
    colesbury
    on Nov 16, 2023
    ContributorAuthor
    More actions

    Thanks @Lincoln-developer! I'd suggest starting by transforming some of the functions to use the critical section API. Some functions that use Argument Clinic can just use the @critical_section directive. Other functions will need manual Py_BEGIN_CRITICAL_SECTION() and Py_END_CRITICAL_SECTION() calls. Feel free to tag me on a draft PR if you would like early feedback.

    Reacted by Angufibo Lincoln
  4. corona10 commented on Jan 5, 2024

    @corona10
    corona10
    on Jan 5, 2024
    Member
    More actions

    @colesbury Since this work has not be updated from last year November, can I assign this issue to myself?

    Reacted by Erlend E. Aasland
  5. colesbury commented on Jan 5, 2024

    @colesbury
    colesbury
    on Jan 5, 2024
    ContributorAuthor
    More actions

    @Lincoln-developer are you still working on this or can @corona10 take this over?

  6. Lincoln-developer commented on Jan 5, 2024

    @Lincoln-developer
    Lincoln-developer
    on Jan 5, 2024
    Contributor
    More actions

    I think he can take it on because l have taken longer than expected ,its okay

    Reacted by Sam Gross
  7. Lincoln-developer commented on Jan 5, 2024

    @Lincoln-developer
    Lincoln-developer
    on Jan 5, 2024
    Contributor
    More actions

    My apologies for not delivering as expected,

    Reacted by Donghee Na
  8. corona10
    self-assigned this
    on Jan 6, 2024
  9. corona10 commented on Jan 6, 2024

    @corona10
    corona10
    on Jan 6, 2024
    Member
    More actions

    TODO based on diff

    • PyList_Insert
    • PyList_SetItem
    • PyList_Append
    • list_repr
    • list_length
    • list_contains
    • list_item
    • list_slice
    • PyList_GetSlice
    • list_concat
    • list_repeat
    • _list_clear
    • list_ass_slice
    • list_inplace_repeat
    • list_ass_item
    • list_insert_impl
    • list_clear_impl
    • list_remove_impl
    • list_copy_impl
    • list_append
    • list_extend
    • list_pop_impl
    • list_reverse_impl
    • PyList_Reverse
    • PyList_AsTuple
    • list_index_impl
    • list_count
    • list_remove
    • list_richcompare
    • list___sizeof___impl
    • list_subscript
    • list_ass_subscript
    • listiter_next
    • listiter_len
    • listreviter_next
    • listreviter_len
    • listiter_reduce_general
    Reacted by Erlend E. Aasland
  10. corona10
    added 3 commits that reference this issue on Jan 6, 2024

    pythongh-112087: Update list.{pop,clear,reverse,remove} to use CS

    81b80db

    gh-112087: Update list.{pop,clear,reverse,remove} to use CS (gh-113764)

    a023bc2

    pythongh-112087: Update list impl to be thread-safe with manual CS

    10bbebd
  11. corona10 commented on Jan 13, 2024

    @corona10
    corona10
    on Jan 13, 2024
    Member
    More actions

    Not to forget: https://github.com/python/cpython/pull/113863/files#r1449145544

  12. 60 remaining items

  13. colesbury
    added 3 commits that reference this issue on Apr 1, 2024

    pythongh-112087: Make `list.extend(dict)` behave atomically

    a1fa3fb

    Merge branch 'pythongh-112087-dict' into nogil-integration

    71a9a85

    gh-112087: Make `list.extend(dict)` behave atomically (#117438)

    027fa2e
  14. diegorusso
    added 7 commits that reference this issue on Apr 17, 2024

    pythongh-112087: Make list_{concat, repeat, inplace_repeat, ass_item)…

    5bb45d1

    pythongh-112087: Use QSBR technique for list_new/clear for free-threa…

    664d810

    pythongh-112087: Make list_{slice, ass_slice, subscript} to be thread…

    77e36d1

    pythongh-112087: Update list_get_item_ref to optimistically avoid loc…

    a34ec0b

    pythongh-112087: Store memory allocation information into _PyListArray (

    6429975

    pythongh-112087: Make list.sort to be thread-safe for PEP 703. (pytho…

    e5130ab

    pythongh-112087: Make `list.extend(dict)` behave atomically (python#1…

    363da51
  15. Glyphack
    added 4 commits that reference this issue on Sep 2, 2024

    pythongh-112087: Update list.{pop,clear,reverse,remove} to use CS (py…

    32a3a35

    pythongh-112087: Update list impl to be thread-safe with manual CS (p…

    28eb863

    pythongh-112087: Remove duplicated critical_section (pythongh-114268)

    ea93f48

    pythongh-112087: Make list_repr and list_length to be thread-safe (py…

    8a3e7c5
  16. LukasWoodtli
    added a commit that references this issue on Jan 22, 2025

    pythongh-112087: Make list_{concat, repeat, inplace_repeat, ass_item)…

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

Metadata

Metadata

Assignees

  • @corona10
    corona10

Labels

3.13only security fixesonly security fixestopic-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.