Open Torxed opened 4 months ago
The KeyboardInterrupt
is blocked until the call to C_WaitForSlotEvent()
returns to Python.
For example you can also insert or remove a token to get unblocked.
I don't know if it is possible to allow Ctrl-C from inside a C function called by Python. Do you have an idea?
I believe the signal library would be able to hook the interrupt, from there you would need to break the C_WaitForSlotEvent()
loop cleanly some how.
I haven't checked the low level C code yet, but could have a look :)
The only way to interrupt a C_WaitForSlotEvent()
is to call C_Finalize()
.
Maybe a signal handler could do that?
The problem is that the application that uses PyKCS11 may also want to get KeyboardInterrupt
events.
My C is very rusty, but instinctively I think builtin Python could handle this:
The end users code:
import time
# `b.py` will emulate PyKCS11 here
# just for the sake of testing signal() handling
# import PyKCS11
import b
while True:
time.sleep(0.25)
b.py
:
import signal
_op_sigint = signal.getsignal(signal.SIGINT)
def signal_handler(sig, frame):
print("lib.C_Finalize() here")
_op_sigint(sig, frame)
signal.signal(signal.SIGINT, signal_handler)
The drawback is that the stacktrace will add upon the users actual position of the Ctrl-C:
anton@bigrigv2 ~ $ python test.py
^CTraceback (most recent call last):
File "/home/anton/test.py", line 5, in <module>
time.sleep(0.25)
File "/home/anton/b.py", line 7, in signal_handler
_op_sigint(sig, frame)
KeyboardInterrupt
Where it was actually called on time.sleep()
not in b.py
, but the stack trace will (I hope) stay intact, just having extra entries in the stack.
However, this is the smallest and most non-invasive handling I could think of.
As it preserves user-set signal handling, as well as defaults to built-in handler of the given signal.
Not sure how you would access the given PyKCS11Lib()
instance, to reach self.lib.C_Finalize()
tho.
I don't think it is a good example code.
time.sleep()
can be interrupted by Ctrl-C.
In your output I do not see the "lib.C_Finalize() here"
message. Is the print()
executed?
We need something that is not interruptible in Python.
Your system information
Please describe your issue in as much detail as possible:
Using the events.py example, as referenced in #167, it appears to be working but blocking KeyboardInterrupt:
The above exception, appears to be blocked until
systemctl stop pcscd.service
is executed.Steps for reproducing this issue:
Pressing Ctrl-C will result in:
Until
pcscd.service
is restarted, then: