Repository navigation
[3008.x] Fix RequestClient socketpair FD leak via graceful drain (#69991) - #69997
Merged
dwoz merged 2 commits intoAug 11, 2026
Merged
Conversation
``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
2 tasks
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).
twangboy
approved these changes
Aug 11, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RequestClient.close()now schedules an async graceful drain that awaits_send_recv_exit_futurebefore closing socket/context_send_recvsets the exit future in try/finally on every exit pathSyncWrapper.__del__emitsResourceWarningfor wrappers GC'd without explicit closeAsyncReqMessageClientgraceful 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
RequestClient.close()doesn't leak sockets under repeated cyclessaltutil.refresh_pillar