From e22088f0991348625bb9cd7293040267c28365f9 Mon Sep 17 00:00:00 2001 From: philmoz Date: Thu, 20 Feb 2025 22:33:10 +1100 Subject: [PATCH] fix(radio): limit number of consecutive retries when writing SD card data (#5929) --- radio/src/storage/sdcard_common.cpp | 73 ++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/radio/src/storage/sdcard_common.cpp b/radio/src/storage/sdcard_common.cpp index 99b344c03..db049ceba 100644 --- a/radio/src/storage/sdcard_common.cpp +++ b/radio/src/storage/sdcard_common.cpp @@ -80,41 +80,70 @@ void storageCheck(bool immediately) // Don't write anything to SD card if in EM if (UNEXPECTED_SHUTDOWN()) return; + static constexpr uint8_t retryLimit = 10; + + static uint8_t retryRadioCount = 0; if (storageDirtyMsk & EE_GENERAL) { - TRACE("eeprom write general"); - const char * error = writeGeneralSettings(); - if (error) { - TRACE("writeGeneralSettings error=%s", error); - } - else { - storageDirtyMsk &= ~EE_GENERAL; + if (retryRadioCount < retryLimit) { + TRACE("SD card write radio settings"); + const char * error = writeGeneralSettings(); + if (error) { + TRACE("writeGeneralSettings error=%s", error); + retryRadioCount += 1; + } else { + storageDirtyMsk &= ~EE_GENERAL; + retryRadioCount = 0; + } + } else { + // Reset timeout to next check + storageDirtyTime10ms = get_tmr10ms(); + retryRadioCount = retryLimit / 2; // Retry again after timeout; but fewer times + // TODO: provide some mechanism to alert user that SD card has serious error } } #if defined(STORAGE_MODELSLIST) + static uint8_t retryLabelsCount = 0; if (storageDirtyMsk & EE_LABELS) { - TRACE("SD card write labels"); - const char * error = modelslist.save(); - if (error) { - TRACE("writeLabels error=%s", error); - } - else { - storageDirtyMsk &= ~EE_LABELS; + if (retryLabelsCount < retryLimit) { + TRACE("SD card write labels"); + const char * error = modelslist.save(); + if (error) { + TRACE("writeLabels error=%s", error); + retryLabelsCount += 1; + } else { + storageDirtyMsk &= ~EE_LABELS; + retryLabelsCount = 0; + } + } else { + // Reset timeout to next check + storageDirtyTime10ms = get_tmr10ms(); + retryLabelsCount = retryLimit / 2; // Retry again after timeout; but fewer times + // TODO: provide some mechanism to alert user that SD card has serious error } } #endif + static uint8_t retryModelCount = 0; if (storageDirtyMsk & EE_MODEL) { - TRACE("eeprom write model"); - const char * error = writeModel(); + if (retryModelCount < retryLimit) { + TRACE("SD card write model settings"); + const char * error = writeModel(); #if defined(STORAGE_MODELSLIST) - modelslist.updateCurrentModelCell(); + modelslist.updateCurrentModelCell(); #endif - if (error) { - TRACE("writeModel error=%s", error); - } - else { - storageDirtyMsk &= ~EE_MODEL; + if (error) { + TRACE("writeModel error=%s", error); + retryModelCount += 1; + } else { + storageDirtyMsk &= ~EE_MODEL; + retryModelCount = 0; + } + } else { + // Reset timeout to next check + storageDirtyTime10ms = get_tmr10ms(); + retryModelCount = retryLimit / 2; // Retry again after timeout; but fewer times + // TODO: provide some mechanism to alert user that SD card has serious error } } }