fix: prevent asyncio.Lock deadlock in AsyncTxn.do_request error handling #293
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.
Summary
Fixes a deadlock in the async client when an error occurs during
do_request().Root Cause: When an error occurs in
do_request(), the code callsself.discard()while still holdingself._lock. Sincediscard()also tries to acquireself._lockandasyncio.Lockis not reentrant, this causes a deadlock.The deadlock scenario:
do_request()acquiresself._lock(line 171)await self.discard()(line 221)discard()tries to acquireself._lock(line 460)do_request()Fix
_discard_internal()which doesn't acquire the lockdo_request()error handler to call_discard_internal()directlydiscard()to acquire lock then delegate to_discard_internal()Testing
Tested by reproducing the deadlock scenario:
Validation
This fix was reviewed by GPT-5 and Gemini 2.5 Pro, both confirmed:
_discard_internal) is idiomatic Python