Skip to content

Commit 457fa8b

Browse files
committed
usb/sagas: Fix command response race condition
Fix a race condition where a response to a USB command could be missed if it arrives before the USB transferOut() call returns. This is done by using a channel to buffer incoming responses. Fixes: pybricks/support#2467
1 parent fa275b8 commit 457fa8b

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/usb/sagas.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,12 @@ function* handleUsbConnectPybricks(hotPlugDevice?: USBDevice): Generator {
454454
writeCommand.matches(a),
455455
);
456456

457+
// Response may come before request returns, so we need to buffer them
458+
// in a channel to avoid missing responses.
459+
const responseChannel = yield* actionChannel(
460+
usbDidReceivePybricksMessageResponse,
461+
);
462+
457463
for (;;) {
458464
const action = yield* take(chan);
459465

@@ -525,7 +531,7 @@ function* handleUsbConnectPybricks(hotPlugDevice?: USBDevice): Generator {
525531
}
526532

527533
const { response, timeout } = yield* race({
528-
response: take(usbDidReceivePybricksMessageResponse),
534+
response: take(responseChannel),
529535
timeout: delay(1000),
530536
});
531537

0 commit comments

Comments
 (0)