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 18, 2024
1 parent ffb4908 commit 2422801
Show file tree
Hide file tree
Showing 2 changed files with 14 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
2 changes: 2 additions & 0 deletions packages/transport/tests/apiUsb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('api/usb', () => {
const abortController = new AbortController();
await api.enumerate(abortController.signal);
const promise = api.read('123', abortController.signal);
await Promise.resolve();
abortController.abort();

const result = await promise;
Expand Down Expand Up @@ -82,6 +83,7 @@ describe('api/usb', () => {
const abortController = new AbortController();
await api.enumerate(abortController.signal);
const promise = api.write('123', Buffer.alloc(api.chunkSize), abortController.signal);
await Promise.resolve();
abortController.abort();

const result = await promise;
Expand Down

0 comments on commit 2422801

Please sign in to comment.