diff --git a/docs/features/feature-catalog.md b/docs/features/feature-catalog.md index 82b6e97..5a4ccd4 100644 --- a/docs/features/feature-catalog.md +++ b/docs/features/feature-catalog.md @@ -67,6 +67,7 @@ Every feature — user-facing or significant internal mechanism — is listed he | F-055 | Fix: heap-exhaustion abort() under rapid MQTT command bursts — free-heap guard on publish + command intake | v2.1.4 | Stable | | F-056 | Diagnostics report — CPU temperature, WiFi reconnects, OTA state, stack high-water mark, published automatically every 5 min + on-demand via `telemetry.get_diagnostics` | v2.1.5 | Stable | | F-057 | QA-to-shipping stats reset — `telemetry.reset_boot_data` (lifetime boot count + SD boot log) and `logs.clear` (SD debug logs) | v2.1.6 | Stable | +| F-058 | Fix: NTP sync RTC corruption on flaky internet — de-dup guard, retry cooldown, large-jump consensus check | v2.1.7 | Stable | --- @@ -933,3 +934,26 @@ Response: `{ "status": "SUCCESS", "type": "logs.clear", "data": { "deleted": 4 } Verified: `pio run` on all four environments. Subsystem: `src/Telemetry/Telemetry.hpp`, `src/Telemetry/Telemetry.cpp`, `src/FileManager/FileManager.hpp`, `src/FileManager/FileManager.cpp`, `src/Communication/Handlers/TelemetryHandler/TelemetryHandler.hpp`, `src/Communication/Handlers/TelemetryHandler/TelemetryHandler.cpp`, `src/Communication/Handlers/LoggingHandler/LoggingHandler.hpp`, `src/Communication/Handlers/LoggingHandler/LoggingHandler.cpp` + +--- + +### F-058 — Fix: NTP sync RTC corruption on flaky internet + +Field report: units on unstable internet had RTC time drift anywhere from ~10 minutes to half a day, while units on stable internet stayed accurate indefinitely. Root cause found by auditing `Timekeeper::syncTimeWithNTP()` (F-019) against the same function in the pre-restructure firmware (`Controller - Production FW/vesper`) — the bug was present identically in both, confirmed byte-for-byte before this fix. + +**Root cause.** `syncTimeWithNTP()` wrote whatever `getLocalTime()` returned straight to the RTC via `rtc.adjust()`, with no plausibility check. NTP sync is not on a fixed nightly schedule — `ntpSyncTimerCallback` in [main.cpp](../../src/main.cpp) fires on **every WiFi reconnect** (`onNetworkConnected` callback, 3s stabilisation delay), so an unstable link produces far more sync attempts than a healthy one. The underlying Arduino `configTime()`/`getLocalTime()` API has no request/response correlation — `getLocalTime()` accepts a reply based only on "does the system clock look set now," not "is this the reply to the request I just sent." If WiFi reconnected faster than a single attempt's ~5s resolve window, a stale reply from an earlier abandoned `configTime()` call could be read back during a later attempt's window and written straight to the RTC. This matches the reported symptom — random-magnitude jumps, not a fixed offset, and exposure scaling with connection flakiness rather than occurring on a schedule. + +**Fix — three layers, all in [TimeKeeper.cpp](../../src/TimeKeeper/TimeKeeper.cpp):** + +1. **In-flight de-dup guard.** `ntpSyncInProgress` (`std::atomic`) rejects a new sync attempt while one is already running. Since the SNTP client itself can't distinguish attempts, the fix is to never have more than one in flight — nothing left for a stale reply to collide with. +2. **Retry cooldown.** `lastNtpAttemptMs` enforces a minimum 10s gap (`NTP_MIN_RETRY_MS`) between attempts, independent of the de-dup guard, throttling reconnect storms on a flapping link. +3. **Plausibility + consensus gate.** New `commitNtpResult(time_t candidateLocalEpoch)` compares the NTP candidate against the current RTC value before writing anything: + - Drift ≤ `NTP_JUMP_THRESHOLD_SEC` (2 min) — committed immediately as normal drift correction. + - Drift > threshold — held as a pending candidate, **not written**. Only committed once `NTP_AGREE_REQUIRED` (3) consecutive sync attempts agree with each other within `NTP_AGREE_TOLERANCE_SEC` (5s). A disagreeing candidate restarts the count. A single bad/stale reply can no longer move the clock by hours — it would have to reproduce identically across three independent syncs. + - RTC not running (dead battery / fresh board) — first NTP result trusted unconditionally, since there is nothing meaningful to compare against. + +All four thresholds are `static constexpr` at the top of `TimeKeeper.cpp`. Consensus state (`pendingNtpEpoch`/`pendingNtpAgreeCount`) is in-RAM only and does not persist across reboot — a device that restarts mid-consensus starts the count over, which is intentional. + +Full rationale documented in [TimeKeeper/README.md](../../src/TimeKeeper/README.md#ntp-sync-safety-bugfix). + +Subsystem: `src/TimeKeeper/TimeKeeper.hpp`, `src/TimeKeeper/TimeKeeper.cpp`