amazon-ssm-agent icon indicating copy to clipboard operation
amazon-ssm-agent copied to clipboard

Ctlr+c in session manager produces unexpected behavior

Open johnnyplaydrums opened this issue 7 years ago • 7 comments

Hello! When I ctrl+c while in a session manager session, the macOS agent starts doing weird stuff and spits out both the remote command prompt and my local command prompt. I would expect the session manager agent to capture the ctrl+c and send it to the remote host, which would stop the process running in that shell. Can I make a request for that feature or am I doing something wrong? Regardless, what's the best way to stop a process running in the shell of a session manager session? Thank you!

johnnyplaydrums avatar Jan 11 '19 19:01 johnnyplaydrums

Thank you for the feedback! We were not able to reproduce the issue and was hoping you can help us understand the repro steps better. To start with, which terminal and what versions of aws-cli, session-manager-plugin and ssm-agent are you using for this scenario? Is this a consistent repro in your setup?

sahirangaraju avatar Jan 25 '19 18:01 sahirangaraju

Ah shoot, sorry to waste your time I realized this is an issue on our side and not a problem with ssm. Thank you for the help! :disappear:

johnnyplaydrums avatar Jan 25 '19 23:01 johnnyplaydrums

@johnplaydrums How did you solve it, myself and a few colleagues are having the same problem, we're not even using the same terminal program (colleague is using Terminal.app and I'm using iTerm 2)

jfharden avatar Mar 21 '19 11:03 jfharden

@jfharden Hey, I was having issues because we wrap the aws ssm start-session call in a python script, and the python script was having trouble handling ctrl+c correctly. Once I used the command directly (aws ssm start-session) it handled ctrl+c just fine. Are you wrapping the aws cli call in a script or anything?

johnnyplaydrums avatar Mar 21 '19 15:03 johnnyplaydrums

@johnplaydrums Brilliant! Thank you. I'm using https://github.com/mmmorris1975/aws-runas to get the correct credentials and it seems this is the cause for us. If I run it as

# aws-runas myaccount aws ssm start-session --target i-123456789`

when I ctrl+c it goes crazy (exactly as you describe), but if I

# $(aws-runas myaccount)
# aws ssm start-session --target i-123456789

then everything is fine.

jfharden avatar Mar 21 '19 16:03 jfharden

Yeah makes sense. You could add functionality to the aws-runas tool to better support ctrl+c, but I’d recommend just running it as you did in your second example. Cheers!

johnnyplaydrums avatar Mar 21 '19 16:03 johnnyplaydrums

I hit this today. There's a very simple fix. If you're running Python then instead of just running Popen, check_call or whatever wrap it in code to ignore the signal, for instance:

        try:
            signal.signal(signal.SIGINT, signal.SIG_IGN)
            p = Popen(command, **kwargs)
            p.wait()
        finally:
            signal.signal(signal.SIGINT, signal.SIG_DFL)

This way the parent doesn't do anything with it. Watch out for leaving sig-int in the wrong state in a multi-threaded program but hopefully this will be useful for some simple scripts.

keeely avatar Jul 13 '23 20:07 keeely