From 773427ad7c1657d40ac0b11719aff94b28457f25 Mon Sep 17 00:00:00 2001 From: nacos82 <333503338+nacos82@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:54:16 +0200 Subject: [PATCH] Don't interrupt the on-chip match when a verify is cancelled A Cancel that arrives while match_finger() waits for the match interrupt made the cleanup command (0x62) go out mid-match. On 138a:0097 the sensor then drops off the USB bus and the service is left with a dead handle. wait_int() gets a non-cancellable mode that gives up after max_wait seconds, and match_finger() uses it so the match always finishes first. Co-Authored-By: Claude Fable 5.2 --- validitysensor/sensor.py | 3 ++- validitysensor/usb.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/validitysensor/sensor.py b/validitysensor/sensor.py index dd019bb..362455d 100644 --- a/validitysensor/sensor.py +++ b/validitysensor/sensor.py @@ -870,7 +870,8 @@ def match_finger(self) -> typing.Tuple[int, int, bytes]: rsp = tls.app(cmd) assert_status(rsp) - b = usb.wait_int() + # The sensor drops off the USB bus if the match is interrupted, so let it finish + b = usb.wait_int(cancellable=False) if b[0] != 3: raise Exception('Finger not recognized: %s' % hexlify(b).decode()) diff --git a/validitysensor/usb.py b/validitysensor/usb.py index 464b092..3bc19d4 100644 --- a/validitysensor/usb.py +++ b/validitysensor/usb.py @@ -4,6 +4,7 @@ from binascii import hexlify, unhexlify from enum import Enum from struct import unpack +from time import monotonic import usb.core as ucore from usb.core import USBError @@ -122,8 +123,10 @@ def read_82(self): def cancel(self): self.cancel = True - def wait_int(self): + # With cancellable=False a cancel request is ignored and the wait gives up after max_wait seconds instead. + def wait_int(self, cancellable: bool = True, max_wait: float = 5): self.cancel = False + deadline = monotonic() + max_wait while True: try: @@ -133,7 +136,10 @@ def wait_int(self): return resp except USBError as e: if e.errno == errno.ETIMEDOUT: - if self.cancel: + if cancellable: + if self.cancel: + raise CancelledException() + elif monotonic() > deadline: raise CancelledException() else: raise e