fix(agent): stop idempotency waiters from blocking thread-pool workers#2932
Merged
zastrowm merged 3 commits intoJun 24, 2026
Merged
Conversation
Contributor
Author
|
CC: @zastrowm |
zastrowm
reviewed
Jun 24, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
Author
|
@zastrowm . Have refactored basis your suggestions. |
zastrowm
previously approved these changes
Jun 24, 2026
Contributor
|
Assessment: Approve Clean, well-scoped fix. Replacing Review notes
Nicely done — the explanatory docstring on |
zastrowm
approved these changes
Jun 24, 2026
This was referenced Jun 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Duplicate agent invocations that share an
idempotency_token(THROW mode) wait for the in-flight primary to finish and then receive its result. That wait was implemented asasyncio.to_thread(event.wait), which parks a worker from the event loop's default thread pool for the entire duration of the primary - one parked worker per waiting duplicate, doing no work.This makes a retry storm dangerous when enough number of retries get triggered
min(32, cpu+4)workers), the primary can no longer obtain a worker for its ownasyncio.to_threadmodel call (e.g.BedrockModel). The primary then never completes, so it never wakes the waiters, and the event loop wedges permanently with no timeout. On an 8-core host this triggers at ~12 concurrent same-token calls on a shared loop.The fix removes the parked thread entirely: a waiting duplicate now registers an
asyncio.Futureand awaits it. On completion the primary resolves each waiter's future on its own loop viacall_soon_threadsafe. No waiter holds a thread, so duplicates can no longer starve the executor the primary depends on.Related Issues
Documentation PR
Not required — no public API or documented behavior change.
Type of Change
Bug fix
Public API Changes
None. From the caller's perspective behavior is unchanged: a duplicate still blocks until the primary finishes and then yields the same result (and still fires
callback_handlerwith it). The change is purely how the wait is implemented internally.Testing
Existing idempotency unit and integration tests pass unchanged; added async unit tests covering cross-thread future resolution, the register-after-settle race, and waiter cancellation. Separately verified in a resource-capped container that previously-hanging configurations now complete and that waiting duplicates park zero threads.
hatch run prepareChecklist