fix: prevent asyncio.Lock deadlock in AsyncTxn.do_request error handling#293
Closed
shaunpatterson wants to merge 2 commits intodgraph-io:mainfrom
Closed
fix: prevent asyncio.Lock deadlock in AsyncTxn.do_request error handling#293shaunpatterson wants to merge 2 commits intodgraph-io:mainfrom
shaunpatterson wants to merge 2 commits intodgraph-io:mainfrom
Conversation
When an error occurs in do_request(), the code was calling self.discard() while still holding self._lock. Since discard() also tries to acquire self._lock and asyncio.Lock is not reentrant, this caused a deadlock. The fix: - Extract discard logic into _discard_internal() which doesn't acquire lock - Update do_request() error handler to call _discard_internal() directly - Update public discard() to acquire lock then delegate to _discard_internal() - Add defensive assertion to verify lock is held when calling internal method This was discovered when using the async client with queries that triggered schema errors - the error handling path would deadlock indefinitely. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Minor formatting fixes: - Add missing blank lines after docstrings - Consolidate multi-line function signature to single line Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
4 tasks
Contributor
|
Good catch. I opened a new PR (#296) with the changes needed for us to be able to approve and merge:
Going to close this PR in favor of the other, and we'll cut a new patch release that includes the fix ASAP. Thanks! |
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.
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