From dd67dbf54448ec43d4c0ed3105982244f6e4b6eb Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 20 Nov 2026 16:43:30 +0100 Subject: [PATCH] lib.ec.ssh.AsyncSSH: Forward SIGINT to remote The first Ctrl-C cancels the running task while the SSH connection is still open, but _run_ssh() has no cleanup: the connection is abandoned and the remote process is never signalled. A long-running command such as zypper dup therefore keeps running after the client dies, taking its children with it and leaving the system in a half-baked state, e.g. a mid-air package transaction. Catch the cancellation inside the run, forward SIGINT to the remote process, and wait for it to exit, draining the captured output, before propagating the cancellation. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.2 Signed-off-by: Jan Lindemann --- src/python/jw/pkg/lib/ec/ssh/AsyncSSH.py | 18 +++++++++++++++++- .../jw/pkg/lib/ec/secret_store/layout/test.py | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/python/jw/pkg/lib/ec/ssh/AsyncSSH.py b/src/python/jw/pkg/lib/ec/ssh/AsyncSSH.py index cd7d53d6..1cf75aef 100644 --- a/src/python/jw/pkg/lib/ec/ssh/AsyncSSH.py +++ b/src/python/jw/pkg/lib/ec/ssh/AsyncSSH.py @@ -439,7 +439,23 @@ class AsyncSSH(Base): await proc.stdin.drain() proc.stdin.write_eof() - completed = await proc.wait(check = False) + finished = asyncio.create_task(proc.wait(check = False)) + try: + completed = await asyncio.shield(finished) + except asyncio.CancelledError: + # A Ctrl-C cancels this task while the connection is + # still open. Forward the interrupt to the remote process + # and wait for it to exit so we do not leave it (and its + # children) running. + if not finished.done(): + try: + proc.send_signal('INT') + except Exception as e: + log(DEBUG, f'Failed to send INT to remote process ({e})') + completed = await asyncio.shield(finished) + await asyncio.gather(*tasks) + raise + await asyncio.gather(*tasks) stdout = b''.join(stdout_parts) if stdout_parts else None diff --git a/test/unit/python/jw/pkg/lib/ec/secret_store/layout/test.py b/test/unit/python/jw/pkg/lib/ec/secret_store/layout/test.py index 1ca50ad0..477b3208 100644 --- a/test/unit/python/jw/pkg/lib/ec/secret_store/layout/test.py +++ b/test/unit/python/jw/pkg/lib/ec/secret_store/layout/test.py @@ -5,8 +5,8 @@ from jw.pkg.lib.ec.secret_store.layout import ( FT_SECRET_FILE, FT_SECRET_KEY, SECRET_SUFFIX, - target_name, host_prefix, + target_name, ) from jw.pkg.lib.file_types import FT_COMPOUND, FT_DIR, FT_REG -- 2.56.1