Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
colesbury opened this issue Mar 11, 2024 · 2 comments · Fixed by #116888
Closed

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

colesbury opened this issue Mar 11, 2024 · 2 comments · Fixed by #116888
Assignees

Comments

@colesbury
Copy link
Contributor

colesbury commented Mar 11, 2024

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

@colesbury
Copy link
Contributor Author

This is also important for test_multiprocessing_pool_circular_import in test_importlib.

@corona10 corona10 self-assigned this Mar 11, 2024
@colesbury
Copy link
Contributor Author

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

corona10 added a commit to corona10/cpython that referenced this issue Mar 12, 2024
colesbury added a commit to colesbury/cpython that referenced this issue Mar 12, 2024
@colesbury colesbury reopened this Mar 13, 2024
corona10 added a commit to corona10/cpython that referenced this issue Mar 14, 2024
corona10 added a commit to corona10/cpython that referenced this issue Mar 16, 2024
@corona10 corona10 linked a pull request Mar 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants