Skip to content

gh-155477: Fix multiprocessing.Pool deadlock on close() with a buffersize imap - #155478

Merged
serhiy-storchaka merged 2 commits into
python:mainfrom
tonghuaroot:fix-pool-buffersize-close
Aug 16, 2026
Merged

gh-155477: Fix multiprocessing.Pool deadlock on close() with a buffersize imap#155478
serhiy-storchaka merged 2 commits into
python:mainfrom
tonghuaroot:fix-pool-buffersize-close

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

The buffersize variant of Pool.imap()/imap_unordered() throttles the task
generator on a semaphore. close() did not release those semaphores (only
terminate() did), so a partially-consumed iterator left the task handler
blocked in sema.acquire() and join() deadlocked. Release the buffersize
semaphores in close(), and stop the task generator once the pool leaves the
RUN state. Introduced in gh-64192.

…buffersize imap

close() did not release the buffersize semaphores that throttle the task
generator, so a partially-consumed imap left the task handler blocked and
join() deadlocked. Release them in close() and stop the generator once the
pool leaves the RUN state.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR. In general LGTM, I have only few nitpicks.

This is a fix for an unrealized feature, so NEWS is not required. It will only confuse late readers.

Comment thread Lib/multiprocessing/pool.py Outdated
self._change_notifier.put(None)
# Wake any task generator throttled on a buffersize semaphore so
# it observes the CLOSE state and stops submitting.
for sema in tuple(self._taskqueue_buffersize_semaphores):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why tuple() instead of list() or .copy()?

The fix is for an unrealized feature, so no NEWS is needed.
@tonghuaroot

Copy link
Copy Markdown
Contributor Author

Done in 306d869: switched to list() and dropped the NEWS.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) August 16, 2026 13:23
@serhiy-storchaka
serhiy-storchaka merged commit 70fdc96 into python:main Aug 16, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants