Skip to content

gh-158677: Fix Pool.imap(buffersize=...) blocking other tasks on the pool - #158792

Closed
oyiakoumis wants to merge 1 commit into
python:mainfrom
oyiakoumis:fix-imap-buffersize-deadlock-park
Closed

oyiakoumis wants to merge 1 commit into
python:mainfrom
oyiakoumis:fix-imap-buffersize-deadlock-park

Conversation

@oyiakoumis

@oyiakoumis oyiakoumis commented Oct 4, 2026 •

Copy link
Copy Markdown

Problem

With buffersize, imap() and imap_unordered() throttle task submission by blocking on a semaphore inside the pool's task handler thread. The pool has only one such thread, so while it waits for the iterator to be consumed, no other task on the pool is submitted. This deadlocks, for example, zip() over two buffered imap() iterators.

Fix

The task handler no longer waits. When the buffer of an iterator is full, it sets that iterator's tasks aside and moves on to the next job. When a result is consumed, next() puts them back on the task queue.

The input iterable is still consumed by the task handler thread, so nothing else changes: imap() returns immediately and next() never waits for the iterable.

close() is unchanged: a partially consumed iterator still stops early. That is gh-158675 and is left out of this PR.

Alternative considered

The issue suggested submitting the next task from next(), as Executor.map(buffersize=...) does. I tried that approach first but moved away from it because the iterable would then be consumed in the caller's thread. As a result, next() could hold a ready result while waiting for the next input item, causing a deadlock if producing that item depends on the result being handled.

Tests

  • Other tasks (apply_async(), a second buffered imap()) run while a buffered iterator is waiting to be consumed. This test fails without the fix.
  • An exception raised by the iterable is propagated through next(). This case was not previously covered with buffersize.

Tested on macOS. Not tested on Linux, Windows or the free-threaded build.

No NEWS entry, since buffersize is new in 3.16 and not released yet.

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

…n the pool

With buffersize, the task handler thread waited on a semaphore when the
buffer of an imap() or imap_unordered() iterator was full. The pool has
only one such thread, so no other task was submitted until the iterator
was consumed, which could deadlock.

The task handler no longer waits. When the buffer is full, it sets the
iterator's tasks aside and moves on. They are put back on the task queue
when a result is consumed.
@bedevere-app

bedevere-app Bot commented Oct 4, 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 4, 2026

Copy link
Copy Markdown
Member

This is breaking Linux and Windows CI.

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

In the issue you said:

I would like to work on this. Before I write a patch, I would like to know which direction maintainers prefer.

But you did not wait for anything. Really, don't use automated agents for making fixes if they do not leave the time for maintainers to even reply.

@oyiakoumis

Copy link
Copy Markdown
Author

Sorry @picnixz. I opened this PR as a draft to collaborate with @PrakharAgarwal17 on a potential patch, and I didn't realize that a draft would still create noise. I am closing it for now and will continue on the issue.

@oyiakoumis oyiakoumis closed this Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants