From ffb4908898459be4d57dd290699538ebdbdf943c Mon Sep 17 00:00:00 2001 From: Martin Varmuza Date: Mon, 11 Nov 2024 11:55:34 +0100 Subject: [PATCH] fix(transport): usb - update reference to device after every async operation --- packages/transport/src/api/usb.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/transport/src/api/usb.ts b/packages/transport/src/api/usb.ts index 7cd17860a9e..c2b0821106f 100644 --- a/packages/transport/src/api/usb.ts +++ b/packages/transport/src/api/usb.ts @@ -354,7 +354,7 @@ export class UsbApi extends AbstractApi { } public async closeDevice(path: string) { - const device = this.findDevice(path); + let device = this.findDevice(path); if (!device) { return this.error({ error: ERRORS.DEVICE_NOT_FOUND }); } @@ -362,13 +362,24 @@ export class UsbApi extends AbstractApi { this.logger?.debug(`usb: closeDevice. device.opened: ${device.opened}`); if (device.opened) { - try { - const interfaceId = this.debugLink ? DEBUGLINK_INTERFACE_ID : INTERFACE_ID; - this.logger?.debug(`usb: device.releaseInterface: ${interfaceId}`); - if (!this.debugLink) { + if (!this.debugLink) { + try { // NOTE: `device.reset()` interrupts transfers for all interfaces (debugLink and normal) await device.reset(); + } catch (err) { + this.logger?.error( + `usb: device.reset error ${err}. device: ${this.formatDeviceForLog(device)}`, + ); } + } + } + + device = this.findDevice(path); + if (device?.opened) { + try { + const interfaceId = this.debugLink ? DEBUGLINK_INTERFACE_ID : INTERFACE_ID; + this.logger?.debug(`usb: device.releaseInterface: ${interfaceId}`); + await device.releaseInterface(interfaceId); this.logger?.debug( `usb: device.releaseInterface done: ${interfaceId}. device: ${this.formatDeviceForLog(device)}`, @@ -380,7 +391,8 @@ export class UsbApi extends AbstractApi { // ignore } } - if (device.opened) { + device = this.findDevice(path); + if (device?.opened) { try { this.logger?.debug(`usb: device.close`); await device.close();