Visitar URL original
Make `list` objects thread-safe in `--disable-gil` builds · Issue #112087 · python/cpython · GitHub
Skip to content

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

Description

@colesbury

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

Activity

  1. Lincoln-developer commented on Nov 16, 2023

    @Lincoln-developer
    Contributor

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

  2. colesbury commented on Nov 16, 2023

    @colesbury
    ContributorAuthor

    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.

  3. corona10 commented on Jan 5, 2024

    @corona10
    Member

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

  4. colesbury commented on Jan 5, 2024

    @colesbury
    ContributorAuthor

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

  5. Lincoln-developer commented on Jan 5, 2024

    @Lincoln-developer
    Contributor

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

  6. Lincoln-developer commented on Jan 5, 2024

    @Lincoln-developer
    Contributor

    My apologies for not delivering as expected,

  7. self-assigned this
    on Jan 6, 2024
  8. corona10 commented on Jan 6, 2024

    @corona10
    Member

    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
  9. added 3 commits that reference this issue on Jan 6, 2024
  10. corona10 commented on Jan 13, 2024

    @corona10
    Member
  11. 60 remaining items

  12. added 3 commits that reference this issue on Apr 1, 2024
  13. added 7 commits that reference this issue on Apr 17, 2024
  14. added 4 commits that reference this issue on Sep 2, 2024
  15. added a commit that references this issue on Jan 22, 2025
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