Skip to content

Prevent ThreadPrefetchDatasetIterator.close() deadlock when parent iterator is blocked - #1461

Closed
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1018969110
Closed

copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1018969110

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Prevent ThreadPrefetchDatasetIterator.close() deadlock when parent iterator is blocked

ThreadPrefetchDatasetIterator.close() previously joined the background prefetch thread before closing its parent iterator. When the prefetch thread was blocked inside parent.__next__() waiting on an upstream resource that only unblocks upon parent.close(), close() deadlocked indefinitely.

Make close() idempotent, signal both _closed_event and _prefetch_should_stop, clear buffered items, and close the parent iterator before joining the prefetch worker, check closed in _put_iterator_elements_in_buffer so an unblocked producer exits cleanly without dropping autotuned prefetch elements on buffer resize, and enqueue a closed sentinel after shutdown so any outer consumer blocked in _buffer.get() (such as nested ThreadPrefetch pipelines) unblocks cleanly.

Fixes #1372.

@copybara-service
copybara-service Bot force-pushed the test_1018969110 branch 3 times, most recently from 295bf69 to 4af46d8 Compare November 11, 2026 10:18
…iterator is blocked

`ThreadPrefetchDatasetIterator.close()` previously joined the background prefetch thread before closing its parent iterator. When the prefetch thread was blocked inside `parent.__next__()` waiting on an upstream resource that only unblocks upon `parent.close()`, `close()` deadlocked indefinitely.

Make `close()` idempotent, signal both `_closed_event` and `_prefetch_should_stop`, clear buffered items, and close the parent iterator before joining the prefetch worker, check `closed` in `_put_iterator_elements_in_buffer` so an unblocked producer exits cleanly without dropping autotuned prefetch elements on buffer resize, and enqueue a closed sentinel after shutdown so any outer consumer blocked in `_buffer.get()` (such as nested `ThreadPrefetch` pipelines) unblocks cleanly.

Fixes #1372.

PiperOrigin-RevId: 1018969110
@copybara-service copybara-service Bot closed this Nov 12, 2026
@copybara-service
copybara-service Bot deleted the test_1018969110 branch November 12, 2026 21:21
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.

ThreadPrefetch close() cannot cancel a producer blocked in parent.__next__()

1 participant

⌂ 换网址