Fix UnboundLocalError in send_request when response not assigned #1718
+163
−5
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.
Fixes an intermittent
UnboundLocalErrorthat could occur insend_request()at the line checkingisinstance(response_or_error, JSONRPCError).Motivation and Context
We received a report of intermittent
UnboundLocalErrorexceptions insend_request():This can occur due to a race condition in anyio's
fail_aftercontext manager (anyio#589) where exceptions may be incorrectly suppressed, leaving theresponse_or_errorvariable unassigned while execution continues past the try/except block.The fix:
response_or_errortoNonebefore the try block_process_responsestatic method with a defensiveNonecheckEndOfStreamandClosedResourceErrorexceptions explicitlyHow Has This Been Tested?
_process_responsecovering all code pathsBreaking Changes
None. This is a defensive fix that handles edge cases more gracefully.
Types of changes
Checklist
Additional context
Closes #1717
The root cause is a race condition in anyio where
fail_after's cancel scope can incorrectly suppress exceptions when a timeout and successful completion happen simultaneously. While this was reportedly fixed in anyio 4.0 (PR #591), edge cases may remain. This fix adds defensive code to handle the scenario gracefully rather than crashing withUnboundLocalError.