fix(dispatcher): pick radio-watchdog activity by age, not largest timestamp

The watchdog took max(last_recv, last_irq, last_tx) of three raw millis()
timestamps. Around the 32-bit wrap a transmit stamped in the upper half of the
range is numerically larger than every fresh post-wrap receive, so it kept
winning for up to ~24 days: the watchdog measured silence from that stale
transmit, concluded the radio had gone deaf, and reset a radio that was
receiving normally — once per watchdog interval, until the old value stopped
winning.

Extract the decision into RadioWatchdog (pure, host-testable). It compares each
timestamp only against its own previous value, so any change is fresh activity,
and stamps it against a monotonic 64-bit clock extended from millis(). Evidence
older than one 32-bit cycle now stays old instead of aliasing back to "recent",
which the previous unsigned-subtraction age could not do either.

Covered in test/test_radio_watchdog: pre-wrap TX with continuous post-wrap RX,
genuine silence across the wrap, silence past a full millis() cycle, no activity
since boot, disabled watchdog, TX-only traffic, and a stalled mesh loop.
This commit is contained in:
agessaman committed 2026-09-09 14:42:39 -07:00
1 parent a98781cccb
commit 6c4ec6869c
4 files changed
+343 -19

No files matched your search

+17 -17
View File
@@ -29,6 +29,9 @@ void Dispatcher::begin() {
_radio->begin();
prev_isrecv_mode = _radio->isInRecvMode();
#ifdef WITH_MQTT_BRIDGE
radio_watchdog.reset((uint32_t)_ms->getMillis());
#endif
}
float Dispatcher::getAirtimeBudgetFactor() const {
@@ -94,23 +97,20 @@ void Dispatcher::loop() {
// MQTTPrefs radio_watchdog_minutes setting.
#ifdef WITH_MQTT_BRIDGE
{
const uint32_t watchdog_ms = getRadioWatchdogMillis();
if (watchdog_ms > 0) {
unsigned long last_recv = _radio->getLastRecvMillis();
unsigned long last_irq = _radio->getLastRadioInterruptMillis();
unsigned long last_active = (last_recv > last_irq ? last_recv : last_irq);
if (last_radio_active_ms > last_active) last_active = last_radio_active_ms;
if (is_recv && last_active > 0) {
unsigned long silent_ms = _ms->getMillis() - last_active;
unsigned long since_recovery = _ms->getMillis() - last_watchdog_recovery;
if (silent_ms > watchdog_ms && since_recovery > watchdog_ms) {
_err_flags |= ERR_EVENT_RADIO_WATCHDOG;
MESH_DEBUG_PRINTLN("Radio watchdog: silent %lu ms, state=%d, recovering", silent_ms, _radio->getRadioState());
_radio->idle();
_radio->startRecv();
last_watchdog_recovery = _ms->getMillis();
}
}
// Selecting the most recent event by age rather than by largest timestamp is
// what keeps this correct across the millis() wrap — see RadioWatchdog.
RadioWatchdogDecision wd = radio_watchdog.update(
(uint32_t)_ms->getMillis(), is_recv, getRadioWatchdogMillis(),
(uint32_t)_radio->getLastRecvMillis(),
(uint32_t)_radio->getLastRadioInterruptMillis(),
(uint32_t)last_radio_active_ms);
if (wd.recover) {
_err_flags |= ERR_EVENT_RADIO_WATCHDOG;
MESH_DEBUG_PRINTLN("Radio watchdog: silent %lu ms, state=%d, recovering",
(unsigned long)wd.silent_ms, _radio->getRadioState());
_radio->idle();
_radio->startRecv();
radio_watchdog.noteRecovery();
}
}
#endif // WITH_MQTT_BRIDGE (radio watchdog)