Skip to content

init: recover from a stuck sensor, stop restart loops, and quiet the journal - #290

Open
alhasapi wants to merge 8 commits into
uunicorn:masterfrom
alhasapi:fix/init-recovery-and-log-flood
Open

alhasapi wants to merge 8 commits into
uunicorn:masterfrom
alhasapi:fix/init-recovery-and-log-flood

Conversation

@alhasapi

@alhasapi alhasapi commented Oct 9, 2026

Copy link
Copy Markdown

What this fixes

On a Lenovo 06cb:009a (Synaptics Metallica MIS) running Python 3.14, the daemon
can come up with an unresponsive sensor and then crash-loop forever while
flooding the journal:

init.open() -> open_common() -> init_flash() -> get_flash_info()
  -> tls.cmd('3e') -> usb.cmd() -> dev.write(1, out)
usb.core.USBTimeoutError: [Errno 110] Operation timed out

atexit.register(close) is only reached after a successful open_common(),
and close() is the only place that resets the USB device. So a failed init
never reset the sensor, and every systemd restart met the same stuck state.

Machine-generated evidence from the affected host (10 days):

metric value
Scheduled restart job 12,701
max systemd restart counter 5,075
starts on 2026-10-06 5,267
USBTimeoutError: [Errno 110] lines that day 10,530
journal pinned at the default 4 GiB ceiling
unit --debug only, no crash loop (2026-10-01) 113,639 lines / ~106 MiB

Changes (one commit per concern)

  1. flash: tolerate short/empty flash-info reply and never format an unknown IC
    A 0000-only 3e reply crashed unpack(). Return an empty FlashInfo,
    re-query once after reset_blob, and refuse to format if the flash IC is
    still unknown instead of guessing flash parameters.
  2. usb: retry transient 0x0104/0x0401 busy status during send_init
    Bounded cmd_retry_busy() for the busy statuses and USB timeouts; stop
    asserting on the init_hardcoded reply so the clean-slate fallback works.
  3. init: run send_init() before init_flash()
    The ordering that makes protected-flash commands time out on 06cb:009a.
  4. init: reset and re-open the sensor instead of crash-looping
    usb.reset()/usb.reopen() (re-discovery, so a re-enumerated device is
    found again) plus _open_with_recovery(): a bounded number of attempts with
    a hard reset/re-open in between. RebootException still propagates.
  5. dbus: re-acquire the USB device on Resume
    init.resume() re-opens the device and rebuilds state instead of reusing a
    stale post-suspend PyUSB handle.
  6. service: drop --debug default and throttle restarts
    Remove --debug, set Restart=on-failure, RestartSec=5, and bound the
    storm with StartLimitIntervalSec=300 / StartLimitBurst=5.

Relationship to existing work (nothing silently duplicated)

This is a consolidated PR that adopts and credits the open proposals and
adds the pieces none of them covered:

Happy to split or rebase any commit if you prefer a different grouping; they are
deliberately independent.

Testing

  • tests/ (new, pytest, no hardware required): 15 tests —
    full/short flash replies, refusing to format an unknown IC, busy/timeout
    retry bounds, non-timeout errors not retried, init ordering, bounded
    reset/re-open recovery, RebootException propagation, resume re-acquire,
    and the unit file (no --debug, throttled restarts).
  • python -m py_compile clean.
  • Hardware validation on the reporter's 06cb:009a (reboot + suspend/resume
    matrix) is in progress and will be posted here as a follow-up comment.

Marked draft until that hardware run is attached.

Notes

  • The repo has no CI; the tests run with python -m pytest and a local
    pytest.ini.
  • No behavior change for already-working sensors: the normal path is unchanged,
    recovery only engages after a failure.

…own IC

The 06cb:009a sensor replies to the '3e' (get_flash_info) command with only
the 2-byte status word (0000) and no 14-byte header/partition table while its
flash is uninitialized. get_flash_info() unconditionally unpacked the header
and crashed with struct.error, leaving the service in a restart loop.

Return an empty FlashInfo (no IC, no partitions) for short replies, and make
init_flash() re-query once after reset_blob. If the flash IC is still unknown,
refuse to format instead of guessing flash parameters (size/sector size/erase
command), which could corrupt the sensor flash.

Builds on the analysis and patch in uunicorn#272 and the
short-reply handling in uunicorn#273/uunicorn#245.

Tests: tests/test_flash_info.py (no hardware required).
After a wake-up or reset the 06cb:009a sensor answers the first command(s)
with a transient busy status, which assert_status() treated as a fatal error.
Add cmd_retry_busy(), a bounded retry helper that also retries USB timeout
errors, and use it for RomInfo.get() and get_fw_info() in send_init().

Stop asserting on the init_hardcoded response: a non-zero status there means
'firmware not loaded yet', which the following 4302 check already handles,
so asserting made the clean-slate fallback unreachable (as analysed in uunicorn#272
and fixed in uunicorn#245/uunicorn#273).

Tests: tests/test_usb_retry.py.
On 06cb:009a the protected-flash commands issued by init_flash() time out
when the hardcoded init/firmware blobs from send_init() have not been loaded
first. Reordering the sequence fixes initialization on that hardware (as
tested in uunicorn#272).
When the sensor stops answering, init() raised USBTimeoutError after the 15s
USB timeout. atexit.register(close) is only reached after a successful
open_common(), so the only code that resets the USB device (usb.close()) never
ran; systemd restarted the service straight back into the same stuck state.
On this machine that produced 12,701 restarts and a multi-GB journal.

Add usb.reset()/usb.reopen() (re-discovery, so a re-enumerated device is
found again) and wrap open()/open_devpath() in _open_with_recovery(), which
retries a bounded number of times, resetting and re-opening the device in
between. RebootException (normal post-format reboot) is still propagated.
atexit registration is now idempotent.

Tests: tests/test_init_recovery.py.
Resume() called init.open_common(), which skips usb.open() and reuses the
pre-suspend PyUSB handle. After a resume the kernel has reset/re-enumerated
the device, so operations failed with 'USBError: [Errno 19] No such device'
and the service stayed up but unusable.

Add init.resume(), which resets TLS state, re-acquires the device (usb.reopen()
with a fallback to usb.open()) and re-runs the bounded init recovery. This is
the targeted change discussed in uunicorn#249/uunicorn#285 without the extra systemd unit.

Tests: tests/test_resume.py.
The packaged unit started dbus-service with --debug, which enables DEBUG-level
hex dumps of every USB/TLS command. On 2026-10-01 the unit had only 2 starts
yet wrote 113,639 journal lines (~106 MiB), and it also dumped raw key
material. Debug tracing stays available via an explicit override.

Set Restart=on-failure with RestartSec=5 and bound the restart storm with
StartLimitIntervalSec=300 / StartLimitBurst=5. Previously the unit relied on
Restart=no (which Arch patches to on-failure without a delay); with a 15s
per-attempt init and the default StartLimitBurst=5/10s, the limit never
tripped no matter how many times the service failed.

Tests: tests/test_service_unit.py.
@alhasapi

alhasapi commented Oct 9, 2026

Copy link
Copy Markdown
Author

Test run on the PR branch (no hardware):

tests/test_flash_info.py::test_get_flash_info_parses_full_reply PASSED   [  6%]
tests/test_flash_info.py::test_get_flash_info_short_reply_returns_empty_flashinfo PASSED [ 13%]
tests/test_flash_info.py::test_init_flash_refuses_to_format_without_identified_ic PASSED [ 20%]
tests/test_init_recovery.py::test_open_with_recovery_resets_and_retries PASSED [ 26%]
tests/test_init_recovery.py::test_open_with_recovery_gives_up_bounded PASSED [ 33%]
tests/test_init_recovery.py::test_reboot_exception_is_not_retried PASSED [ 40%]
tests/test_init_recovery.py::test_open_common_loads_firmware_before_flash PASSED [ 46%]
tests/test_resume.py::test_resume_reopens_usb_and_reinitializes PASSED   [ 53%]
tests/test_resume.py::test_resume_falls_back_to_fresh_open PASSED        [ 60%]
tests/test_service_unit.py::test_debug_is_not_enabled_by_default PASSED  [ 66%]
tests/test_service_unit.py::test_restart_is_throttled PASSED             [ 73%]
tests/test_usb_retry.py::test_retries_transient_busy_status PASSED       [ 80%]
tests/test_usb_retry.py::test_retries_transient_usb_timeout PASSED       [ 86%]
tests/test_usb_retry.py::test_non_timeout_usb_error_is_not_retried PASSED [ 93%]
tests/test_usb_retry.py::test_bounded_when_sensor_stays_busy PASSED      [100%]
============================== 15 passed in 0.25s ==============================

Commit range:

bcddb69 service: drop --debug default and throttle restarts
7bebaab dbus: re-acquire the USB device on Resume
5ee8b4f init: reset and re-open the sensor instead of crash-looping
ce41c1a init: run send_init() before init_flash()
6bc143f usb: retry transient 0x0104/0x0401 busy status during send_init
4c0e245 flash: tolerate short/empty flash-info reply and never format an unknown IC

@alhasapi

alhasapi commented Oct 9, 2026

Copy link
Copy Markdown
Author

Local validation (part 1) — applied on the reporting machine

06cb:009a, Arch/Omarchy, Python 3.14.7, pyusb 1.3.1, libusb 1.0.30.
Patched build installed over 0.15 via a local AUR overlay carrying this exact
diff; journald capped at 500 MiB.

Before (from the journal): 12,701 scheduled restarts, max counter 5,075,
5,267 starts on 2026-10-06, 10,530 USBTimeoutError [Errno 110] lines that day,
journal pinned at 4.1 GiB.

After install:

  • NRestarts=0, ActiveState=active through the whole run.
  • 10x net.reactivated.Fprint.Manager.Resume (exercises the new
    init.resume() re-acquire path) and 2x systemctl restart python3-validity (exercises cold init.open()): NRestarts=0 every time,
    no init tracebacks, fprintd-list still lists all 4 enrolled prints.
  • Patched unit output since start: 96 lines / 9.9 KB, 0 init tracebacks;
    the only 7 DEBUG:root lines are the old process shutting down.
  • journalctl --disk-usage → 477.3 MiB (from 4.1 GiB).
resume 1..10: NRestarts=0 active=active OK
restart 1:    NRestarts=0 active=active
restart 2:    NRestarts=0 active=active

Still to do before this comes out of draft: 2 full reboots and >=10 real
suspend/resume cycles with a verification after each. The 10 simulated resumes

  • 2 restarts cover the code paths, but not real USB re-enumeration.

close() only caught RebootException, so a USB error in reboot() (the device is
often already reset/gone during shutdown or suspend) produced an
"Exception ignored in atexit callback" traceback on every stop. Log it at
debug level instead; the USB close still runs in finally.

Tests: tests/test_init_recovery.py.
A cancelled verify (or one that times out waiting for a finger) raised
CancelledException, fell into the generic handler and logged a full chained
traceback containing the 30s USBTimeoutError. This is normal operation and was
a large part of the journal volume. Handle CancelledException and USBError
explicitly with a single log line and keep the existing status callbacks.

Tests: the verify/enroll paths are DBus-bound; behaviour is covered by the
hardware suspend/resume matrix rather than unit tests.
@alhasapi

alhasapi commented Oct 9, 2026

Copy link
Copy Markdown
Author

Two more commits added, plus a fuller local suspend/resume run.

Added commits

  • init: never print a traceback from the atexit reboot handler — close()
    only caught RebootException, so a USB error in reboot() (the device is
    usually already reset/gone during shutdown or suspend) produced
    Exception ignored in atexit callback on every stop.
  • dbus: log expected verify/enroll cancel and timeout without a traceback —
    a cancelled verify (or one timing out waiting for a finger) raised
    CancelledException, hit the generic handler and logged a full chained
    traceback containing the 30s USBTimeoutError. That was a large share of the
    journal volume. Now handled explicitly with a single log line; status
    callbacks unchanged. 17 unit tests total.

Real suspend/resume matrix (no longer simulated)

On the same 06cb:009a host, with the old python3-validity-suspend-hotfix
restart disabled so the new in-process path is exercised:

  • 14 real suspend/resume cycles (kernel PM: suspend entry (deep) /
    PM: suspend exit confirmed for each).
  • open-fprintd-resume.service calls Manager.Resume() -> python-validity
    Device.Resume() -> init.resume(); the sensor re-initializes in the same
    process (Opening sensor: 57K0 FM-3367-001 after every resume).
  • Same PIDs before/after every cycle (no restart), NRestarts=0 throughout,
    0 init tracebacks, fprintd-list returns all 4 enrolled prints immediately
    after wake.
  • journalctl --disk-usage = 432 MiB (from 4.1 GiB).

Remaining before un-drafting: 2 full reboots. A reboot ends the agent session,
so those are being done by the machine owner and the results will be posted.

@alhasapi

alhasapi commented Oct 9, 2026

Copy link
Copy Markdown
Author

Final hardware validation — pulling out of draft

Two full reboots and 14 real suspend/resume cycles on the affected
06cb:009a machine. Everything below is machine-generated.

Reboods (2/2)

boot id NRestarts services init errors DEBUG dumps prints
316dddc6… 0 active/active 0 0 4
c4977e89… 0 active/active 0 0 4

Suspend/resume (14/14)

  • Each cycle confirmed by kernel PM: suspend entry (deep) / PM: suspend exit.
  • open-fprintd-resume.service → Manager.Resume() → Device.Resume() →
    init.resume(); the daemon re-initializes in the same PID
    (Opening sensor: 57K0 FM-3367-001 after every wake).
  • Same PIDs before/after every cycle, NRestarts=0, 0 init tracebacks,
    all fingerprints listed immediately after wake.
  • The old python3-validity-suspend-hotfix restart unit is disabled; the
    in-process path replaces it (and removes the ~2 s lock-screen race it caused).

Journal volume

  • journalctl --disk-usage: 452 MiB (was 4.1 GiB; capped at 500 MiB).
  • Projected after fix: ~2,731 lines/day, i.e. -97.6% vs the
    debug-only day (113,639 lines) and -98.9% vs the worst crash-loop day
    (258,073 lines).

With the two extra commits (atexit hardening + expected cancel/timeout logging),
that is 8 commits and 17 unit tests.

@alhasapi
alhasapi marked this pull request as ready for review October 9, 2026 09:13
@alhasapi

alhasapi commented Oct 9, 2026

Copy link
Copy Markdown
Author

A ready-to-test overlay with this exact patch, the Arch packaging to apply it,
and the raw validation evidence is now public:

https://github.com/alhasapi/python-validity-fix

  • patch/fix.patch — the combined diff (same as this PR's branch)
  • aur/ — PKGBUILD overlay that applies it to 0.15
  • local/ — install/validate scripts + systemd/journald drop-ins
  • docs/ — reboot and 14x suspend/resume evidence, journal-volume numbers

Useful if you want to reproduce the hardware validation without assembling the
patch by hand.

@medden

medden commented Oct 24, 2026

Copy link
Copy Markdown

Tested on hardware: ThinkPad T480, Synaptics 06cb:009a. This fixes the Failed: 0401 init failure.

System

  • Arch Linux (Omarchy 4.0.6), kernel 7.2.8
  • python 3.14.8, libusb 1.0.30, python-pyusb 1.3.1
  • open-fprintd 0.7, fprintd-clients-git 1.90.1
  • BIOS N24ET76W (1.51); sensor reports 57K0 FM-3367-001, FWExt 1.2

Before (AUR python-validity 0.15, unpatched)

  • dbus-service crash-looped. The first 3e command returned 0104, so get_flash_info() raised Exception: Failed: 0401. After a few restarts it hit USBTimeoutError.
  • factory-reset.py also failed with Failed: 0401. A BIOS "Reset Fingerprint Data" plus a full power-off didn't change anything.

After (0.15 + this PR, applied with git apply --exclude='debian/*' --exclude='tests/*' --exclude=pytest.ini)

  • First start: Clean slate, then No firmware detected. Uploading..., then Loaded FWExt version 1.2. The service exited, as expected after the firmware upload.
  • Second start: Flash has 5 partitions, Opening sensor: 57K0 FM-3367-001, then calibration. fprintd-list sees the device.
  • fprintd-enroll gave enroll-completed. fprintd-verify gave verify-match 4 out of 4 times.
  • pam_fprintd works for sudo (3 out of 3).
  • I haven't tested suspend/resume with the patched driver yet. I'll follow up here once I have.

The service file changes didn't apply to the AUR package's unit, so I applied them with sed in the PKGBUILD (drop --debug, add RestartSec=5, StartLimitIntervalSec=300, StartLimitBurst=5).

Thanks for this, and for the root cause in #272. It would be great to see it merged.

@e2jk

e2jk commented Nov 6, 2026

Copy link
Copy Markdown

Hardware validation of this PR on a ThinkPad X1 Carbon 6th, Synaptics 06cb:009a: enrollment, fingerprint authentication, and actual suspend/resume now work.

Environment: Arch/Omarchy, kernel 7.2.15-2-omarchy, Python 3.14.9, libusb 1.0.30, PyUSB 1.3.1, cryptography 50.0.2; open-fprintd 0.7 and python-validity 0.15 with this PR applied in a local package. All 17 included tests passed.

Before applying the PR, initialization produced Failed: 0401 and USB timeouts after fetching the required Lenovo firmware with validity-sensors-firmware. With the PR it progressed to firmware upload. An explicit service restart during troubleshooting interrupted that upload, so the following Signature validation failed: 044f was not an independent regression in this PR. A reader factory reset recovered it: flash formatting, FWExt 1.2 upload, sensor calibration, and device registration completed.

The factory-reset utility itself returned USBError: [Errno 19] No such device while waiting for the reset response, but the reader re-enumerated and initialization completed automatically through udev.

Verified afterward:

  • Enrollment completed. The first enrollment did not match in verification. After disabling USB autosuspend for this device and enrolling afresh, the enrolled finger matched and a different finger was rejected. These were combined changes, so this does not isolate the cause of the initial mismatches.
  • The configured lock-screen, sudo, and polkit PAM stacks each authenticated the enrolled finger successfully.
  • An actual Omarchy screen lock was unlocked by fingerprint; the shell logged successful fingerprint PAM authentication.
  • A real system suspend/resume completed with open-fprintd-suspend.service and open-fprintd-resume.service enabled, and the packaged restart-on-resume hotfix disabled to avoid overlapping recovery. The reader resumed successfully and a second actual fingerprint screen unlock succeeded. Password fallback remains configured.
  • Cold reboot persistence has not been tested.

One remaining recovery observation: after the interrupted-upload signature failure, attempts 2–5 in the same process reported Unexpected TLS version 4 0 via init_flash() -> get_flash_info() -> tls.cmd() -> tls.app(). _open_with_recovery() resets/reopens USB but does not call tls.reset(), so the previous attempt's secure_rx/secure_tx flags may survive the hardware reset. Resetting TLS state between attempts may be worth checking; this is an observation and code inspection, not a verified fix.

Codex, for e2jk

@e2jk

e2jk commented Nov 8, 2026

Copy link
Copy Markdown

Follow-up: scan cancellation race after suspend/resume

Follow-up to my earlier hardware validation on a ThinkPad X1 Carbon 6th,
Synaptics 06cb:009a, using python-validity 0.15 with PR #290 and open-fprintd
0.7 on Arch/Omarchy.

After an overnight suspend, the driver reported resume complete but fingerprint
unlock repeatedly requested another scan. Direct verification later matched
without a driver restart. Further short suspend tests reproduced repeated
capture retries; a fresh verification session subsequently succeeded.

Adding exception-only logging to VerifyStart's capture callback exposed:

Unexpected TLS version 4 0
App payload before secure connection established
Failed: 0450
wait_start: Unexpected interrupt type 0200401000

The TLS errors appeared while the pre-suspend verification worker was still
active around resume initialization. These are capture/protocol failures,
rather than a completed fingerprint comparison returning no match.

Code inspection found two related synchronization problems:

  • Backend Suspend does not stop the verification worker. Resume resets shared
    TLS state and resets/reopens USB without joining that worker. Cancel only
    sets a flag, so its return does not establish that the worker has exited.
  • Usb.wait_int clears the cancellation flag on entry and otherwise checks it
    only after a read timeout. Cancellation can therefore be lost, including
    when the reader continues delivering interrupts.

The observed ordering and code strongly suggest a scan/resume race; we have
not captured a complete thread-level trace proving every failing interleaving.

I prepared a local patch on top of PR #290 that tracks verify/enrollment
workers, cancels and joins them before suspend/resume or another scan, keeps
cancellation asserted until the old worker exits, and observes cancellation
at interrupt-wait entry and when an interrupt arrives. Capture checks for
cancellation before issuing commands. If a worker cannot stop within three
seconds, the candidate exits with failure without concurrent atexit USB cleanup,
allowing the configured systemd Restart=on-failure to recover the backend.
That fallback has not been hardware-tested and is a design choice for review.

Validation so far:

  • All 21 automated tests pass, including four new simulated-reader tests for
    stopping before resume, cancellation at wait entry, cancellation during an
    interrupt read, and cleanup before starting a replacement worker.
  • One real suspend cycle with the patch: old scan stopped before sleep;
    resume completed without capture retries or protocol errors; fingerprint
    PAM authentication unlocked the screen successfully.
  • Kernel resume at 09:02:12.058, fingerprint prompt delivered to PAM at
    09:02:12.939, fingerprint authentication succeeded at 09:02:15.284
    (Nov 7, 2026, Europe/Brussels). These timings do not independently measure
    when the physical display or visible prompt appeared.
  • Enrollment and PAM configuration were unchanged. No factory reset or
    re-enrollment was performed for this investigation.

On November 8, the machine owner reported that things have continued working
correctly and authorized sharing this fix. We did not record an exact count or
duration for those subsequent uses, so the detailed timing above remains one
instrumented suspend cycle. Cold boot, new enrollment, and the forced-exit
fallback have not been independently validated with this candidate.

The complete incremental patch, including regression tests, is below. Apply it
after PR #290 to python-validity 0.15. Feedback on the cancellation/join approach
and timeout recovery policy would be welcome.

resume-worker.patch (implementation and four regression tests)
--- a/dbus_service/dbus-service
+++ b/dbus_service/dbus-service
@@ -49,6 +49,27 @@
     def __init__(self, bus_name: dbus.Bus, config: typing.Dict[str, typing.Any]):
         dbus.service.Object.__init__(self, bus_name, '/io/github/uunicorn/Fprint/Device')
         self.config = config
+        self._worker = None
+        self._suspended = False
+
+    def _stop_worker(self):
+        # Cancellation must stay asserted until the old worker has exited.
+        sensor.cancel()
+        if self._worker is not None:
+            self._worker.join(timeout=3.0)
+            if self._worker.is_alive():
+                logging.error('Scan worker failed to stop; restarting backend')
+                # Do not run atexit USB cleanup concurrently with a stuck worker.
+                os._exit(1)
+            self._worker = None
+
+    def _start_worker(self, target):
+        if self._suspended:
+            raise RuntimeError('Reader is suspended')
+        self._stop_worker()
+        usb.cancel = False
+        self._worker = Thread(target=target, daemon=True)
+        self._worker.start()
 
     def user2identity(self, user: str) -> SidIdentity:
         """Compute UID to SID mapping"""
@@ -67,13 +88,20 @@
     @dbus.service.method(dbus_interface=INTERFACE_NAME, in_signature='', out_signature='')
     def Suspend(self):
         logging.debug('In Suspend')
+        self._suspended = True
+        self._stop_worker()
+        logging.info('Suspend: scan worker stopped')
 
     @dbus.service.method(dbus_interface=INTERFACE_NAME, in_signature='', out_signature='')
     def Resume(self):
         logging.debug('In Resume')
+        self._suspended = True
+        self._stop_worker()
         # Re-acquire the USB device and rebuild sensor state; open_common() alone
         # reuses a stale handle after the device re-enumerates (see #249).
         init.resume()
+        self._suspended = False
+        logging.info('Resume: reader ready, no old scan worker')
 
     @dbus.service.method(dbus_interface=INTERFACE_NAME, in_signature="s", out_signature="as")
     def ListEnrolledFingers(self, user):
@@ -113,6 +141,7 @@
         self.VerifyFingerSelected('any')
 
         def update_cb(e):
+            logging.warning('Scan capture retry: %s: %s', type(e).__name__, e)
             self.VerifyStatus('verify-retry-scan', False)
 
         def run():
@@ -136,13 +165,11 @@
                 logging.exception(e)
                 self.VerifyStatus('verify-no-match', True)
 
-        thread = Thread(target=run)
-        thread.daemon = True
-        thread.start()
+        self._start_worker(run)
 
     @dbus.service.method(dbus_interface=INTERFACE_NAME, in_signature='', out_signature='')
     def Cancel(self):
-        sensor.cancel()
+        self._stop_worker()
 
     @dbus.service.method(dbus_interface=INTERFACE_NAME, in_signature='ss', out_signature='')
     def EnrollStart(self, user, finger_name):
@@ -177,9 +204,7 @@
                               winbio_name + ')')
             self.EnrollStatus('enroll-failed', True)
         else:
-            thread = Thread(target=run)
-            thread.daemon = True
-            thread.start()
+            self._start_worker(run)
 
     @dbus.service.signal(dbus_interface=INTERFACE_NAME, signature='sb')
     def VerifyStatus(self, result, done):
--- a/validitysensor/usb.py
+++ b/validitysensor/usb.py
@@ -197,13 +197,15 @@
         self.cancel = True
 
     def wait_int(self):
-        self.cancel = False
-
         while True:
+            if self.cancel:
+                raise CancelledException()
             try:
                 resp = self.dev.read(131, 1024, timeout=100)
                 resp = bytes(resp)
                 self.trace('<int< %s' % hexlify(resp).decode())
+                if self.cancel:
+                    raise CancelledException()
                 return resp
             except USBError as e:
                 if e.errno == errno.ETIMEDOUT:
--- a/validitysensor/sensor.py
+++ b/validitysensor/sensor.py
@@ -693,6 +693,8 @@
         usb.cancel = True
 
     def capture(self, mode: CaptureMode) -> typing.Tuple[int, int, int, int]:
+        if usb.cancel:
+            raise CancelledException()
         try:
             assert_status(tls.app(self.build_cmd_02(mode)))
 
--- /dev/null
+++ b/tests/test_scan_lifecycle.py
@@ -0,0 +1,99 @@
+"""Exercise real lifecycle methods against a blocked simulated USB reader."""
+import ast
+import errno
+import logging
+import os
+from pathlib import Path
+from threading import Event, Thread
+from types import SimpleNamespace
+import typing
+
+import pytest
+from usb.core import USBError
+from validitysensor.usb import Usb, CancelledException
+
+
+def backend(usb, events):
+    # Load the real Device methods without initializing D-Bus or real hardware.
+    source = Path(__file__).parents[1] / 'dbus_service/dbus-service'
+    cls = next(n for n in ast.parse(source.read_text()).body if isinstance(n, ast.ClassDef) and n.name == 'Device')
+    cls.bases = []
+    for method in cls.body:
+        if isinstance(method, ast.FunctionDef):
+            method.decorator_list = []
+    ns = dict(dbus=SimpleNamespace(Bus=object), typing=typing, SidIdentity=object,
+              User=object, Thread=Thread, logging=logging, os=os, usb=usb,
+              sensor=SimpleNamespace(cancel=lambda: setattr(usb, 'cancel', True)),
+              init=SimpleNamespace(resume=lambda: events.append('resume')))
+    exec(compile(ast.Module(body=[cls], type_ignores=[]), str(source), 'exec'), ns)
+    device = ns['Device'].__new__(ns['Device'])
+    device._worker = None
+    device._suspended = False
+    return device
+
+
+def test_suspend_joins_reader_before_resume_and_new_scan():
+    usb = Usb()
+    events = []
+    reading = Event()
+    class Reader:
+        def read(self, *args, **kwargs):
+            reading.set()
+            Event().wait(0.01)
+            raise USBError('timeout', errno=errno.ETIMEDOUT)
+    usb.dev = Reader()
+    device = backend(usb, events)
+    def scan():
+        try:
+            usb.wait_int()
+        except CancelledException:
+            events.append('cancelled')
+    device._start_worker(scan)
+    assert reading.wait(1)
+    device.Suspend()
+    assert events == ['cancelled']
+    assert device._worker is None
+    with pytest.raises(RuntimeError, match='suspended'):
+        device._start_worker(lambda: None)
+    device.Resume()
+    assert events == ['cancelled', 'resume']
+    device._start_worker(lambda: events.append('new scan'))
+    device._worker.join(1)
+    assert events == ['cancelled', 'resume', 'new scan']
+
+
+def test_cancel_is_not_lost_at_wait_entry():
+    usb = Usb()
+    usb.cancel = True
+    usb.dev = SimpleNamespace(read=lambda *a, **k: pytest.fail('USB read after cancellation'))
+    with pytest.raises(CancelledException):
+        usb.wait_int()
+    assert usb.cancel
+
+
+def test_cancel_observed_even_when_interrupt_arrives():
+    usb = Usb()
+    def read(*a, **k):
+        usb.cancel = True
+        return b'\x02\x00'
+    usb.dev = SimpleNamespace(read=read)
+    with pytest.raises(CancelledException):
+        usb.wait_int()
+
+
+def test_second_worker_waits_for_old_worker_cleanup():
+    usb = Usb()
+    events = []
+    started = Event()
+    device = backend(usb, events)
+    def old():
+        started.set()
+        while not usb.cancel:
+            Event().wait(0.01)
+        Event().wait(0.03)
+        events.append('old cleanup')
+    device._start_worker(old)
+    assert started.wait(1)
+    device._start_worker(lambda: events.append('new scan'))
+    device._worker.join(1)
+    assert events == ['old cleanup', 'new scan']

Codex for e2jk

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants