瀏覽代碼

Do not serve /json or /metrics before the flow is initialized

bTaskAutoFlowCreated is set at the very start of task_autodoFlow, before
doInit() has built the flow, so handler_json and handler_openmetrics passed
their guard while ClassFlowControll::flowpostprocessing was still NULL and
then dereferenced it via getJSON() / getNumbers(). The window is a few
seconds on a normal boot, the whole of /doinit's rebuild (InitFlow() resets
the pointer to NULL first), and five minutes after any panic because the
flow init is deliberately delayed then. A client polling /json during that
delay (Home Assistant REST sensor, Prometheus scraper, scripts) crashes the
device again on every boot, turning one panic into a permanent boot loop.

Track flow readiness explicitly (bFlowInitialized, cleared and set around
InitFlow() in doInit()) and gate both handlers on it; until then they return
the existing "not yet available" 403. Also null-check flowpostprocessing in
getJSON(), getNumbers() and getNumbersName(), matching the neighbouring
accessors, so no other caller can hit the same pointer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
David Schug 1 月之前
父節點
當前提交
2368bfa16e

+ 6 - 0
Changelog.md

@@ -1,3 +1,9 @@
+# [Unreleased]
+
+### Core Changes and Bug fixes
+- Fixed a crash (and, after a crash, a boot loop) when the REST API `/json` or `/metrics` is polled before the flow is initialized: both handlers read the post-processing results while they did not exist yet (a NULL pointer) during startup, during the 5-minute init delay that follows any crash, and while `/doinit` rebuilds the flow. They now answer "not yet available" until the flow is initialized, and the underlying accessors no longer dereference a missing post-processing step
+
+
 # [16.1.0] - 2026-01-11
 
 For a full list of changes see [Full list of changes](https://github.com/jomjol/AI-on-the-edge-device/compare/v16.0.0...v16.1.0)

+ 21 - 5
code/components/jomjol_flowcontroll/ClassFlowControll.cpp

@@ -925,20 +925,36 @@ esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req)
     return result;
 }
 
+// flowpostprocessing is NULL until InitFlow() has parsed the config, and InitFlow() resets it to NULL
+// whenever the flow is rebuilt; REST handlers can call these at any time.
 string ClassFlowControll::getNumbersName()
 {
-    return flowpostprocessing->getNumbersName();
+    if (flowpostprocessing) {
+        return flowpostprocessing->getNumbersName();
+    }
+
+    return "";
 }
 
 string ClassFlowControll::getJSON()
 {
-    return flowpostprocessing->GetJSON();
+    if (flowpostprocessing) {
+        return flowpostprocessing->GetJSON();
+    }
+
+    return "";
 }
 
-/** 
- * @returns a vector of all current sequences
+/**
+ * @returns a vector of all current sequences (empty while the flow is not initialized)
  **/
 const std::vector<NumberPost*> &ClassFlowControll::getNumbers()
 {
-    return *flowpostprocessing->GetNumbers();
+    static const std::vector<NumberPost*> noNumbers;
+
+    if (flowpostprocessing) {
+        return *flowpostprocessing->GetNumbers();
+    }
+
+    return noNumbers;
 }

+ 8 - 2
code/components/jomjol_flowcontroll/MainFlowControl.cpp

@@ -40,6 +40,10 @@ camera_flow_config_temp_t CFstatus;
 TaskHandle_t xHandletask_autodoFlow = NULL;
 
 bool bTaskAutoFlowCreated = false;
+// True only while the flow objects are fully built. bTaskAutoFlowCreated is already true during the
+// post-panic init delay and while doInit() rebuilds the flow, when flowctrl's post-processing is NULL
+// or half-built, so REST handlers that read the results must check this flag instead.
+bool bFlowInitialized = false;
 bool flowisrunning = false;
 
 long auto_interval = 0;
@@ -113,10 +117,12 @@ void DeleteMainFlowTask(void)
 
 void doInit(void)
 {
+    bFlowInitialized = false;
 #ifdef DEBUG_DETAIL_ON
     ESP_LOGD(TAG, "Start flowctrl.InitFlow(config);");
 #endif
     flowctrl.InitFlow(CONFIG_FILE);
+    bFlowInitialized = true;
 #ifdef DEBUG_DETAIL_ON
     ESP_LOGD(TAG, "Finished flowctrl.InitFlow(config);");
 #endif
@@ -468,7 +474,7 @@ esp_err_t handler_json(httpd_req_t *req)
 
     ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri);
 
-    if (bTaskAutoFlowCreated)
+    if (bTaskAutoFlowCreated && bFlowInitialized)
     {
         httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
         httpd_resp_set_type(req, "application/json");
@@ -520,7 +526,7 @@ esp_err_t handler_openmetrics(httpd_req_t *req)
 
     ESP_LOGD(TAG, "handler_openmetrics uri: %s", req->uri);
 
-    if (bTaskAutoFlowCreated)
+    if (bTaskAutoFlowCreated && bFlowInitialized)
     {
         httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
         httpd_resp_set_type(req, "text/plain"); // application/openmetrics-text is not yet supported by prometheus so we use text/plain for now