Skip to content

Commit 18d961b

Browse files
committed
usb/sagas: Delay connection attempt on hotplug.
If we try to connect too soon, we get security errors.
1 parent 662c85b commit 18d961b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/pybricks-blocks

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 544b33a507a3a3c368df7b8057b7c63d3ff6caa0

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)