Skip to content

Wrap fire-and-forget SaltEvent in salt.utils.error.fire_exception - #70259

Merged
dwoz merged 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/70175-receive-path-saltevent
Sep 10, 2026
Merged

dwoz merged 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/70175-receive-path-saltevent

Conversation

@dwoz

@dwoz dwoz commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Fixes #70175

salt.utils.error.fire_exception -- called from salt/minion.py:_thread_return (unhandled-exception path) and from salt/metaproxy/{proxy,deltaproxy}.py -- constructed a bare salt.utils.event.SaltEvent, 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 finalisation emitted the three-warning triad from the underlying transport chain:

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

Fix: wrap the SaltEvent 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 per-job caller fixes on branches
dwoz/fix/70175-pubserver-perjob-leak (server-side PubServer._discard_on_close fix) and
dwoz/fix/70175-saltevent-caller-close (salt.modules.event.fire_master and salt.modules.mine._mine_send). This PR closes the receive-path leg: the fire_exception helper on the minion's job-error return path.

Regression test

New unit test test_fire_exception_context_managed_no_unclosed_warnings in tests/pytests/unit/utils/test_error.py runs salt.utils.error.fire_exception 50 times and asserts no unclosed publish server / unclosed publisher client / unclosed SyncWrapper warnings surface after gc.collect().

Pre-patch (verified by git stash push salt/utils/error.py and re-running)::

AssertionError: fire_exception leaked 75 unclosed-resource
warnings across 50 iterations (expected 0).  Sample warnings:
  - unclosed publisher client <_TCPPubServerPublisher ...>
  - unclosed publisher client <_TCPPubServerPublisher ...> ...

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

@dwoz
dwoz requested a review from a team as a code owner September 10, 2026 07:54
…ltstack#70175)

``salt.utils.error.fire_exception`` -- called from
``salt/minion.py:_thread_return`` (unhandled-exception path) and from
``salt/metaproxy/{proxy,deltaproxy}.py`` -- constructed a bare
``salt.utils.event.SaltEvent``, 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 finalisation emitted the three-warning
triad from the underlying transport chain:

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

Fix: wrap the ``SaltEvent`` 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 per-job caller fixes on branches
``dwoz/fix/70175-pubserver-perjob-leak`` (server-side
``PubServer._discard_on_close`` fix) and
``dwoz/fix/70175-saltevent-caller-close`` (``salt.modules.event.fire_master``
and ``salt.modules.mine._mine_send``).  This PR closes the receive-path
leg: the ``fire_exception`` helper on the minion's job-error return
path.

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

New unit test ``test_fire_exception_context_managed_no_unclosed_warnings``
in ``tests/pytests/unit/utils/test_error.py`` runs
``salt.utils.error.fire_exception`` 50 times and asserts no
``unclosed publish server`` / ``unclosed publisher client`` /
``unclosed SyncWrapper`` warnings surface after ``gc.collect()``.

Pre-patch (verified by ``git stash push salt/utils/error.py`` and
re-running)::

    AssertionError: fire_exception leaked 75 unclosed-resource
    warnings across 50 iterations (expected 0).  Sample warnings:
      - unclosed publisher client <_TCPPubServerPublisher ...>
      - unclosed publisher client <_TCPPubServerPublisher ...>
      ...

Post-patch: PASSED (0 warnings across 50 iterations).
@dwoz
dwoz force-pushed the dwoz/fix/70175-receive-path-saltevent branch from 4e56e91 to ac99b39 Compare September 10, 2026 08:20
@dwoz
dwoz merged commit 0399687 into saltstack:3008.x Sep 10, 2026
3 checks passed
@dwoz
dwoz deleted the dwoz/fix/70175-receive-path-saltevent branch November 13, 2026 08:30

This branch was previously deployed

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

1 participant