Переглянути джерело

Merge pull request #4165 from Apfelstruhdl/fix/rest-api-before-flow-init

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
SybexXx 1 місяць тому
батько
коміт
aee7fcd3cb

+ 1 - 0
Changelog.md

@@ -1,6 +1,7 @@
 # [Unreleased]
 # [Unreleased]
 
 
 ### Core Changes and Bug fixes
 ### Core Changes and Bug fixes
+- Fix crash / boot loop when /json or /metrics is polled before the flow is initialized [#4165](https://github.com/jomjol/AI-on-the-edge-device/pull/4165)
 - Fix crash when webhook enabled without configured URL [#4136](https://github.com/jomjol/AI-on-the-edge-device/pull/4136)
 - Fix crash when webhook enabled without configured URL [#4136](https://github.com/jomjol/AI-on-the-edge-device/pull/4136)
 - Fix checkDigitConsistency corrupting sub-digits of large readings [#4145](https://github.com/jomjol/AI-on-the-edge-device/pull/4145)
 - Fix checkDigitConsistency corrupting sub-digits of large readings [#4145](https://github.com/jomjol/AI-on-the-edge-device/pull/4145)
 - perf(camera): memcpy/memset instead of per-pixel loops in capture path [#4168](https://github.com/jomjol/AI-on-the-edge-device/pull/4168)
 - perf(camera): memcpy/memset instead of per-pixel loops in capture path [#4168](https://github.com/jomjol/AI-on-the-edge-device/pull/4168)

+ 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;
     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()
 string ClassFlowControll::getNumbersName()
 {
 {
-    return flowpostprocessing->getNumbersName();
+    if (flowpostprocessing) {
+        return flowpostprocessing->getNumbersName();
+    }
+
+    return "";
 }
 }
 
 
 string ClassFlowControll::getJSON()
 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()
 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;
 TaskHandle_t xHandletask_autodoFlow = NULL;
 
 
 bool bTaskAutoFlowCreated = false;
 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;
 bool flowisrunning = false;
 
 
 long auto_interval = 0;
 long auto_interval = 0;
@@ -113,10 +117,12 @@ void DeleteMainFlowTask(void)
 
 
 void doInit(void)
 void doInit(void)
 {
 {
+    bFlowInitialized = false;
 #ifdef DEBUG_DETAIL_ON
 #ifdef DEBUG_DETAIL_ON
     ESP_LOGD(TAG, "Start flowctrl.InitFlow(config);");
     ESP_LOGD(TAG, "Start flowctrl.InitFlow(config);");
 #endif
 #endif
     flowctrl.InitFlow(CONFIG_FILE);
     flowctrl.InitFlow(CONFIG_FILE);
+    bFlowInitialized = true;
 #ifdef DEBUG_DETAIL_ON
 #ifdef DEBUG_DETAIL_ON
     ESP_LOGD(TAG, "Finished flowctrl.InitFlow(config);");
     ESP_LOGD(TAG, "Finished flowctrl.InitFlow(config);");
 #endif
 #endif
@@ -468,7 +474,7 @@ esp_err_t handler_json(httpd_req_t *req)
 
 
     ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri);
     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_hdr(req, "Access-Control-Allow-Origin", "*");
         httpd_resp_set_type(req, "application/json");
         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);
     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_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
         httpd_resp_set_type(req, "text/plain"); // application/openmetrics-text is not yet supported by prometheus so we use text/plain for now