Skip to content

Cancel PubServer read task on stream close to release per-job leak - #70257

Merged
dwoz merged 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/70175-pubserver-perjob-leak
Sep 10, 2026
Merged

dwoz merged 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/70175-pubserver-perjob-leak

Conversation

@dwoz

@dwoz dwoz commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

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_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: #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

@dwoz
dwoz requested a review from a team as a code owner September 10, 2026 01:54
@dwoz dwoz added the test:full Run the full test suite label Sep 10, 2026
@dwoz
dwoz force-pushed the dwoz/fix/70175-pubserver-perjob-leak branch from bd9ffff to 7f9e3a3 Compare September 10, 2026 08:08
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
dwoz force-pushed the dwoz/fix/70175-pubserver-perjob-leak branch from 7f9e3a3 to 15d7139 Compare September 10, 2026 08:14
@dwoz
dwoz merged commit ecc39e7 into saltstack:3008.x Sep 10, 2026
2 checks passed
@dwoz
dwoz deleted the dwoz/fix/70175-pubserver-perjob-leak branch November 13, 2026 08:30

This branch was previously deployed

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