Fix UnboundLocalError in send_request when response not assigned#1718
Open
Fix UnboundLocalError in send_request when response not assigned#1718
Conversation
Fixes an intermittent UnboundLocalError that could occur in send_request() when anyio's fail_after context manager incorrectly suppresses exceptions due to a race condition (anyio#589). Changes: - Initialize response_or_error to None before the try block - Add _process_response static method with defensive None check - Handle EndOfStream and ClosedResourceError exceptions explicitly - Use try/except/else structure for cleaner control flow - Add unit tests for _process_response Closes #1717
bf42fa1 to
ee2a010
Compare
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.
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.