Skip to content

Fix minion crash with RuntimeError: Event loop is closed on daemon retry - #70180

Open
twangboy wants to merge 3 commits into
saltstack:3008.xfrom
twangboy:fix/70178/3008.x
Open

Fix minion crash with RuntimeError: Event loop is closed on daemon retry#70180
twangboy wants to merge 3 commits into
saltstack:3008.xfrom
twangboy:fix/70178/3008.x

Conversation

@twangboy

Copy link
Copy Markdown
Contributor

What does this PR do?

When salt-minion runs in classic daemon mode (-d) and loses its master connection with no working failover, MinionManager.tune_in() closes its io_loop and returns. cli.daemons.Minion.start()'s retry loop called _real_start() -> tune_in() again on the same, now-stale MinionManager, whose io_loop.run_forever() immediately raised RuntimeError: Event loop is closed, crashing the process.

  • salt/cli/daemons.py: Minion.start() now constructs a fresh MinionManager (mirroring the narrow construction step from prepare(), not prepare() itself, which would re-daemonize/re-fork) before retrying, instead of reusing the stale instance.
  • salt/minion.py: MinionManager.stop() now guards against a SIGTERM landing in the destroy()-to-reconstruct window and hitting a MinionManager whose loop is already closed; tune_in()'s final close() now checks is_closed() first, matching sibling Minion/SyndicManager tune_in() methods.
  • salt/transport/tcp.py, salt/utils/event.py: destroy()'s own cleanup chain (PublishClient.close() / on_recv(), Subscriber.close(), PubServer.close(), RequestClient.close(), SaltEvent.close_pub() / close_pull()) had several unguarded Task.cancel() / stream.close() calls that raised the same RuntimeError when invoked after their loop was already closed -- guard them the same way the zeromq transport already does, so destroy() can never itself crash.

What issues does this PR fix or reference?

Fixes #70178

Merge requirements satisfied?

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

Commits signed with GPG?

Yes

The minion logged loud "unclosed publish server", "unclosed
SyncWrapper", and "unclosed publisher client" WARNING messages after
losing its master connection with no working failover. Root cause was
two gaps in MinionManager's teardown:

- MinionManager.destroy() never closed event_publisher/event; only
  stop_async() (the SIGTERM path) did.
- cli/daemons.py's Minion.start() never called shutdown()/destroy()
  when _real_start() raised SaltClientError because
  self.minion.restart was True -- it just returned, leaving
  event_publisher/event to be reclaimed only by __del__'s GC-time
  safety net.

- salt/minion.py: MinionManager.destroy() now also closes
  event_publisher and destroys event, mirroring stop_async().
- 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.

Fixes saltstack#70175
When salt-minion runs in classic daemon mode (-d) and loses its master
connection with no working failover, MinionManager.tune_in() closes
its io_loop and returns. cli.daemons.Minion.start()'s retry loop
called _real_start() -> tune_in() again on the same, now-stale
MinionManager, whose io_loop.run_forever() immediately raised
RuntimeError: Event loop is closed, crashing the process.

- salt/cli/daemons.py: Minion.start() now constructs a fresh
  MinionManager (mirroring the narrow construction step from
  prepare(), not prepare() itself, which would re-daemonize/re-fork)
  before retrying, instead of reusing the stale instance.
- salt/minion.py: MinionManager.stop() now guards against a
  SIGTERM landing in the destroy()-to-reconstruct window and hitting
  a MinionManager whose loop is already closed; tune_in()'s final
  close() now checks is_closed() first, matching sibling Minion/
  SyndicManager tune_in() methods.
- salt/transport/tcp.py, salt/utils/event.py: destroy()'s own cleanup
  chain (PublishClient.close()/on_recv(), Subscriber.close(),
  PubServer.close(), RequestClient.close(), SaltEvent.close_pub()/
  close_pull()) had several unguarded Task.cancel()/stream.close()
  calls that raised the same RuntimeError when invoked after their
  loop was already closed -- guard them the same way the zeromq
  transport already does, so destroy() can never itself crash.

Fixes saltstack#70178
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