Skip to content

Wrap fire-and-forget MinionEvent callers to close SaltEvent chain (#7… - #70258

Merged
dwoz merged 2 commits into
saltstack:3008.xfrom
dwoz:dwoz/fix/70175-saltevent-caller-close
Sep 10, 2026
Merged

dwoz merged 2 commits into
saltstack:3008.xfrom
dwoz:dwoz/fix/70175-saltevent-caller-close

Conversation

@dwoz

@dwoz dwoz commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

…0175)

salt.modules.event.fire_master and salt.modules.mine._mine_send constructed a temporary MinionEvent, called fire_event() on it, and dropped the reference. With no context manager and no explicit destroy(), cleanup ran only when GC eventually invoked SaltEvent.__del__ -- and each finalization emitted the three-warning triad from the underlying transport chain:

unclosed publish server <PublishServer ...>
unclosed publisher client <_TCPPubServerPublisher ...>
unclosed SyncWrapper for cls=<class '..._TCPPubServerPublisher'>

Under mixed-job load on a 3008.x minion this surfaced as ~3 ResourceWarnings per job (issue #70175 reports 646 warnings across ~200 jobs). The FDs backing each unclosed publisher pinned an in-flight IPC socket + IOStream + Unpacker until GC ran, contributing to the RSS growth and unclosed-resource noise flagged in the issue.

Fix: wrap the MinionEvent in a with block so __exit__ calls destroy() -> close_pub() / close_pull() -> SyncWrapper.close() synchronously, tearing down the _TCPPubServerPublisher chain in the same call. Companion to PR #70206 (shutdown-time MinionManager destroy chain) and the sibling PR on branch
dwoz/fix/70175-pubserver-perjob-leak (server-side PubServer._discard_on_close fix). This PR closes the third leg: per-job caller-side leaks on the minion.

Regression test

New unit test test_fire_master_context_managed_no_unclosed_warnings in tests/pytests/unit/modules/test_event.py runs salt.modules.event.fire_master 50 times and asserts no unclosed publish server / unclosed publisher client / unclosed SyncWrapper warnings are emitted.

Pre-patch (verified by stashing this commit's changes to salt/modules/event.py + salt/modules/mine.py and re-running)::

E   AssertionError: fire_master leaked 74 unclosed-resource warnings
E   across 50 iterations (expected 0).  Sample warnings:
E     - unclosed publisher client <_TCPPubServerPublisher ...>
E     - unclosed publish server <PublishServer ...>
E     - unclosed SyncWrapper for cls=<class '..._TCPPubServerPublisher'>
FAILED tests/pytests/unit/modules/test_event.py::test_fire_master_context_managed_no_unclosed_warnings

Post-patch: PASSED (0 warnings across 50 iterations).

What does this PR do?

What issues does this PR fix or reference?

Fixes

Previous Behavior

Remove this section if not relevant

New Behavior

Remove this section if not relevant

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes/No

…ltstack#70175)

``salt.modules.event.fire_master`` and ``salt.modules.mine._mine_send``
constructed a temporary ``MinionEvent``, called ``fire_event()`` on it,
and dropped the reference.  With no context manager and no explicit
``destroy()``, cleanup ran only when GC eventually invoked
``SaltEvent.__del__`` -- and each finalization emitted the three-warning
triad from the underlying transport chain:

    unclosed publish server <PublishServer ...>
    unclosed publisher client <_TCPPubServerPublisher ...>
    unclosed SyncWrapper for cls=<class '..._TCPPubServerPublisher'>

Under mixed-job load on a 3008.x minion this surfaced as ~3
ResourceWarnings per job (issue saltstack#70175 reports 646 warnings across ~200
jobs).  The FDs backing each unclosed publisher pinned an in-flight IPC
socket + IOStream + Unpacker until GC ran, contributing to the RSS
growth and unclosed-resource noise flagged in the issue.

Fix: wrap the ``MinionEvent`` in a ``with`` block so ``__exit__`` calls
``destroy()`` -> ``close_pub()`` / ``close_pull()`` -> ``SyncWrapper.close()``
synchronously, tearing down the ``_TCPPubServerPublisher`` chain in the
same call.  Companion to PR saltstack#70206 (shutdown-time ``MinionManager``
destroy chain) and the sibling PR on branch
``dwoz/fix/70175-pubserver-perjob-leak`` (server-side
``PubServer._discard_on_close`` fix).  This PR closes the third leg:
per-job caller-side leaks on the minion.

Regression test
---------------

New unit test ``test_fire_master_context_managed_no_unclosed_warnings``
in ``tests/pytests/unit/modules/test_event.py`` runs
``salt.modules.event.fire_master`` 50 times and asserts no
``unclosed publish server`` / ``unclosed publisher client`` /
``unclosed SyncWrapper`` warnings are emitted.

Pre-patch (verified by stashing this commit's changes to
``salt/modules/event.py`` + ``salt/modules/mine.py`` and re-running)::

    E   AssertionError: fire_master leaked 74 unclosed-resource warnings
    E   across 50 iterations (expected 0).  Sample warnings:
    E     - unclosed publisher client <_TCPPubServerPublisher ...>
    E     - unclosed publish server <PublishServer ...>
    E     - unclosed SyncWrapper for cls=<class '..._TCPPubServerPublisher'>
    FAILED tests/pytests/unit/modules/test_event.py::test_fire_master_context_managed_no_unclosed_warnings

Post-patch: PASSED (0 warnings across 50 iterations).
@dwoz
dwoz requested a review from a team as a code owner September 10, 2026 01:54
@dwoz dwoz added the test:full Run the full test suite label Sep 10, 2026
@dwoz
dwoz merged commit b5f43ae into saltstack:3008.x Sep 10, 2026
2 checks passed
twangboy pushed a commit to twangboy/salt that referenced this pull request Sep 11, 2026
cli/daemons.py's Minion.start() never called shutdown()/destroy() when
_real_start() raised SaltClientError because self.minion.restart was
True -- it just returned (or looped for multi-master failover),
leaving event_publisher/event to be reclaimed only by __del__'s
GC-time safety net, which is what logged the loud "unclosed publish
server", "unclosed SyncWrapper", and "unclosed publisher client"
WARNING messages.

- salt/cli/daemons.py: Minion.start() now destroys the MinionManager
  on SaltClientError, both before retrying (daemonized multi-master
  failover) and before falling through to the final break/return.

The companion gap in MinionManager.destroy() itself -- it never closed
event_publisher/event, only stop_async() (the SIGTERM path) did -- was
independently fixed upstream in 3008.x by adbbb90 (PR saltstack#70258), so
that half of the original fix is dropped here to avoid duplicating it.

Fixes saltstack#70175
twangboy added a commit to twangboy/salt that referenced this pull request Oct 1, 2026
Minion.start() never called destroy() when _real_start() raised
SaltClientError because self.minion.restart was True, leaving
event_publisher/event to be reclaimed only by __del__'s GC-time
safety net. Destroy the MinionManager both before the daemonized
multi-master failover retry and before the final return.

The companion gap in MinionManager.destroy() was fixed in saltstack#70258.

Refs saltstack#70175
twangboy added a commit to twangboy/salt that referenced this pull request Oct 19, 2026
Minion.start() never called destroy() when _real_start() raised
SaltClientError because self.minion.restart was True, leaving
event_publisher/event to be reclaimed only by __del__'s GC-time
safety net. Destroy the MinionManager both before the daemonized
multi-master failover retry and before the final return.

The companion gap in MinionManager.destroy() was fixed in saltstack#70258.

Refs saltstack#70175
dwoz pushed a commit that referenced this pull request Oct 21, 2026
Minion.start() never called destroy() when _real_start() raised
SaltClientError because self.minion.restart was True, leaving
event_publisher/event to be reclaimed only by __del__'s GC-time
safety net. Destroy the MinionManager both before the daemonized
multi-master failover retry and before the final return.

The companion gap in MinionManager.destroy() was fixed in #70258.

Refs #70175
@dwoz
dwoz deleted the dwoz/fix/70175-saltevent-caller-close branch November 13, 2026 08:29

This branch was previously deployed

1 inactive deployment
ci — 21f90c85 Deployed Sep 10, 2026 by dwoz via Prepare Workflow Run #27057
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.

1 participant