Repository navigation
Cancel PubServer read task on stream close to release per-job leak - #70257
Merged
Merged
Conversation
dwoz
force-pushed
the
dwoz/fix/70175-pubserver-perjob-leak
branch
from
September 10, 2026 08:08
bd9ffff to
7f9e3a3
Compare
Companion to saltstack#70206 which fixed the shutdown path for the same issue saltstack#70175 symptoms. This handles the steady-state per-job path. Root cause: ``PubServer.handle_stream`` scheduled ``_stream_read`` as an asyncio Task at accept time; the task awaited ``stream.read_bytes(...)`` and its coroutine frame held a 1 MiB msgpack ``Unpacker`` local and a ``client`` local. When the peer closed the connection, ``_discard_on_close`` removed the ``Subscriber`` from ``self.clients`` and cleared presence, but did NOT cancel the Task. If tornado did not translate the FIN to a prompt ``StreamClosedError`` (observed on real 3008.x minions), the Task stayed pending in ``asyncio.all_tasks()`` forever, pinning the ``Unpacker`` buffer, the ``client``, and the ``IOStream`` graph. Tracemalloc on a live 3008.x minion under a 132-job / 5-min mixed load: +140 pinned ``Subscriber`` and +140 pinned ``Unpacker`` instances (~142 MiB retention). Fix: ``handle_stream`` and ``_validate_ssl_and_add_client`` now store the ``_stream_read`` Task on ``Subscriber._read_task``; ``_discard_on_close`` cancels that Task (idempotent) and calls ``client.close()`` (also idempotent) so the coroutine frame releases promptly regardless of whether the FIN raised ``StreamClosedError``. Regression test: ``test_pub_server_discard_on_close_cancels_read_task`` registers 50 subscribers with a never-completing ``read_bytes`` stream (the pathological state), fires the close callback, and asserts zero pending ``_stream_read`` Tasks remain. Fails on unpatched 3008.x (50 leaked tasks), passes with the fix. Related-to: saltstack#70175, saltstack#70206
dwoz
force-pushed
the
dwoz/fix/70175-pubserver-perjob-leak
branch
from
September 10, 2026 08:14
7f9e3a3 to
15d7139
Compare
2 of 3 tasks
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.
Companion to #70206 which fixed the shutdown path for the same issue #70175 symptoms. This handles the steady-state per-job path.
Root cause:
PubServer.handle_streamscheduled_stream_readas an asyncio Task at accept time; the task awaitedstream.read_bytes(...)and its coroutine frame held a 1 MiB msgpackUnpackerlocal and aclientlocal. When the peer closed the connection,_discard_on_closeremoved theSubscriberfromself.clientsand cleared presence, but did NOT cancel the Task. If tornado did not translate the FIN to a promptStreamClosedError(observed on real 3008.x minions), the Task stayed pending inasyncio.all_tasks()forever, pinning theUnpackerbuffer, theclient, and theIOStreamgraph.Tracemalloc on a live 3008.x minion under a 132-job / 5-min mixed load: +140 pinned
Subscriberand +140 pinnedUnpackerinstances (~142 MiB retention).Fix:
handle_streamand_validate_ssl_and_add_clientnow store the_stream_readTask onSubscriber._read_task;_discard_on_closecancels that Task (idempotent) and callsclient.close()(also idempotent) so the coroutine frame releases promptly regardless of whether the FIN raisedStreamClosedError.Regression test:
test_pub_server_discard_on_close_cancels_read_taskregisters 50 subscribers with a never-completingread_bytesstream (the pathological state), fires the close callback, and asserts zero pending_stream_readTasks remain. Fails on unpatched 3008.x (50 leaked tasks), passes with the fix.Related-to: #70175, #70206
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