Fix get_ip_address() returning the last interface instead of the first#3624
Open
Matcinis wants to merge 1 commit into
Open
Fix get_ip_address() returning the last interface instead of the first#3624Matcinis wants to merge 1 commit into
Matcinis wants to merge 1 commit into
Conversation
The outer loop over network interfaces had no break, so ip_address was overwritten by every subsequent up/non-loopback interface with a matching address family. On hosts with Docker, this meant the bridge IP (e.g. 172.18.0.1) was returned instead of the actual LAN address, because virtual interfaces can sort after the real one in dict order. Add a break once a match is found so the function returns the first qualifying interface, plus regression tests covering the Docker case, loopback/down-interface skipping, and the no-match case. Fixes nicolargo#3617 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015NJfi2d6yvmmDzvPmm4eof
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.
What
get_ip_address()inglances/globals.pywas returning the last matching interface's address instead of the first one, because the outer loop overpsutil.net_if_stats().items()had nobreak— only the inner loop over addresses for a single interface did.Why it matters
On hosts with Docker (or any virtual bridge interface), this silently returns the bridge IP (e.g.
172.18.0.1) instead of the real LAN address (e.g.192.168.0.150), whenever the virtual interface happens to sort after the real one in dict order.Fix
Break out of the outer loop as soon as a qualifying address has been found for an interface, so the function returns the first up/non-loopback interface instead of the last.
Tests
Added
tests/test_globals.pywith a regression test that reproduces the exact Docker-bridge scenario from the issue (fails on the old code, passes after the fix — verified locally by temporarily reverting the fix), plus coverage for loopback/down-interface skipping and the no-match case.Ran
ruff check/ruff format --checkon both changed files — no issues.Fixes #3617