Properly handle shutdown sequence in both prototype and dbus service.

This commit is contained in:
Viktor Dragomiretskyy 2020-07-07 14:07:19 +12:00
parent 7f14da61a8
commit 746ef2888d
4 changed files with 27 additions and 21 deletions

View file

@ -159,10 +159,6 @@ usb.trace_enabled = True
tls.trace_enabled = True tls.trace_enabled = True
svc = Device() svc = Device()
try: loop.run()
loop.run()
finally:
reboot()
raise
print("Normal exit") print("Normal exit")

View file

@ -1,16 +1,33 @@
import atexit
from proto9x.usb import usb from proto9x.usb import usb
from proto9x.tls import tls from proto9x.tls import tls
from proto9x.sensor import sensor from proto9x.sensor import sensor
from proto9x.sensor import reboot
from proto9x.flash import read_tls_flash from proto9x.flash import read_tls_flash
from usb import core as usb_core
def close():
if usb.dev is not None:
# Send the reboot command before closing the device.
# Without it the sensor seems to keep creating new TLS sessions and eventually runs out of memory.
# The reboot command may fail if we're shutting down because the device is already gone.
try:
reboot()
finally:
usb.close()
def open(): def open():
usb.open() usb.open()
usb.send_init() usb.send_init()
# We must register atexit only after we opened usb device,
# so that our handler is called before pyusb's one and we can still talk to the device
atexit.register(close)
tls.parseTlsFlash(read_tls_flash()) tls.parseTlsFlash(read_tls_flash())
tls.open() tls.open()
sensor.open() sensor.open()

View file

@ -39,6 +39,14 @@ class Usb():
self.thread.daemon = True self.thread.daemon = True
self.thread.start() self.thread.start()
def close(self):
if self.dev is not None:
try:
self.dev.reset()
self.dev = None
finally:
self.thread.join()
def usb_dev(self): def usb_dev(self):
return self.dev return self.dev

View file

@ -1,4 +1,3 @@
from proto9x.tls import tls from proto9x.tls import tls
from proto9x.usb import usb from proto9x.usb import usb
from proto9x.db import db from proto9x.db import db
@ -80,17 +79,3 @@ def enroll(sid, finger):
print('Created a finger record with dbid %d' % recid) print('Created a finger record with dbid %d' % recid)
# can't use atexit as it conflicts with atexit installed by libusb
class Blah:
def __init__(self):
self.tls=tls
def __del__(self):
if usb.dev is not None:
print('Rebooting device...')
try:
reboot()
except:
pass
blah=Blah()