Skip to content

Commit 37de6a5

Browse files
committed
usb/sagas: Delay connection attempt on hotplug.
If we try to connect too soon, we get security errors.
1 parent 7a6a249 commit 37de6a5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/usb/sagas.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (c) 2025 The Pybricks Authors
2+
// Copyright (c) 2025-2026 The Pybricks Authors
33

44
import { firmwareVersion } from '@pybricks/firmware';
55
import { AnyAction } from 'redux';
@@ -77,6 +77,11 @@ function* handleUsbConnectPybricks(hotPlugDevice?: USBDevice): Generator {
7777
// so that reducers still work correctly.
7878
if (hotPlugDevice !== undefined) {
7979
yield* put(usbHotPlugConnectPybricks());
80+
81+
// Even though we get the connect event, the browser does not
82+
// appear to be ready to open the device right away, so we add a
83+
// small delay before trying to connect.
84+
yield* delay(100);
8085
}
8186

8287
if (navigator.usb === undefined) {
@@ -655,6 +660,8 @@ function* handleUsbConnectEvent(): Generator {
655660
// if there is exactly one Pybricks device connected, we can connect to
656661
// it, otherwise we should let the user choose which one to connect to
657662
if (pybricksDevices.length === 1) {
663+
// REVISIT: Device is passed as hotplug device, even though it is
664+
// not technically a hotplug event.
658665
yield* spawn(handleUsbConnectPybricks, pybricksDevices[0]);
659666
}
660667
}

0 commit comments

Comments
 (0)