From b44ed4552f0072b2318e4e650289ff12d6b9a101 Mon Sep 17 00:00:00 2001 From: Mike Robbins Date: Mon, 29 Jun 2026 07:07:04 -0400 Subject: [PATCH] Fix spiLock deadlocks / frequent watchdog restarts in WarmNodeStore::save() (#10809) Co-authored-by: Ben Meadors --- src/MessageStore.cpp | 10 ++++++++-- src/mesh/WarmNodeStore.cpp | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/MessageStore.cpp b/src/MessageStore.cpp index 32e78f400..705e37170 100644 --- a/src/MessageStore.cpp +++ b/src/MessageStore.cpp @@ -354,10 +354,16 @@ void MessageStore::clearAllMessages() resetMessagePool(); #ifdef FSCom - concurrency::LockGuard guard(spiLock); SafeFile f(filename.c_str(), false); uint8_t count = 0; - f.write(&count, 1); // write "0 messages" + + // SafeFile already does its own spiLock in its constructor and close(). + // Avoid nesting spiLocks, as this will hang until watchdog reset! + { + concurrency::LockGuard guard(spiLock); + f.write(&count, 1); // write "0 messages" + } + f.close(); #endif diff --git a/src/mesh/WarmNodeStore.cpp b/src/mesh/WarmNodeStore.cpp index 39fbd90c6..2af7c6f0b 100644 --- a/src/mesh/WarmNodeStore.cpp +++ b/src/mesh/WarmNodeStore.cpp @@ -589,12 +589,22 @@ bool WarmNodeStore::save() h.entrySize = sizeof(WarmNodeEntry); h.crc = crc32Buffer(packed.data(), h.count * sizeof(WarmNodeEntry)); - concurrency::LockGuard g(spiLock); - FSCom.mkdir("/prefs"); + // SafeFile already does its own spiLock in its constructor and close(). + // Avoid nesting spiLocks, as this will hang until watchdog reset! + + { + concurrency::LockGuard g(spiLock); + FSCom.mkdir("/prefs"); + } auto f = SafeFile(warmFileName, false); - f.write((const uint8_t *)&h, sizeof(h)); - f.write((const uint8_t *)packed.data(), h.count * sizeof(WarmNodeEntry)); + + { + concurrency::LockGuard g(spiLock); + f.write((const uint8_t *)&h, sizeof(h)); + f.write((const uint8_t *)packed.data(), h.count * sizeof(WarmNodeEntry)); + } + bool ok = f.close(); if (!ok) LOG_ERROR("WarmStore: can't write %s", warmFileName);