Skip to content

[3008.x] Fix RequestClient socketpair FD leak via graceful drain (#69991) - #69997

Merged
dwoz merged 2 commits into
saltstack:3008.xfrom
dwoz:dwoz/fix/bug7-requestclient-drain-3008.x
Aug 11, 2026
Merged

dwoz merged 2 commits into
saltstack:3008.xfrom
dwoz:dwoz/fix/bug7-requestclient-drain-3008.x

Conversation

@dwoz

@dwoz dwoz commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • RequestClient.close() now schedules an async graceful drain that awaits _send_recv_exit_future before closing socket/context
  • _send_recv sets the exit future in try/finally on every exit path
  • New SyncWrapper.__del__ emits ResourceWarning for wrappers GC'd without explicit close
  • Pattern mirrors AsyncReqMessageClient graceful shutdown ([Bug]: Memory leak in salt-api #68637 chain)

Validated in stress bench: median FD/minion dropped from 924 (throttle threshold) to 35 (idle baseline); 0/50 minions crossed 500 FDs.

Fixes #69991

Test plan

  • Unit test: RequestClient.close() doesn't leak sockets under repeated cycles
  • Long-run soak: minion FD count remains stable under sustained saltutil.refresh_pillar

``RequestClient.close()`` was dropping a ``(None, None)`` sentinel and
immediately closing ``self.socket`` + destroying ``self.context``. The
running ``send_recv_task`` coroutine was still on the io_loop with its
locals holding a reference to the socket; the close raced the task's
finally, leaving the underlying socketpair + mailbox FDs unfreed.
Under sustained ``saltutil.refresh_pillar`` / re-auth churn this
leaked ~451 socketpairs (~902 FDs) per minion, tripping the 1024-FD
ulimit throttle at ~924/1024 within minutes.

Port the graceful-drain pattern from ``AsyncReqMessageClient`` (from
twangboy's saltstack#68637 chain: ``6ad90a51d65``, ``aa317c67e51``,
``bcb3778c9c1``, ``b96ddd58ce6``): add ``_send_recv_exit_future``
that ``_send_recv`` sets in a try/finally, then rewrite ``close()`` to
schedule an async ``_drain_and_close`` task that awaits the future
(with 5s timeout) before closing the socket + destroying the context.

Also add ``SyncWrapper.__del__`` that emits ``ResourceWarning`` if the
wrapper was GC'd without an explicit ``close()`` -- mirrors
``SaltEvent.__del__`` (``salt/utils/event.py:278-321``) and surfaces
future missed-close bugs in tests and sentry rather than silently
leaking loops.

Validated in a 10-min stress bench: median FD/minion dropped from
924 (throttle threshold) to 35 (idle baseline); zero minions crossed
500 FDs across 50 stressed minions.

Fixes saltstack#69991
@dwoz
dwoz requested a review from a team as a code owner August 9, 2026 22:26
@dwoz dwoz added the test:full Run the full test suite label Aug 9, 2026
@dwoz dwoz added this to the Argon v3008.3 milestone Aug 9, 2026
dwoz added a commit that referenced this pull request Aug 10, 2026
Mirrors ``SaltEvent.__del__`` at ``salt/utils/event.py``: emits
``ResourceWarning`` when a ``SyncWrapper`` is garbage-collected
without an explicit ``close()`` (i.e. no ``with`` statement, no
``destroy()``). Deliberately does NOT close the wrapped object /
io_loop from ``__del__`` -- doing so during GC or interpreter
shutdown risks touching partially-freed tornado / asyncio state.

Surfaces missed-close bugs in tests / sentry / log aggregators so
they can be fixed at the source, without re-introducing the
'silent GC-time cleanup' trap.

The primary RequestClient socket-leak fix from 3008.x PR #69997 is
not needed on 3006.x -- that code path is already covered by the
``AsyncReqMessageClient`` hardening from #68637.

Fixes #69991
The graceful-drain patch scheduled ``_drain_and_close`` on the io_loop
via ``call_soon_threadsafe`` and returned immediately.  When the caller
was already on the loop thread (async production code, or a sync
fixture-teardown after an async test) and did not yield control to the
loop before it was torn down, the drain task was destroyed while
pending -- socket + context never got closed, and the ``zmq.Context``
finalizer later blocked in ``__del__`` -> ``term()`` under GC.  All 20
functional zeromq CI jobs hung until the 3h workflow timeout after the
first ``test_request_client_send_recv_socket_closed`` finished cleanly
and pytest tried to run the next test (repro: 2h45m gap between last
PASSED and cancellation in Debian 11 job 93340807019, py-spy dump
showed the main thread stuck in ``zmq.sugar.context.term``).

Split the teardown into three branches:

1. Same-thread + loop-running: the shutdown sentinel is already queued;
   fall through to the sync teardown so socket/context close
   deterministically before we return.  ``_send_recv`` picks up the
   sentinel on the next iteration and drops its socket ref, matching
   base-branch behavior that the ``send_recv_socket_closed`` test
   asserts on.
2. Cross-thread + loop-running: schedule the drain and block on a
   ``threading.Event`` (6s cap, matching the 5s drain timeout) so the
   caller doesn't tear down its loop while our task is still pending.
3. Loop not running: sync teardown directly (unchanged fallback).

Restores test_request_client.py to all-passing on the functional
transport suite (16 passed + 1 xfailed, was hanging indefinitely after
the third test).
@dwoz
dwoz merged commit ab80f9a into saltstack:3008.x Aug 11, 2026
474 checks passed
@dwoz
dwoz deleted the dwoz/fix/bug7-requestclient-drain-3008.x branch November 13, 2026 08:30

This branch was previously deployed

1 inactive deployment
ci — 980f142c Deployed Aug 11, 2026 by dwoz via Build Onedir Packages / Windows (amd64) #26198
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