Add timeout while waiting for checksum

This adds a timeout while waiting for the checksum from the hub when
downloading and running a program. Without this, we can get stuck in
the "loading" state if the hub doesn't respond as expected. This can
happen, for example, if a program is started with the hub button at the
same time as the start button in Pybricks Code is pressed.

Issue: https://github.com/pybricks/support/issues/301
This commit is contained in:
David Lechner
2021-04-12 13:24:10 -05:00
parent 0d89f8daad
commit 1964c9c485
+28 -2
View File
@@ -4,6 +4,7 @@
import {
SagaGenerator,
actionChannel,
delay,
getContext,
put,
race,
@@ -108,7 +109,19 @@ function* downloadAndRun(_action: HubDownloadAndRunAction): Generator {
return;
}
const checksumAction = yield* take(checksumChannel);
const { checksumAction, checksumTimeout } = yield* race({
checksumAction: take(checksumChannel),
checksumTimeout: delay(1000),
});
if (checksumTimeout) {
console.error(`timeout waiting for checksum`);
yield* put(didFailToFinishDownload());
return;
}
defined(checksumAction);
if (checksumAction.checksum !== (0xff ^ xor8(sizeBuf))) {
console.error(
`bad checksum ${checksumAction.checksum} vs ${0xff ^ xor8(sizeBuf)}`,
@@ -136,7 +149,20 @@ function* downloadAndRun(_action: HubDownloadAndRunAction): Generator {
}
// TODO: dispatch progress
}
const checksumAction = yield* take(checksumChannel);
const { checksumAction, checksumTimeout } = yield* race({
checksumAction: take(checksumChannel),
checksumTimeout: delay(1000),
});
if (checksumTimeout) {
console.error(`timeout waiting for checksum`);
yield* put(didFailToFinishDownload());
return;
}
defined(checksumAction);
if (checksumAction.checksum !== (0xff ^ xor8(chunk))) {
console.error(
`bad checksum ${checksumAction.checksum} vs ${0xff ^ xor8(chunk)}`,