Skip to content

Commit c641f3f

Browse files
Fix infinite save retry loop and method check bugs
- Add retry limit (3 attempts) to saveFileContents() to prevent infinite retry loop when file writes fail. Shows error message after retries exhausted instead of silently retrying forever. This fixes the 'reloading every 1-2 seconds' behavior reported in #460. - Clear save retry timeout on disconnect to prevent retries from continuing after switching boards or disconnecting. - Fix _isMethodAllowed() missing parentheses on toUpperCase call (was passing function reference instead of calling it). - Add missing await on _isMethodAllowed() call in _fetch() so the method permission check actually evaluates the result. - Fix missing closing quote in save failure message template literal. Fixes #460
1 parent 6be9157 commit c641f3f

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

js/common/web-file-transfer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class FileTransferClient {
8989
};
9090

9191
if (fetchOptions.method && fetchOptions.method.toUpperCase() != 'OPTIONS') {
92-
if (!this._isMethodAllowed(fetchOptions.method)) {
92+
if (!await this._isMethodAllowed(fetchOptions.method)) {
9393
if (fetchOptions.method.toUpperCase() == "MOVE") {
9494
// This should only happen if rename is used and the user doesn't have latest version
9595
console.warn("Please upgrade to the latest version of CircuitPython. Allowing MOVE for now.");
@@ -114,7 +114,7 @@ class FileTransferClient {
114114

115115
async _isMethodAllowed(method) {
116116
if (this._allowedMethods) {
117-
return this._allowedMethods.includes(method.toUpperCase);
117+
return this._allowedMethods.includes(method.toUpperCase());
118118
}
119119

120120
return false;

js/script.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ async function loadEditor() {
462462

463463
var editor;
464464
var currentTimeout = null;
465+
var saveRetryCount = 0;
466+
const MAX_SAVE_RETRIES = 3;
465467

466468
// Save the File Contents and update the UI
467469
async function saveFileContents(path) {
@@ -482,16 +484,24 @@ async function saveFileContents(path) {
482484
if (await workflow.writeFile(path, contents, offset)) {
483485
setFilename(workflow.currentFilename);
484486
setSaved(true);
487+
saveRetryCount = 0;
485488
} else {
486-
await showMessage(`Saving file '${workflow.currentFilename} failed.`);
489+
await showMessage(`Saving file '${workflow.currentFilename}' failed.`);
487490
}
488491
} catch (e) {
489492
console.error("write failed", e, e.stack);
490493
unchanged = Math.min(oldUnchanged, unchanged);
491494
if (currentTimeout != null) {
492495
clearTimeout(currentTimeout);
493496
}
494-
currentTimeout = setTimeout(saveFileContents, 2000);
497+
saveRetryCount++;
498+
if (saveRetryCount < MAX_SAVE_RETRIES) {
499+
console.log(`Save retry ${saveRetryCount} of ${MAX_SAVE_RETRIES}...`);
500+
currentTimeout = setTimeout(saveFileContents, 2000);
501+
} else {
502+
saveRetryCount = 0;
503+
await showMessage(`Saving file '${workflow.currentFilename}' failed after multiple attempts. Check your connection and try again.`);
504+
}
495505
}
496506
}
497507

@@ -535,6 +545,11 @@ async function onTextChange(update) {
535545
}
536546

537547
function disconnectCallback() {
548+
if (currentTimeout != null) {
549+
clearTimeout(currentTimeout);
550+
currentTimeout = null;
551+
}
552+
saveRetryCount = 0;
538553
updateUIConnected(false);
539554
}
540555

0 commit comments

Comments
 (0)