Skip to content

Fix get_ip_address() returning the last interface instead of the first#3624

Open
Matcinis wants to merge 1 commit into
nicolargo:developfrom
Matcinis:fix-get-ip-address-first-match
Open

Fix get_ip_address() returning the last interface instead of the first#3624
Matcinis wants to merge 1 commit into
nicolargo:developfrom
Matcinis:fix-get-ip-address-first-match

Conversation

@Matcinis

Copy link
Copy Markdown

What

get_ip_address() in glances/globals.py was returning the last matching interface's address instead of the first one, because the outer loop over psutil.net_if_stats().items() had no break — 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.py with 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 --check on both changed files — no issues.

Fixes #3617

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IP plugin displays wrong interface: outer loop in get_ip_address() never breaks

1 participant