From f968179b60d51a50a3106e3249dbb041d51ee6d1 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Thu, 4 Jan 2024 14:31:09 +0100 Subject: [PATCH] Better handling of out of memory situations in live data websocket --- src/WebApi_ws_live.cpp | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/WebApi_ws_live.cpp b/src/WebApi_ws_live.cpp index f3a427292..c523ce3e7 100644 --- a/src/WebApi_ws_live.cpp +++ b/src/WebApi_ws_live.cpp @@ -6,6 +6,7 @@ #include "Configuration.h" #include "Datastore.h" #include "MessageOutput.h" +#include "Utils.h" #include "WebApi.h" #include "Battery.h" #include "Huawei_can.h" @@ -67,20 +68,12 @@ void WebApiWsLiveClass::loop() try { std::lock_guard lock(_mutex); - DynamicJsonDocument root(4200 * INV_MAX_COUNT); // TODO(helge) check if this calculation is correct + DynamicJsonDocument root(4096 * INV_MAX_COUNT); + if (Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) { + JsonVariant var = root; + generateJsonResponse(var); - // TODO(helge) temporary dump of memory usage if allocation of DynamicJsonDocument fails (will be fixed in upstream repo) - if (root.capacity() == 0) { - MessageOutput.printf("Calling /api/livedata/status has temporarily run out of resources. Reason: Alloc memory for DynamicJsonDocument failed (FreeHeap = %d, MaxAllocHeap = %d, MinFreeHeap = %d).\r\n", ESP.getFreeHeap(), ESP.getMaxAllocHeap(), ESP.getMinFreeHeap()); - _lastWsPublish = millis(); - return; - } - - JsonVariant var = root; - generateJsonResponse(var); - - String buffer; - if (buffer) { + String buffer; serializeJson(root, buffer); if (Configuration.get().Security.AllowReadonly) {