Skip to content

Remember that a connections probe was disabled - #3732

Merged
nicolargo merged 1 commit into
nicolargo:developfrom
datrixlab:fix/connections-disable-sticks
Sep 12, 2026
Merged

nicolargo merged 1 commit into
nicolargo:developfrom
datrixlab:fix/connections-disable-sticks

Conversation

@datrixlab

Copy link
Copy Markdown
Contributor

Description

The connections plugin stops using a probe that fails — psutil.net_connections() raising, or the nf_conntrack files not being present — and records that by writing net_connections_enabled / nf_conntrack_enabled into stats.

But update() starts every refresh with stats = self.get_init_value(), which is copy.copy(self.stats_init_value), and stats_init_value has both flags True (glances/plugins/connections/__init__.py:98). The flag is written into a dict that is thrown away at the end of the refresh, and the next refresh calls the failing probe again.

Measured by pointing conntrack at a file that does not exist and refreshing three times:

cycle 0: stats['nf_conntrack_enabled'] = False
cycle 1: stats['nf_conntrack_enabled'] = False
cycle 2: stats['nf_conntrack_enabled'] = False
conntrack read attempts: 3          (1 after this PR)
"Can not get network connections track" warnings: 3   (1 after this PR)

So on any host without /proc/sys/net/netfilter/nf_conntrack_* — macOS, Windows, a Linux kernel without the module — the warning plus its Disable connections track info line are logged on every single refresh.

The same applies to the psutil.net_connections() guard, and that one matters beyond the log: the plugin is disabled by default "because it consumes lots of CPU", so a user who turns it on (config or the K key) on a host where the call raises, such as macOS without root, pays for the failing call every refresh.

Every other plugin that gives up keeps the flag outside the per-refresh stats: mem with self.zfs_enabled (mem/__init__.py:134, read at :180), sensors with its grabber's init flag, wifi with a module-level check.

Resume

  • Keep net_connections_enabled / nf_conntrack_enabled on the plugin instance, set to False the first time a probe fails.
  • Copy them into stats at the start of each refresh, so update_views(), msg_curse() and the REST/WebUI payload still carry the same two fields as before.

Tests in tests/test_connections_states.py:

  • a failed conntrack read happens once across three refreshes, and the flag stays False
  • a raising psutil.net_connections is called once across three refreshes
  • guard: when both probes work, they keep running every refresh and nf_conntrack_percent is still computed

The tests drive the real update() with the refresh timer pushed past its deadline between calls, the way the main loop reaches it; without that, the refresh timer alone would make any version of the code look correct.

With glances/plugins/connections/__init__.py reverted, the first two fail (3 reads / 3 calls instead of 1) and the guard passes. pytest tests/test_connections_states.py tests/test_core.py tests/test_plugin_ports.py → 75 passed, 6 skipped. ruff check and ruff format --check clean.

Fixed tickets

None open; I did not find an issue for this.

The connections plugin gives up on a probe that fails: psutil.net_connections
raising, or the nf_conntrack files not being there. It records that by
writing net_connections_enabled / nf_conntrack_enabled into stats, but
update() starts every refresh with

    stats = self.get_init_value()

which is a copy of stats_init_value, where both flags are True. The flag is
therefore forgotten as soon as it is written, and update() calls the failing
probe again on the next refresh. Measured with the conntrack path pointing at
a missing file and three refreshes: three reads and three
"Can not get network connections track" warnings, instead of one.

That also defeats the guard around psutil.net_connections, which is the
expensive call the plugin's own comment is about ("because it consumes lots
of CPU"), on a host where it raises, such as macOS without root.

Keep the two flags on the plugin, the way mem keeps zfs_enabled and sensors
keeps its probe state, and copy them into stats each refresh so the views and
the REST API still see them.
@nicolargo nicolargo added the bug label Sep 12, 2026
@nicolargo nicolargo added this to the Glances 4.5.7 milestone Sep 12, 2026
@nicolargo
nicolargo merged commit dfa6137 into nicolargo:develop Sep 12, 2026
17 of 19 checks passed
@nicolargo

Copy link
Copy Markdown
Owner

Thanks @datrixlab

Merged into develop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants