From 8cf1f0463a666c86d024c9a081dbb7f71ea87008 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 14 Oct 2017 16:55:47 +0300 Subject: [PATCH] device: release HID handle before failing --- libagent/device/keepkey_defs.py | 2 +- libagent/device/trezor.py | 11 ++++++++--- libagent/device/trezor_defs.py | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/libagent/device/keepkey_defs.py b/libagent/device/keepkey_defs.py index 48ab192..5ecf127 100644 --- a/libagent/device/keepkey_defs.py +++ b/libagent/device/keepkey_defs.py @@ -2,7 +2,7 @@ # pylint: disable=unused-import,import-error -from keepkeylib.client import CallException as Error +from keepkeylib.client import CallException, PinException from keepkeylib.client import KeepKeyClient as Client from keepkeylib.messages_pb2 import PassphraseAck, PinMatrixAck from keepkeylib.transport_hid import HidTransport as Transport diff --git a/libagent/device/trezor.py b/libagent/device/trezor.py index 7b57207..990398d 100644 --- a/libagent/device/trezor.py +++ b/libagent/device/trezor.py @@ -77,7 +77,12 @@ class Trezor(interface.Device): ' (current: {})') raise ValueError(fmt.format(self, self.required_version, current_version)) - connection.ping(msg='', pin_protection=True) # unlock PIN + try: + connection.ping(msg='', pin_protection=True) # unlock PIN + except Exception as e: + log.exception('ping failed: %s', e) + connection.close() # so the next HID open() will succeed + raise return connection raise interface.NotFoundError('{} not connected'.format(self)) @@ -117,7 +122,7 @@ class Trezor(interface.Device): assert len(result.signature) == 65 assert result.signature[:1] == b'\x00' return result.signature[1:] - except self._defs.Error as e: + except self._defs.CallException as e: msg = '{} error: {}'.format(self, e) log.debug(msg, exc_info=True) raise interface.DeviceError(msg) @@ -136,7 +141,7 @@ class Trezor(interface.Device): assert len(result.session_key) in {65, 33} # NIST256 or Curve25519 assert result.session_key[:1] == b'\x04' return result.session_key - except self._defs.Error as e: + except self._defs.CallException as e: msg = '{} error: {}'.format(self, e) log.debug(msg, exc_info=True) raise interface.DeviceError(msg) diff --git a/libagent/device/trezor_defs.py b/libagent/device/trezor_defs.py index 1baeabd..aeac1ad 100644 --- a/libagent/device/trezor_defs.py +++ b/libagent/device/trezor_defs.py @@ -2,7 +2,7 @@ # pylint: disable=unused-import,import-error -from trezorlib.client import CallException as Error +from trezorlib.client import CallException, PinException from trezorlib.client import TrezorClient as Client from trezorlib.messages_pb2 import PassphraseAck, PinMatrixAck from trezorlib.transport_bridge import BridgeTransport