diff --git a/hwilib/commands.py b/hwilib/commands.py index 51e55b2..53ce649 100644 --- a/hwilib/commands.py +++ b/hwilib/commands.py @@ -32,11 +32,14 @@ def get_client(device_type, device_path, password=''): class_name = device_type.capitalize() module = device_type.lower() + client = None try: imported_dev = importlib.import_module('.devices.' + module, __package__) client_constructor = getattr(imported_dev, class_name + 'Client') client = client_constructor(device_path, password) except ImportError as e: + if client: + client.close() raise UnknownDeviceError('Unknown device type specified') return client @@ -63,6 +66,7 @@ def find_device(device_path, password='', device_type=None, fingerprint=None): for d in devices: if device_type is not None and d['type'] != device_type: continue + client = None try: client = get_client(d['type'], d['path'], password) master_xpub = client.get_pubkey_at_path('m/0h')['xpub'] @@ -73,6 +77,8 @@ def find_device(device_path, password='', device_type=None, fingerprint=None): else: return client except: + if client: + client.close() pass # Ignore things we wouldn't get fingerprints for return None diff --git a/hwilib/devices/coldcard.py b/hwilib/devices/coldcard.py index c0c8735..8a7f772 100644 --- a/hwilib/devices/coldcard.py +++ b/hwilib/devices/coldcard.py @@ -200,16 +200,20 @@ def enumerate(password=''): d_data['type'] = 'coldcard' d_data['path'] = path + client = None try: client = ColdcardClient(path) master_xpub = client.get_pubkey_at_path('m/0h')['xpub'] d_data['fingerprint'] = get_xpub_fingerprint_hex(master_xpub) - client.close() except Exception as e: d_data['error'] = "Could not open client or get fingerprint information: " + str(e) + if client: + client.close() + results.append(d_data) # Check if the simulator is there + client = None try: client = ColdcardClient(CC_SIMULATOR_SOCK) master_xpub = client.get_pubkey_at_path('m/0h')['xpub'] @@ -219,10 +223,11 @@ def enumerate(password=''): d_data['type'] = 'coldcard' d_data['path'] = CC_SIMULATOR_SOCK results.append(d_data) - client.close() except RuntimeError as e: if str(e) == 'Cannot connect to simulator. Is it running?': pass else: raise e + if client: + client.close() return results diff --git a/hwilib/devices/digitalbitbox.py b/hwilib/devices/digitalbitbox.py index bf120ec..7a8ab6f 100644 --- a/hwilib/devices/digitalbitbox.py +++ b/hwilib/devices/digitalbitbox.py @@ -497,6 +497,7 @@ def enumerate(password=''): d_data['type'] = 'digitalbitbox' d_data['path'] = path + client = None try: client = DigitalbitboxClient(path, password) @@ -507,9 +508,11 @@ def enumerate(password=''): else: master_xpub = client.get_pubkey_at_path('m/0h')['xpub'] d_data['fingerprint'] = get_xpub_fingerprint_hex(master_xpub) - client.close() except Exception as e: d_data['error'] = "Could not open client or get fingerprint information: " + str(e) + if client: + client.close() + results.append(d_data) return results diff --git a/hwilib/devices/keepkey.py b/hwilib/devices/keepkey.py index 1b8ae0a..3ff53f1 100644 --- a/hwilib/devices/keepkey.py +++ b/hwilib/devices/keepkey.py @@ -418,6 +418,7 @@ def enumerate(password=''): d_data['type'] = 'keepkey' d_data['path'] = path + client = None try: client = KeepkeyClient(path, password) client.client.init_device() @@ -426,11 +427,13 @@ def enumerate(password=''): d_data['fingerprint'] = get_xpub_fingerprint_hex(master_xpub) else: d_data['error'] = 'Not initialized' - client.close() except Exception as e: if str(e) == 'Unsupported device': continue d_data['error'] = "Could not open client or get fingerprint information: " + str(e) + if client: + client.close() + results.append(d_data) return results diff --git a/hwilib/devices/ledger.py b/hwilib/devices/ledger.py index 17f6cab..1f8b56f 100644 --- a/hwilib/devices/ledger.py +++ b/hwilib/devices/ledger.py @@ -287,13 +287,16 @@ def enumerate(password=''): d_data['type'] = 'ledger' d_data['path'] = path + client = None try: client = LedgerClient(path, password) master_xpub = client.get_pubkey_at_path('m/0h')['xpub'] d_data['fingerprint'] = get_xpub_fingerprint_hex(master_xpub) - client.close() except Exception as e: d_data['error'] = "Could not open client or get fingerprint information: " + str(e) + if client: + client.close() + results.append(d_data) return results diff --git a/hwilib/devices/trezor.py b/hwilib/devices/trezor.py index f6e346a..e950264 100644 --- a/hwilib/devices/trezor.py +++ b/hwilib/devices/trezor.py @@ -365,6 +365,7 @@ def enumerate(password=''): d_data['type'] = 'trezor' d_data['path'] = dev.get_path() + client = None try: client = TrezorClient(d_data['path'], password) client.client.init_device() @@ -373,12 +374,14 @@ def enumerate(password=''): d_data['fingerprint'] = get_xpub_fingerprint_hex(master_xpub) else: d_data['error'] = 'Not initialized' - client.close() except TypeError as e: if dev.get_path().startswith('udp:'): continue except Exception as e: d_data['error'] = "Could not open client or get fingerprint information: " + str(e) + if client: + client.close() + results.append(d_data) return results