Skip to content

Commit bc20fb0

Browse files
committed
Keep strong references to tasks started via SSHConnection
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!
1 parent edc9673 commit bc20fb0

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

asyncssh/connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ def __init__(self, loop: asyncio.AbstractEventLoop,
984984
self._x11_listener: Union[None, SSHX11ClientListener,
985985
SSHX11ServerListener] = None
986986

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

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

1147+
self._tasks.discard(task)
1148+
11461149
# pylint: disable=broad-except
11471150
try:
11481151
task.result()
@@ -1161,6 +1164,8 @@ def create_task(self, coro: Awaitable[None],
11611164

11621165
task = asyncio.ensure_future(coro)
11631166
task.add_done_callback(partial(self._reap_task, task_logger))
1167+
self._tasks.add(task)
1168+
11641169
return task
11651170

11661171
def is_client(self) -> bool:

0 commit comments

Comments
 (0)