What are you using as your SSH server here? If you are using OpenSSH’s “sshd", I think the problem may be that OpenSSH doesn’t implement the SSH ‘signal’ channel request. So, even though AsyncSSH is sending the request properly, it is getting ignored by OpenSSH. You can find more info at
https://bugzilla.mindrot.org/show_bug.cgi?id=1424. Unfortunately, this is a very old bug and as far as I know the patch listed there has never made it into a standard release.
It’s a bit of a hack, but depending on the signal you want to send it might be possible to do it by writing to stdin instead. If you set up the command to run in a PTY (by setting term_type), you can then write something like ‘\x03’ (Ctrl-C) to stdin to get the equivalent of a SIGINT, or ‘\x1c’ to get the equivalent of a SIGQUIT. I don’t know of anything you can write to get other signals like SIGTERM, SIGHUP, or SIGKILL, though.
Here’s an example of a program which uses the stdin trick I mentioned:
import asyncio, asyncssh, sys
async def run_client(loop):
async with asyncssh.connect(‘localhost') as conn:
proc = await conn.create_process('sleep 5', term_type='ansi')
loop.call_later(1, proc.stdin.write, '\x03')
await proc.wait_closed()
print(proc.exit_signal)
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(run_client(loop))
except (OSError, asyncssh.Error) as exc:
sys.exit('SSH connection failed: ' + str(exc))
This executes a remote command of ‘sleep 5’ but then arranges to abort after 1 second by sending a Ctrl-C. It prints the results of the signal which caused the process to exit. Try replacing ‘\x03’ with ‘\x1c’ to see the different between exiting with an INT signal vs. a QUIT signal.
Note the setting of term_type there to ‘ansi’ to make sure a PTY is requested on the remote system. Without that, I don’t believe these control characters will trigger a signal being sent to the process being executed.
As you can see from this example, there’s no problem arranging to abort even when there’s already an active wait_closed() on the channel/process.