Skip to content

Commit

Permalink
fix(transport): fix node bridge segfault when disconnected while butt…
Browse files Browse the repository at this point in the history
…on request displayed on device
  • Loading branch information
mroz22 committed Nov 13, 2024
1 parent c1d1f8f commit 6647c41
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/transport/src/api/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class UsbApi extends AbstractApi {
}

public async read(path: string, signal?: AbortSignal) {
const device = this.findDevice(path);
const device = await this.findDevice(path);
if (!device) {
return this.error({ error: ERRORS.DEVICE_NOT_FOUND });
}
Expand Down Expand Up @@ -227,7 +227,7 @@ export class UsbApi extends AbstractApi {
}

public async write(path: string, buffer: Buffer, signal?: AbortSignal) {
const device = this.findDevice(path);
const device = await this.findDevice(path);
if (!device) {
return this.error({ error: ERRORS.DEVICE_NOT_FOUND });
}
Expand Down Expand Up @@ -285,7 +285,7 @@ export class UsbApi extends AbstractApi {
}

private async openInternal(path: string, first: boolean, signal?: AbortSignal) {
const device = this.findDevice(path);
const device = await this.findDevice(path);
if (!device) {
return this.error({ error: ERRORS.DEVICE_NOT_FOUND });
}
Expand Down Expand Up @@ -354,7 +354,7 @@ export class UsbApi extends AbstractApi {
}

public async closeDevice(path: string) {
let device = this.findDevice(path);
let device = await this.findDevice(path, true);
if (!device) {
return this.error({ error: ERRORS.DEVICE_NOT_FOUND });
}
Expand All @@ -364,6 +364,8 @@ export class UsbApi extends AbstractApi {
if (device.opened) {
if (!this.debugLink) {
try {
// https://github.com/trezor/trezor-suite/issues/15336
await createTimeoutPromise(500);
// NOTE: `device.reset()` interrupts transfers for all interfaces (debugLink and normal)
await device.reset();
} catch (err) {
Expand All @@ -374,7 +376,7 @@ export class UsbApi extends AbstractApi {
}
}

device = this.findDevice(path);
device = await this.findDevice(path);
if (device?.opened) {
try {
const interfaceId = this.debugLink ? DEBUGLINK_INTERFACE_ID : INTERFACE_ID;
Expand All @@ -391,7 +393,7 @@ export class UsbApi extends AbstractApi {
// ignore
}
}
device = this.findDevice(path);
device = await this.findDevice(path);
if (device?.opened) {
try {
this.logger?.debug(`usb: device.close`);
Expand All @@ -414,7 +416,10 @@ export class UsbApi extends AbstractApi {
return this.success(undefined);
}

private findDevice(path: string) {
private async findDevice(path: string, refresh = false) {
if (refresh) {
await this.enumerate();
}
const device = this.devices.find(d => d.path === path);
if (!device) {
return;
Expand Down

0 comments on commit 6647c41

Please sign in to comment.