Skip to content

list(set) should be atomic in the free-threaded build #116621

Closed

Description

@colesbury

We have code that constructs a list from a set, while the set may be concurrently updated. For example:

def _cleanup():
# check for processes which have finished
for p in list(_children):
if (child_popen := p._popen) and child_popen.poll() is not None:
_children.discard(p)

This is a fairly common pattern currently, but in the free-threaded build, this may lead to RuntimeError: Set changed size during iteration. We should make it so that list(set) locks the set to avoid the error and so that the list contains a consistent copy of the set.

For additional context, see the following code from the nogil-3.12 fork:

cc @corona10

Linked PRs

Activity

  1. colesbury commented on Mar 11, 2024

    @colesbury
    ContributorAuthor

    This is also important for test_multiprocessing_pool_circular_import in test_importlib.

  2. self-assigned this
    on Mar 11, 2024
  3. colesbury commented on Mar 11, 2024

    @colesbury
    ContributorAuthor

    There are a few other cases in nogil-3.12 that we probably want to handle atomically as well, because I think we use the same pattern where the calling code assumes list(some_type) is atomic for certain built-in types:

    • PyDictKeys_Check
    • PyDictValues_Check
    • PyDictItems_Check
  4. added a commit that references this issue on Mar 12, 2024
  5. added a commit that references this issue on Mar 12, 2024
  6. added a commit that references this issue on Mar 12, 2024
  7. added 3 commits that reference this issue on Mar 14, 2024
  8. added a commit that references this issue on Mar 19, 2024
  9. added 3 commits that reference this issue on Mar 20, 2024
  10. added 3 commits that reference this issue on Mar 25, 2024
  11. added 3 commits that reference this issue on Apr 17, 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