Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store references to tasks #718

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions asyncssh/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,8 @@ def __init__(self, loop: asyncio.AbstractEventLoop,

self._close_event = asyncio.Event()

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

self._server_host_key_algs: Optional[Sequence[bytes]] = None

self._logger = logger.get_child(
Expand Down Expand Up @@ -1083,6 +1085,7 @@ 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 @@ -1101,6 +1104,7 @@ 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 Expand Up @@ -2727,6 +2731,9 @@ def abort(self) -> None:

self.logger.info('Aborting connection')

# cancel all running tasks
for task in self._tasks:
task.cancel()
self._force_close(None)

def close(self) -> None:
Expand Down Expand Up @@ -2754,6 +2761,8 @@ async def wait_closed(self) -> None:
await self._agent.wait_closed()

await self._close_event.wait()
if self._tasks:
await asyncio.wait(self._tasks)

def disconnect(self, code: int, reason: str,
lang: str = DEFAULT_LANG) -> None:
Expand Down