From 062eaf7379974675dc6c00445f29ce2a4753db0b Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Fri, 2 May 2025 09:48:51 +0100 Subject: [PATCH] nixos-rebuild-ng: alert user if we can't clean-up remote process --- .../src/nixos_rebuild/process.py | 61 +++++++++++-------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/process.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/process.py index b21010f54f40..d6d798fdc62d 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/process.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/process.py @@ -179,27 +179,40 @@ def _kill_long_running_ssh_process(args: Args, remote: Remote) -> None: # its arguments as regex) quoted_args = re.escape(shlex.join(str(a) for a in args)) logger.debug("killing remote process using pkill with args=%r", quoted_args) - r = subprocess.run( - [ - "ssh", - *remote.opts, - *SSH_DEFAULT_OPTS, - remote.host, - "--", - "pkill", - "--signal", - "SIGINT", - "--full", - "--", - quoted_args, - ], - check=False, - capture_output=True, - text=True, - ) - logger.debug( - "remote pkill captured output with stdout=%r, stderr=%r, returncode=%s", - r.stdout, - r.stderr, - r.returncode, - ) + cleanup_interrupted = False + + try: + r = subprocess.run( + [ + "ssh", + *remote.opts, + *SSH_DEFAULT_OPTS, + remote.host, + "--", + "pkill", + "--signal", + "SIGINT", + "--full", + "--", + quoted_args, + ], + check=False, + capture_output=True, + text=True, + ) + logger.debug( + "remote pkill captured output with stdout=%r, stderr=%r, returncode=%s", + r.stdout, + r.stderr, + r.returncode, + ) + except KeyboardInterrupt: + cleanup_interrupted = True + raise + finally: + if cleanup_interrupted or r.returncode: + logger.warning( + "could not clean-up remote process, the command %s may still be running in host '%s'", + args, + remote.host, + )