requestJSONBufferLock() variable timeout

* default timeout = 1800ms
* reduced timeout for ws (300ms)
This commit is contained in:
Frank
2025-12-30 01:57:46 +01:00
parent 5739a54287
commit 80b906999b
3 changed files with 6 additions and 6 deletions

View File

@@ -439,7 +439,7 @@ void sappend(char stype, const char* key, int val);
void sappends(char stype, const char* key, char* val);
void prepareHostname(char* hostname);
bool isAsterisksOnly(const char* str, byte maxLen) __attribute__((pure));
bool requestJSONBufferLock(uint8_t module=255);
bool requestJSONBufferLock(uint8_t module=255, unsigned timeoutMS = 1800);
void releaseJSONBufferLock();
uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLen);
uint8_t extractModeSlider(uint8_t mode, uint8_t slider, char *dest, uint8_t maxLen, uint8_t *var = nullptr);

View File

@@ -220,19 +220,19 @@ bool isAsterisksOnly(const char* str, byte maxLen)
//threading/network callback details: https://github.com/Aircoookie/WLED/pull/2336#discussion_r762276994
bool requestJSONBufferLock(uint8_t module)
bool requestJSONBufferLock(uint8_t module, unsigned timeoutMS)
{
bool haveLock = false;
#ifdef ARDUINO_ARCH_ESP32
// We use a recursive mutex to prevent parallel JSON writes from parallel tasks.
// This also fixes hanging up for the full timeout interval in cases when the contention is from the same task.
// see https://github.com/wled/WLED/pull/4089 for more details.
if (esp32SemTake(jsonBufferLockMutex, 1800) == pdTRUE) haveLock = true; // WLEDMM must wait longer than suspendStripService timeout = 1500ms
if (esp32SemTake(jsonBufferLockMutex, timeoutMS) == pdTRUE) haveLock = true; // WLEDMM must wait longer than suspendStripService timeout = 1500ms
#else
// 8266: only wait in case that can_yield() tells us we can yield and delay
if (can_yield()) {
unsigned long now = millis();
while (jsonBufferLock && millis()-now < 1800) delay(1); // wait for fraction for buffer lock // WLEDMM must wait longer than suspendStripService timeout = 1500ms
while (jsonBufferLock && millis()-now < timeoutMS) delay(1); // wait for fraction for buffer lock // WLEDMM must wait longer than suspendStripService timeout = 1500ms
if (!jsonBufferLock) haveLock = true;
}
#endif

View File

@@ -49,7 +49,7 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
}
bool verboseResponse = false;
if (!requestJSONBufferLock(11)) {
if (!requestJSONBufferLock(11, 300)) {
client->text(F("{\"error\":3}")); // ERR_NOBUF
return;
}
@@ -138,7 +138,7 @@ void sendDataWs(AsyncWebSocketClient * client)
DEBUG_PRINTF("sendDataWs\n");
if (!ws.count()) return;
if (!requestJSONBufferLock(12)) {
if (!requestJSONBufferLock(12, 300)) {
if (client) {
client->text(F("{\"error\":3}")); // ERR_NOBUF
} else {