Always close clients if they exist

This commit is contained in:
Andrew Chow 2019-01-31 15:31:24 -05:00
parent 56252a6044
commit 00e30c856d
6 changed files with 29 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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