Skip to content

Commit

Permalink
Keep strong references to tasks started via SSHConnection
Browse files Browse the repository at this point in the history
This commit keeps a set of outstanding tasks started through
SSHConnection.create_task(), so that these tasks won't be canceled
by the garbage collector while the SSH connection is still active.
Thanks go to GitHub user Birnendampf for pointing this potential
issue and suggesting a simple fix!
  • Loading branch information
ronf committed Nov 28, 2024
1 parent edc9673 commit bc20fb0
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions asyncssh/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ def __init__(self, loop: asyncio.AbstractEventLoop,
self._x11_listener: Union[None, SSHX11ClientListener,
SSHX11ServerListener] = None

self._tasks: Set[asyncio.Task[None]] = set()
self._close_event = asyncio.Event()

self._server_host_key_algs: Optional[Sequence[bytes]] = None
Expand Down Expand Up @@ -1143,6 +1144,8 @@ def _reap_task(self, task_logger: Optional[SSHLogger],
task: 'asyncio.Task[None]') -> None:
"""Collect result of an async task, reporting errors"""

self._tasks.discard(task)

# pylint: disable=broad-except
try:
task.result()
Expand All @@ -1161,6 +1164,8 @@ def create_task(self, coro: Awaitable[None],

task = asyncio.ensure_future(coro)
task.add_done_callback(partial(self._reap_task, task_logger))
self._tasks.add(task)

return task

def is_client(self) -> bool:
Expand Down

0 comments on commit bc20fb0

Please sign in to comment.