From 7a0f093ab1d5aed3669b3ed328650fa9cf2b4a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20M=C3=B6hle?= <91616163+softhack007@users.noreply.github.com> Date: Sat, 21 Mar 2026 23:46:27 +0100 Subject: [PATCH] small robustness improvement for closeFile() Refactor file closing logic to improve concurrency handling: "Consuming" doCloseFile early reduces the risk that several tasks will try to close the same file. --- wled00/file.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wled00/file.cpp b/wled00/file.cpp index 8cafb70c..56af35cc 100644 --- a/wled00/file.cpp +++ b/wled00/file.cpp @@ -47,7 +47,8 @@ void closeFile() { // --> file reads rarely cause refill stalls compared to writes, but large/fragmented reads can still exceed the ~0.08–0.12 ms budget. // esp32 recommendations: use f.setBufferSize() (512–1024 for reads is reasonable); use delay(0) after file reads, to reduce task contention - if (!f) {doCloseFile = false; return;} // WLEDMM only do all this hick-hack when f is an open file + if (!doCloseFile || !f) { doCloseFile = false; return; } // file not open, or no request to close -> nothing to do, nothing to wait + doCloseFile = false; // consume flag early, to reduce the time window for concurrent closing attempts from several tasks. bool oldLock = suspendStripService; #if defined(WLEDMM_FILEWAIT) || !defined(ARDUINO_ARCH_ESP32) // only wait if we don't have the flicker-free RMTHI driver @@ -80,7 +81,6 @@ void closeFile() { #endif suspendStripService = oldLock; // restore previous lock DEBUGFS_PRINTF("took %d ms\n", millis() - s); - doCloseFile = false; } //find() that reads and buffers data from file stream in 256-byte blocks.