Skip to content

Fix PublishChannel fd leak in Minion.eval_master single-master path (#68901) - #69341

Merged
dwoz merged 1 commit into
saltstack:3006.xfrom
dwoz:fix/issue-68901
Jun 25, 2026
Merged

dwoz merged 1 commit into
saltstack:3006.xfrom
dwoz:fix/issue-68901

Conversation

@dwoz

@dwoz dwoz commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Closes a file-descriptor leak in the Salt minion. The single-master sign-in
branch of MinionBase.eval_master only ran pub_channel.close() inside an
except SaltClientError clause, so any other failure mode (an OSError
from the transport, asyncio cancellation, or the silent no-raise return
from AsyncPubChannel.connect() when its -|RETRY|- sentinel is present)
left the freshly-created AsyncPubChannel with its socket open. The
transport: detect loop also reassigned pub_channel to the next transport
without closing the previous unauthenticated one. The fix uses a
try/finally, mirroring the multi-master path, and closes the
unauthenticated channel inside the detect loop before continuing.

What issues does this PR fix or reference?

Fixes #68901

Previous Behavior

A minion with an unstable connection to its master accumulated one leaked
socket FD per failed reconnect attempt. Fleets with thousands of minions
hit per-process FD limits (e.g. 10k on Debian/systemd defaults), at which
point the minion goes unresponsive but does not exit, so systemd does not
restart it. Reporter observed daily hits at ~1k minions.

New Behavior

Each failed reconnect attempt closes its AsyncPubChannel exactly once
regardless of which exception caused the failure. Successful reconnects
hand the live channel off to the caller unchanged.

Merge requirements satisfied?

  • Docs (no user-visible behavior change beyond bugfix; no doc change required)
  • Changelog (changelog/68901.fixed.md)
  • Tests written/updated (tests/pytests/unit/test_minion.py::test_eval_master_single_master_closes_pub_channel_on_failure_68901)

Commits signed with GPG?

No - base branch commits are not signed; this PR follows the same convention.

@dwoz
dwoz requested a review from a team as a code owner June 6, 2026 06:51
@dwoz dwoz added the test:full Run the full test suite label Jun 6, 2026
@dwoz dwoz added this to the Sulphur v3006.26 milestone Jun 6, 2026
@dwoz

dwoz commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

Audit: the prior fd-leak fix on 3006.x (commit 9cd7805, Feb 2026, "Ensure we're not leaking file handles on connect failures") added the try/finally to the multi-master / failover path inside the for master in opts['local_masters'] loop. The single-master sign-in branch (master_type == 'str', the bottom else: at line ~795) was left without the same try/finally — only SaltClientError closes pub_channel; any other exception leaks the socket FD. The existing functional test test_minion_connection_failure_no_fd_leak runs against a single-master config but is heuristic (3 consecutive sample-to-sample increases or +25 vs baseline) and apparently does not consistently catch the leak under its async-teardown tolerances. This PR closes the remaining gap and adds a deterministic unit test that fails on the unfixed tree (close count 0) and passes with the fix.

@dwoz
dwoz force-pushed the fix/issue-68901 branch from b6743fb to d063c4c Compare June 12, 2026 23:47
@dwoz
dwoz force-pushed the fix/issue-68901 branch from d063c4c to 7965367 Compare June 15, 2026 05:47
When the single-master sign-in branch of MinionBase.eval_master failed,
only SaltClientError was handled: AsyncPubChannel.close() was called from
inside `except SaltClientError`, but any other exception (e.g. OSError
from the transport, asyncio cancellation, or the silent no-raise return
from AsyncPubChannel.connect() when its retry sentinel is present) left
the just-created channel with its socket still open. The `transport:
detect` loop also reassigned `pub_channel` without closing the prior one
when authentication had not succeeded. Over thousands of reconnect
attempts on flaky networks the minion exhausted its file-descriptor
limit and went unresponsive.

Mirror the multi-master branch's structure: stage the live channel into
ret_pub_channel before raising gen.Return, and use a `try / finally` so
the pub_channel is closed on every failure path, not just SaltClientError.
In the detect loop close the unauthenticated channel before continuing.

Fixes saltstack#68901

This branch was previously deployed

1 inactive deployment
ci — 7579b96d Deployed Jun 25, 2026 by dwoz via Build Onedir Packages / macOS (arm64) #24617
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants