Skip to content

gh-158675: Fix Pool.imap(buffersize=...) dropping results after close() - #158676

Open
oyiakoumis wants to merge 3 commits into
python:mainfrom
oyiakoumis:fix-imap-buffersize-close
Open

oyiakoumis wants to merge 3 commits into
python:mainfrom
oyiakoumis:fix-imap-buffersize-close

Conversation

@oyiakoumis

Copy link
Copy Markdown

With buffersize, calling close() before the imap() / imap_unordered() iterator was exhausted made the iterator stop early without an error. The items not yet submitted to the workers were dropped.

This came from the fix for gh-155477 (gh-155478): close() woke the throttled task generator, and the generator stopped as soon as the pool was no longer in the RUN state.

Changes

  • close() no longer releases the buffersize semaphores. The task generator keeps submitting items as results are consumed, so nothing is dropped.
  • join() now sets a flag and releases the semaphores. From then on, the generator submits the remaining items without throttling. This keeps multiprocessing.Pool.imap(buffersize=...) deadlocks on close()/join() after partial consumption #155477 fixed: close() + join() on a partially consumed iterator does not hang.
  • The task generator stops early only when the pool is terminated.

Behaviour after this change

Call Remaining items buffersize limit
close() submitted as results are consumed still applies
join() all submitted no longer applies
terminate() dropped not relevant

One consequence: close() + join() with buffersize on an infinite iterable no longer returns, which matches imap() without buffersize.

Tests and docs

No NEWS entry, because buffersize is new in 3.16 and has not been released (same as gh-155478).

I used Claude Code (Claude Opus 5.5, effort: Medium) to help investigate the bug and develop the fix, tests, and whatsnew entry. I reviewed, understood, modified, and tested the resulting changes myself.

@oyiakoumis
oyiakoumis requested a review from gpshead as a code owner October 3, 2026 19:15
@python-cla-bot

python-cla-bot Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Oct 3, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@read-the-docs-community

read-the-docs-community Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Comment thread Lib/multiprocessing/pool.py
@oyiakoumis
oyiakoumis requested a review from picnixz October 9, 2026 16:32
@bedevere-app

bedevere-app Bot commented Oct 18, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz

picnixz commented Oct 18, 2026

Copy link
Copy Markdown
Member

Please add a NEWS entry.

@oyiakoumis

Copy link
Copy Markdown
Author

Please add a NEWS entry.

@picnixz Are you sure? buffersize hasn't been released yet, so I thought the skip news label would be appropriate here. Happy to add a NEWS entry if you prefer, though!

@picnixz

picnixz commented Oct 18, 2026

Copy link
Copy Markdown
Member

We released 3.16.0alpha so if it has been in alpha 1, we need a NEWS entry

@oyiakoumis

Copy link
Copy Markdown
Author

@picnixz my bad. added.

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