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

CLI scripting not handling Ctrl+C KeyboardInterrupt #562

Closed
chrisminett opened this issue Jul 31, 2023 · 2 comments
Closed

CLI scripting not handling Ctrl+C KeyboardInterrupt #562

chrisminett opened this issue Jul 31, 2023 · 2 comments

Comments

@chrisminett
Copy link

  • YubiKey Manager (ykman) version: 5.0.1
  • How was it installed?: Package from website downloads
  • Operating system and version: MacOS 13.5
  • YubiKey model and version: N/A
  • Bug description summary: CLI scripting not handling Ctrl+C KeyboardInterrupt

Steps to reproduce

Running the following example code using ykman script will not break the loop:

from time import sleep

# Run until stopped with Ctrl+C
while True:
    try:
        print('Sleeping')
        sleep(1.0)
        print('looping')
    except KeyboardInterrupt:
        print('***BREAK***')
        break

Expected result

Pressing Ctrl+C breaks out of the loop.

Actual results and logs

Pressing Ctrl+C prints Exiting due to signal 2 and the loop continues.

From the example script, good behaviour when running directly with python:

$ python break.py
Sleeping
looping
Sleeping
^C***BREAK***

Running the example script through ykman, bad behaviour and has to be terminated by kill <pid>:

$ ./ykman script -f ~/Downloads/break.py
Sleeping
looping
Sleeping
looping
Sleeping
^CExiting due to signal 2
looping
Sleeping
^CExiting due to signal 2
looping
Sleeping
looping
Sleeping
[1]    89895 terminated  ./ykman script -f break.py

Other info

This simplified example comes from trying to programme multiple OTP keys using the yubiotp_batch.py script. I found that it's impossible to break using Ctrl+C and the CSV file was never written and closed.

This seems to be because s.multi is always waiting for another device, which is why the test case above is just a copy of the loop's try/except:

I can't see anything immediately obvious as to why this happens, as it seems that the script is just given to exec:

exec(script_body, {}) # nosec

Replicating that behaviour as well directly in python doesn't cause the issue, so I assume there must be some other intercept somewhere in ykman.

fname = "/path/to/break.py"

with open(fname) as script:

    exec(script.read(), {})
@dainnilsson
Copy link
Member

I've tried to reproduce this using ykman 5.2.0, but I haven't been able to. It might be that it was already fixed since 5.0.1, could you give 5.2.0 a try?

@chrisminett
Copy link
Author

@dainnilsson I've installed 5.2.0 from the macOS package in GitHub releases, and can't reproduce the issue 🎉

Looking at the comparison between release/5.0.1...release/5.2.0 I can't see anything obvious that would have impacted this, but at least it works, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants