fix: correct exception handling in get_external_ip fallback chain#3311
Open
Grizouforever wants to merge 2 commits intolatent-to:masterfrom
Open
fix: correct exception handling in get_external_ip fallback chain#3311Grizouforever wants to merge 2 commits intolatent-to:masterfrom
Grizouforever wants to merge 2 commits intolatent-to:masterfrom
Conversation
The fallback chain in get_external_ip() catches ExternalIPNotFound but none of the code inside the try blocks raises that exception. The actual exceptions (requests.RequestException, AssertionError, OSError, json.JSONDecodeError, etc.) propagate unhandled, preventing fallthrough to the next IP provider. Changes: - Replace all `except ExternalIPNotFound` with `except Exception` to properly catch real exceptions and allow fallthrough - Replace deprecated `os.popen()` calls with `subprocess.run()` - Replace bare `assert` validation with direct `ip_to_int()` calls which raise on invalid input regardless of -O flag Fixes latent-to#3309
The implementation in bittensor/utils/networking.py was updated in this
PR to replace os.popen("curl ...") with subprocess.run(...), but the
test test_get_external_ip_os_request_urllib_broken still mocked os.popen
which no longer intercepts any calls. Update the test to mock
subprocess.run and bittensor.utils.networking.urllib_request.urlopen so
all fallback paths are properly blocked and the expected ExternalIPNotFound
exception is raised.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
|
Read CONTRIBUTING |
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 #3309
The fallback chain in
get_external_ip()catchesExternalIPNotFound, but none of the code inside the try blocks actually raises that exception. Real exceptions (requests.RequestException,AssertionError,OSError,json.JSONDecodeError, etc.) propagate unhandled, preventing fallthrough to the next IP provider. This means if the first provider (AWS checkip) fails, the function crashes instead of trying the remaining 5 providers.Changes
bittensor/utils/networking.py:except ExternalIPNotFoundwithexcept Exceptionto properly catch real exceptionsos.popen()calls withsubprocess.run()(Python 3.0+ recommended)assert isinstance(...)validation with directip_to_int()calls that raise on invalid input regardless of-Ooptimization flagContext
PR #3262 previously attempted this fix but was closed due to being based on the wrong branch and author not responding to review. This PR is based on
master(the current default branch) and incorporates the additional improvements suggested.