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

Race condition in test_queue.test_shutdown_immediate_put_join #116631

Closed
colesbury opened this issue Mar 11, 2024 · 1 comment
Closed

Race condition in test_queue.test_shutdown_immediate_put_join #116631

colesbury opened this issue Mar 11, 2024 · 1 comment
Labels
tests Tests in the Lib/test dir topic-free-threading

Comments

@colesbury
Copy link
Contributor

colesbury commented Mar 11, 2024

There is a race condition in test_shutdown_immediate_put_join that leads to ValueError: task_done() called too many times. This happens frequently in the free-threaded build. It may also happen in the default build depending on when the GIL is released. You can reliably reproduce the issue by adding a short sleep at the start of Queue.task_done().

The problem is that we call q.task_done() in a thread concurrently with q.shutdown(immediate=True).

self.assertEqual(q.unfinished_tasks, nb)
for i in range(nb):
t = threading.Thread(target=q.task_done)
t.start()
threads.append(t)
q.shutdown(immediate)

The queue has one unfinished task. If, q.task_done() finishes first then everything is okay. However, if the q.shutdown(immediate=True) happens first, it will remove the single outstanding task and the q.task_done() will raise an exception.

cc @EpicWink

Linked PRs

@colesbury colesbury added tests Tests in the Lib/test dir topic-free-threading labels Mar 11, 2024
colesbury added a commit to colesbury/cpython that referenced this issue Mar 12, 2024
…oin`

The test case had a race condition: if `q.task_done()` was executed
after `shutdown(immediate=True)`, then it would raise an exception
because the immediate shutdown already emptied the queue. This happened
rarely with the GIL (due to the switching interval), but frequently in
the free-threaded build.
colesbury added a commit to colesbury/cpython that referenced this issue Mar 12, 2024
@EpicWink
Copy link
Contributor

CC @YvesDup

colesbury added a commit to colesbury/cpython that referenced this issue Mar 13, 2024
colesbury added a commit that referenced this issue Mar 13, 2024
…116670)

The test case had a race condition: if `q.task_done()` was executed
after `shutdown(immediate=True)`, then it would raise an exception
because the immediate shutdown already emptied the queue. This happened
rarely with the GIL (due to the switching interval), but frequently in
the free-threaded build.
vstinner pushed a commit to vstinner/cpython that referenced this issue Mar 20, 2024
…oin` (python#116670)

The test case had a race condition: if `q.task_done()` was executed
after `shutdown(immediate=True)`, then it would raise an exception
because the immediate shutdown already emptied the queue. This happened
rarely with the GIL (due to the switching interval), but frequently in
the free-threaded build.
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
…oin` (python#116670)

The test case had a race condition: if `q.task_done()` was executed
after `shutdown(immediate=True)`, then it would raise an exception
because the immediate shutdown already emptied the queue. This happened
rarely with the GIL (due to the switching interval), but frequently in
the free-threaded build.
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…oin` (python#116670)

The test case had a race condition: if `q.task_done()` was executed
after `shutdown(immediate=True)`, then it would raise an exception
because the immediate shutdown already emptied the queue. This happened
rarely with the GIL (due to the switching interval), but frequently in
the free-threaded build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir topic-free-threading
Projects
None yet
Development

No branches or pull requests

2 participants