From 8c726549bfa7dd4d36cc2e6d1b41ba1fe6b19dbd Mon Sep 17 00:00:00 2001 From: Eunchurn Park Date: Thu, 25 Dec 2025 19:43:56 +0900 Subject: [PATCH] fix(epub): add retry logic for SD card streaming Add 3 retry attempts with 50ms delay for ZIP to SD card streaming to handle timing issues after display refresh. --- lib/Epub/Epub/Section.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index 5323a7a..76c39d8 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -118,15 +118,25 @@ bool Section::persistPageDataToSD(const int fontId, const float lineCompression, const bool extraParagraphSpacing) { const auto localPath = epub->getSpineItem(spineIndex).href; const auto tmpHtmlPath = epub->getCachePath() + "/.tmp_" + std::to_string(spineIndex) + ".html"; - File tmpHtml; - if (!FsHelpers::openFileForWrite("SCT", tmpHtmlPath, tmpHtml)) { - return false; + + // Retry logic for SD card timing issues + bool success = false; + for (int attempt = 0; attempt < 3 && !success; attempt++) { + if (attempt > 0) { + Serial.printf("[%lu] [SCT] Retrying stream (attempt %d)...\n", millis(), attempt + 1); + delay(50); // Brief delay before retry + } + + File tmpHtml; + if (!FsHelpers::openFileForWrite("SCT", tmpHtmlPath, tmpHtml)) { + continue; + } + success = epub->readItemContentsToStream(localPath, tmpHtml, 1024); + tmpHtml.close(); } - bool success = epub->readItemContentsToStream(localPath, tmpHtml, 1024); - tmpHtml.close(); if (!success) { - Serial.printf("[%lu] [SCT] Failed to stream item contents to temp file\n", millis()); + Serial.printf("[%lu] [SCT] Failed to stream item contents to temp file after retries\n", millis()); return false; }