From e4b8aacc0a98e189c10732946e95b3b41914a75a Mon Sep 17 00:00:00 2001 From: Sleeper85 Date: Fri, 21 Aug 2026 17:50:30 +0200 Subject: [PATCH] Fixed 6h reboot on networks that filter the ESP32 outbound traffic http_request runs on the loop task, which has a 4s budget before the 5s task WDT panics. A dropped connect() blocks for 4.5s (the ESPHome default timeout) and a filtered port 53 blocks for a fixed 7s, which timeout does not cover. - timeout 3s and watchdog_timeout 60s cover the two blocking paths - a failure counter stops the service after 3 network-level failures, instead of freezing the loop 14s every 6h forever - the POST is now skipped when the GET failed Reproduced and measured on OPNsense. See Design_decisions.md. --- documents/README/dev/Design_decisions.md | 46 ++++++++++++++++++ packages/yambms/yambms_core.yaml | 4 ++ packages/yambms/yambms_service.yaml | 62 ++++++++++++++++-------- yambms_custom.yaml | 8 ++- 4 files changed, 99 insertions(+), 21 deletions(-) diff --git a/documents/README/dev/Design_decisions.md b/documents/README/dev/Design_decisions.md index dd758fb..41d5b4f 100644 --- a/documents/README/dev/Design_decisions.md +++ b/documents/README/dev/Design_decisions.md @@ -113,6 +113,52 @@ interval to make protocols "consistent" with each other. When adding a frame to a sequence, remember that the sequence length is tracked alongside the array. Keep the two in sync, or the new frame will silently never be emitted. +## The update service must never block the loop task + +**File:** `packages/yambms/yambms_service.yaml` + +`http_request` runs synchronously on the ESP32 loop task. ESPHome arms the IDF task watchdog at +5s with `CONFIG_ESP_TASK_WDT_PANIC=y`, and feeds it at most every 1 s, so any single blocking +call has a **4s budget** before the device panics and reboots. + +Two paths exceed that budget on a network that filters outbound traffic. Both were reproduced on +an OPNsense firewall and measured on hardware: + +| Path | Blocking time | Bounded by | +|---|---|---| +| Port 80 dropped silently | 4.5s | `timeout`, whose ESPHome default is 4.5s | +| Port 53 filtered (drop **or** reject) | 7.0 s, fixed | nothing — `timeout` does not cover DNS | + +The 7s figure is the lwIP resolver retry ladder (`1 + 1 + 2 + 3`s over `DNS_MAX_RETRIES`), and +it is deterministic to a few milliseconds. A reject is not faster than a drop: lwIP's UDP API has +no error callback, so the ICMP unreachable is counted and discarded, and the query still runs its +full budget. Only an actual DNS *answer* — including `NXDOMAIN` from a local resolver — fails fast. + +This is why three separate settings are needed, and none of them is redundant: + +- **`watchdog_timeout: 60s`** is the only thing that covers the DNS wait. It is never reached in + normal operation, where a full request takes under 400 ms. +- **`timeout: 3s`** brings the socket phase back under the 4s budget. It does **not** help the + DNS case. +- **The failure counter** (`yambms${yambms_id}_var_service_failures`) is what stops the loop from + freezing for 7s every 6h, forever, on a device that will never reach the internet. + +`my_network.is_connected()` is not a reachability test — it only proves the device holds an IP. +It is kept as a cheap first filter, but the counter is the real guard. Do not remove it on the +grounds that the connectivity check above it "already handles this". + +The counter is incremented from `on_error`, which fires only when no connection could be made. +An HTTP error status still fires `on_response` and resets the counter, which is correct: the +network worked, so there is no freeze to protect against. + +The POST is guarded by an `if` on the counter rather than nested inside the GET's `on_response`. +Nesting would stack one blocking request inside another on a loop task whose stack is 8192 B by +default — see [LOOP_TASK_STACK.md](../LOOP_TASK_STACK.md). It also avoids a second 7s freeze on +a filtered network, halving the worst case. + +Reaching 3 failures disables the service until reboot, or until the user toggles the +`Update service` switch off and on, which rearms the counter. + ## Empirical constants Some constants were found by trial on real hardware and must not be rounded or harmonised. diff --git a/packages/yambms/yambms_core.yaml b/packages/yambms/yambms_core.yaml index 7930423..1f32db2 100644 --- a/packages/yambms/yambms_core.yaml +++ b/packages/yambms/yambms_core.yaml @@ -240,6 +240,10 @@ globals: type: bool restore_value: no initial_value: 'true' + - id: yambms${yambms_id}_var_service_failures + type: int + restore_value: no + initial_value: '0' - id: yambms${yambms_id}_var_initialized type: bool restore_value: no diff --git a/packages/yambms/yambms_service.yaml b/packages/yambms/yambms_service.yaml index 233a6b4..d6e012d 100644 --- a/packages/yambms/yambms_service.yaml +++ b/packages/yambms/yambms_service.yaml @@ -1,5 +1,5 @@ -# Updated : 2026.08.14 -# Version : 1.2.9 +# Updated : 2026.08.21 +# Version : 1.3.0 # GitHub : https://github.com/Sleeper85/esphome-yambms # YamBMS ( Yet another multi-BMS Merging Solution ) @@ -46,6 +46,14 @@ text_sensor: # https://esphome.io/components/http_request.html http_request: verify_ssl: false + # Must stay under the ESP32 task WDT budget: a 5s WDT minus the 1s feed + # interval leaves 4s. The ESPHome default of 4.5s exceeds it and panics the + # loop task whenever a connect() is silently dropped by a firewall. + timeout: 3s + # DNS resolution runs before the socket and is NOT covered by `timeout`. A + # filtered port 53 blocks the loop task for a fixed 7s, which panics the WDT. + # See Design_decisions.md. + watchdog_timeout: 60s interval: - interval: 6h # 60min @@ -54,33 +62,49 @@ interval: condition: and: - lambda : return id(yambms${yambms_id}_var_service) == true; - - lambda : return id(my_network).is_connected(); + - lambda : return id(my_network).is_connected(); # only proves the device has an IP, not that the internet is reachable. + - lambda : return id(yambms${yambms_id}_var_service_failures) < 3; then: - http_request.get: url: http://script.opentel.be/yambms.get.php capture_response: true + # on_error fires only when no connection could be made (DNS or + # connect failure) — the cases that freeze the loop. An HTTP error + # status still fires on_response, which rightly resets the counter. + on_error: + then: + - lambda: id(yambms${yambms_id}_var_service_failures) += 1; on_response: then: - lambda: |- + id(yambms${yambms_id}_var_service_failures) = 0; json::parse_json(body, [](JsonObject root) -> bool { // Explicitly cast to std::string to resolve ambiguity id(yambms${yambms_id}_last_version).publish_state(root["version"].as()); return true; }); - - http_request.post: - url: http://script.opentel.be/yambms.post.php - request_headers: - Content-Type: application/json - json: |- - root["mac"] = id(esp32_mac_address).state; - root["version"] = "${yambms_version}"; - root["board_chip"] = "${board_chip}"; - root["board_name"] = "${board_name}"; - root["bms_model"] = id(yambms${yambms_id}_var_bms_models); - root["bms_protocol"] = id(yambms${yambms_id}_var_bms_protocols); - root["bms_count"] = id(yambms${yambms_id}_var_bms_counter); - root["comm_bus"] = id(yambms${yambms_id}_var_communication_bus); - root["comm_protocol"] = id(yambms${yambms_id}_var_communication_protocol); - root["uptime"] = id(esp32_uptime).state; - root["date_time_now"] = id(esp32_date_time_now).state; + # Guarded by an `if` rather than nested in on_response: nesting would + # stack a blocking request inside another one on the loop task, whose + # stack is 8192 B by default (see LOOP_TASK_STACK.md). On a filtered + # network it also avoids a second 7 s freeze for nothing. + - if: + condition: + lambda: return id(yambms${yambms_id}_var_service_failures) == 0; + then: + - http_request.post: + url: http://script.opentel.be/yambms.post.php + request_headers: + Content-Type: application/json + json: |- + root["mac"] = id(esp32_mac_address).state; + root["version"] = "${yambms_version}"; + root["board_chip"] = "${board_chip}"; + root["board_name"] = "${board_name}"; + root["bms_model"] = id(yambms${yambms_id}_var_bms_models); + root["bms_protocol"] = id(yambms${yambms_id}_var_bms_protocols); + root["bms_count"] = id(yambms${yambms_id}_var_bms_counter); + root["comm_bus"] = id(yambms${yambms_id}_var_communication_bus); + root["comm_protocol"] = id(yambms${yambms_id}_var_communication_protocol); + root["uptime"] = id(esp32_uptime).state; + root["date_time_now"] = id(esp32_date_time_now).state; diff --git a/yambms_custom.yaml b/yambms_custom.yaml index 454a56e..1004f5d 100644 --- a/yambms_custom.yaml +++ b/yambms_custom.yaml @@ -1,5 +1,5 @@ -# Updated : 2026.06.27 -# Version : 1.1.6 +# Updated : 2026.08.21 +# Version : 1.1.7 # GitHub : https://github.com/Sleeper85/esphome-yambms # YamBMS ( Yet another multi-BMS Merging Solution ) @@ -49,6 +49,10 @@ switch: - globals.set: id: yambms${yambms_id}_var_service value: 'true' + # Rearm the circuit breaker, so the user can retry after fixing the network + - globals.set: + id: yambms${yambms_id}_var_service_failures + value: '0' turn_off_action: - globals.set: id: yambms${yambms_id}_var_service