From 638b866636a8e2dad8f3f4581cae04778cfc0973 Mon Sep 17 00:00:00 2001 From: vetalcore Date: Wed, 29 Jan 2025 17:29:46 +0200 Subject: [PATCH] fix: [lw-12138] handle disconnected ledger wallet error --- packages/hardware-ledger/src/LedgerKeyAgent.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/hardware-ledger/src/LedgerKeyAgent.ts b/packages/hardware-ledger/src/LedgerKeyAgent.ts index 8c87d461df2..5bfb7ec3cc1 100644 --- a/packages/hardware-ledger/src/LedgerKeyAgent.ts +++ b/packages/hardware-ledger/src/LedgerKeyAgent.ts @@ -377,12 +377,20 @@ export class LedgerKeyAgent extends KeyAgentBase { } private static attachDisconnectionCleanupHandler(transport: LedgerTransportType) { - const onDisconnect = () => { + const onDisconnect = async () => { transport.off('disconnect', onDisconnect); this.deviceConnections = this.deviceConnections.filter( ({ deviceConnection }) => deviceConnection.transport !== transport ); - void transport.close(); + + try { + await transport.close(); + } catch (error) { + // hw-transport-webusb also adds a disconnect event listener while creating a Ledger transport, + // so calling close() method will throw an error, since the device is already disconnected + if (error instanceof Error && error.message.includes('The device was disconnected.')) return; + throw new errors.TransportError('Failed to close transport after device disconnection', error); + } }; transport.on('disconnect', onDisconnect); }