Keep retry cancellation and radio recovery scoped to their owners

Cancel packets retained by the radio driver when their mesh retry is retired.
Echo-confirmed delivery completes application replies; explicit cancellation
releases failure references. Let active transmissions finish and prevent a
cancelled packet from returning after a driver timeout.

Preserve final direct-echo waits when only the other radio profile or crossover
policy changes. Recover verified radio-profile settings from backup even when
filesystem repair fails, and protect the backup after a failed save rollback.

Add regressions for direct and flood cancellation, confirmed command replies,
active TX completion and timeouts, subsequent packet cleanup, independent
profile echo ownership, and storage rename/removal failures.
This commit is contained in:
mikecarper committed 2026-09-15 08:27:05 -07:00
1 parent dde87f1d15
commit e720132a8b
8 files changed
+274 -29

No files matched your search

+17 -4
View File
@@ -67,6 +67,7 @@ void Dispatcher::begin() {
outbound_radio_retry_at = 0;
outbound_radio_retry_pending = false;
outbound_radio_retry_used = false;
outbound_cancellation = OutboundCancellation::None;
ota_tx_airtime = 0;
radio_nonrx_start = _ms->getMillis();
@@ -187,7 +188,7 @@ bool Dispatcher::startOutboundTransmit() {
}
bool Dispatcher::scheduleOutboundRadioRetry() {
if (outbound == NULL || outbound_radio_retry_used) return false;
if (outbound == NULL || outbound_radio_retry_used || outbound_cancellation != OutboundCancellation::None) return false;
outbound_radio_retry_used = true;
outbound_radio_retry_pending = true;
@@ -201,12 +202,21 @@ void Dispatcher::failOutboundTransmit() {
if (outbound == NULL) return;
restoreOutboundTxOverrides();
logTxFail(outbound, outbound->getRawLength());
onSendFail(outbound);
if (outbound_cancellation == OutboundCancellation::Delivered) {
// A downstream echo confirms delivery even if the driver's completion
// failed. Pending command replies must complete their application barrier.
onSendComplete(outbound);
} else {
if (outbound_cancellation == OutboundCancellation::None) logTxFail(outbound, outbound->getRawLength());
// Explicit cancellation still releases application references, including
// pending replies and battery alerts, through the ordinary failure hook.
onSendFail(outbound);
}
releasePacket(outbound);
outbound = NULL;
outbound_radio_retry_pending = false;
outbound_radio_retry_used = false;
outbound_cancellation = OutboundCancellation::None;
}
int Dispatcher::calcRxDelay(float score, uint32_t air_time) const {
@@ -358,7 +368,8 @@ void Dispatcher::loop() {
checkRecv();
// A cancelled or expired packet must retire even if the chip never
// becomes available for another retune (including an armed OTA reboot).
if (!isPacketRadioCurrent(outbound) || !allowPacketTransmit(outbound)) {
if (outbound_cancellation != OutboundCancellation::None
|| !isPacketRadioCurrent(outbound) || !allowPacketTransmit(outbound)) {
failOutboundTransmit();
return;
}
@@ -429,6 +440,7 @@ void Dispatcher::loop() {
outbound = NULL;
outbound_radio_retry_pending = false;
outbound_radio_retry_used = false;
outbound_cancellation = OutboundCancellation::None;
} else if (millisHasNowPassed(outbound_expiry)) {
MESH_DEBUG_PRINTLN("%s Dispatcher::loop(): WARNING: outbound packed send timed out!", getLogDateTime());
@@ -840,6 +852,7 @@ void Dispatcher::checkSend() {
if (outbound) {
outbound_radio_retry_pending = false;
outbound_radio_retry_used = false;
outbound_cancellation = OutboundCancellation::None;
if (!allowPacketTransmit(outbound)) {
MESH_DEBUG_PRINTLN("%s Dispatcher::checkSend(): packet no longer allowed, type=%u", getLogDateTime(),
+10
View File
@@ -277,6 +277,7 @@ typedef uint32_t DispatcherAction;
* and scheduling of outbound Packets.
*/
class Dispatcher {
enum class OutboundCancellation : uint8_t { None, Cancelled, Delivered };
Packet* outbound; // current outbound packet
unsigned long outbound_expiry, outbound_start, total_air_time, rx_air_time;
unsigned long outbound_radio_retry_at;
@@ -299,6 +300,7 @@ class Dispatcher {
bool dispatcher_started;
bool outbound_radio_retry_pending;
bool outbound_radio_retry_used;
OutboundCancellation outbound_cancellation = OutboundCancellation::None;
#ifndef RADIO_LIVENESS_SOFT_ONLY
bool nonrx_soft_recovery_attempted;
#endif
@@ -425,6 +427,14 @@ protected:
virtual uint32_t getRadioWatchdogMillis() const; // observer-only radio recovery
#endif
const Packet* getOutboundInFlight() const { return outbound; }
// A retry may be retained outside the manager during radio recovery. Mark
// it here and let loop() release it after RX callbacks finish. An on-air
// transmission completes normally, but cannot retry after a later timeout.
void cancelOutboundRadioRetry(const Packet* packet, bool delivered = false) {
if (packet != NULL && packet == outbound) {
outbound_cancellation = delivered ? OutboundCancellation::Delivered : OutboundCancellation::Cancelled;
}
}
// Milliseconds until Dispatcher can next make progress on a queued packet.
// This includes queue schedules, delayed inbound processing, airtime-budget
// waits, and short channel-busy deferrals.
+12 -17
View File
@@ -306,7 +306,12 @@ void Mesh::loop() {
for (int i = 0; i < TOTAL_DIRECT_RETRY_SLOTS; ++i) {
if (!_direct_retries[i].active) continue;
const auto* packet = _direct_retries[i].queued ? _direct_retries[i].packet : _direct_retries[i].trigger_packet;
if (_direct_retries[i].waiting_final_echo || (packet && !isPacketRadioCurrent(packet))) retireDirectRetrySlot(i);
// Final direct echoes retain metadata only. Their slot still identifies
// the profile, so changing the other channel must not cancel the wait.
const uint8_t profile = i / MAX_DIRECT_RETRY_SLOTS;
if ((_direct_retries[i].waiting_final_echo
&& _retry_radio_generations[profile] != p->generation[profile])
|| (packet && !isPacketRadioCurrent(packet))) retireDirectRetrySlot(i);
}
for (int i = 0; i < TOTAL_FLOOD_RETRY_SLOTS; ++i) {
if (!_flood_retries[i].active) continue;
@@ -1443,12 +1448,13 @@ void Mesh::clearDirectRetrySlot(int idx) {
if (rebuild_timeout) rebuildNextDirectRetryTimeout();
}
void Mesh::retireDirectRetrySlot(int idx) {
void Mesh::retireDirectRetrySlot(int idx, bool delivered) {
if (idx < 0 || idx >= TOTAL_DIRECT_RETRY_SLOTS || !_direct_retries[idx].active) {
return;
}
Packet* retry = _direct_retries[idx].queued ? _direct_retries[idx].packet : NULL;
cancelOutboundRadioRetry(retry ? retry : _direct_retries[idx].trigger_packet, delivered);
if (retry != NULL && retry != getOutboundInFlight()) {
for (int j = 0; j < _mgr->getOutboundTotal(); j++) {
if (_mgr->getOutboundByIdx(j) != retry) continue;
@@ -1627,18 +1633,6 @@ bool Mesh::cancelDirectRetryOnEcho(const Packet* packet) {
onDirectRetryEvent("good", _direct_retries[i].packet, echo_millis, retry_attempt,
_direct_retries[i].next_hop_hash, _direct_retries[i].next_hop_hash_len,
_direct_retries[i].payload_type);
if (_direct_retries[i].queued) {
for (int j = 0; j < _mgr->getOutboundTotal(); j++) {
if (_mgr->getOutboundByIdx(j) == _direct_retries[i].packet) {
Packet* pending = _mgr->removeOutboundByIdx(j);
if (pending) {
releasePacket(pending);
}
break;
}
}
}
clearDirectRetrySlot(i);
} else {
if (_direct_retries[i].trigger_packet != NULL) {
_direct_retries[i].trigger_packet->_snr = echo_snr_x4;
@@ -1648,8 +1642,8 @@ bool Mesh::cancelDirectRetryOnEcho(const Packet* packet) {
: (uint32_t)(_ms->getMillis() - _direct_retries[i].echo_wait_started_at);
onDirectRetryEvent("good", _direct_retries[i].trigger_packet, echo_millis, _direct_retries[i].retry_attempts_sent + 1,
_direct_retries[i].next_hop_hash, _direct_retries[i].next_hop_hash_len);
clearDirectRetrySlot(i);
}
retireDirectRetrySlot(i, true);
cleared = true;
}
@@ -2045,12 +2039,13 @@ void Mesh::clearFloodRetrySlot(int idx) {
if (rebuild_timeout) rebuildNextFloodRetryTimeout();
}
void Mesh::retireFloodRetrySlot(int idx) {
void Mesh::retireFloodRetrySlot(int idx, bool delivered) {
if (idx < 0 || idx >= TOTAL_FLOOD_RETRY_SLOTS || !_flood_retries[idx].active) {
return;
}
Packet* retry = _flood_retries[idx].queued ? _flood_retries[idx].packet : NULL;
cancelOutboundRadioRetry(retry ? retry : _flood_retries[idx].trigger_packet, delivered);
if (retry != NULL && retry != getOutboundInFlight()) {
for (int j = 0; j < _mgr->getOutboundTotal(); j++) {
if (_mgr->getOutboundByIdx(j) != retry) continue;
@@ -2479,7 +2474,7 @@ bool Mesh::cancelFloodRetryOnEcho(const Packet* packet) {
: _flood_retries[i].retry_attempts_sent + 1;
onFloodRetryEvent("good", packet, echo_millis, retry_attempt);
retireFloodRetrySlot(i);
retireFloodRetrySlot(i, true);
cleared = true;
}
+2 -2
View File
@@ -169,7 +169,7 @@ class Mesh : public Dispatcher {
void rebuildNextDirectRetryTimeout();
void rebuildNextFloodRetryTimeout();
void clearDirectRetrySlot(int idx);
void retireDirectRetrySlot(int idx);
void retireDirectRetrySlot(int idx, bool delivered = false);
void calculateDirectRetryKey(const Packet* packet, uint8_t* dest_key) const;
bool calculateTraceReplacementKey(const Packet* packet, uint8_t* dest_key) const;
void replaceQueuedTraceRetries(const Packet* packet);
@@ -181,7 +181,7 @@ class Mesh : public Dispatcher {
bool canDecodeDirectPayloadForSelf(const Packet* packet);
void maybeScheduleDirectRetry(const Packet* packet, uint8_t priority, bool final_hop_retry = false);
void clearFloodRetrySlot(int idx);
void retireFloodRetrySlot(int idx);
void retireFloodRetrySlot(int idx, bool delivered = false);
void replaceQueuedSelfAdvertRetries(const Packet* packet);
bool cancelFloodRetryOnEcho(const Packet* packet);
bool getRecentAdvertTimestamp(const Packet* packet, uint32_t& timestamp) const;
+8 -5
View File
@@ -69,7 +69,7 @@ bool RadioProfileCLI::readImage(const char* path, uint8_t* bytes, size_t size) {
}
bool RadioProfileCLI::writeImage(const char* path, const uint8_t* bytes, size_t size) {
if (fs_->exists(path)) fs_->remove(path);
if (fs_->exists(path) && !fs_->remove(path)) return false;
#if defined(NRF52_PLATFORM)
File file(*fs_);
if (!file.open(path, FILE_O_WRITE)) return false;
@@ -100,11 +100,11 @@ bool RadioProfileCLI::save(const RadioProfileConfig& config, uint16_t preamble,
memcpy(image + ImageSize - 4, &crc, 4);
if (!writeImage(TempPath, image, sizeof(image))) return false;
if (fs_->exists(ImagePath)) {
if (fs_->exists(BackupPath)) fs_->remove(BackupPath);
if (fs_->exists(BackupPath) && !fs_->remove(BackupPath)) return false;
if (!fs_->rename(ImagePath, BackupPath)) return false;
}
if (!fs_->rename(TempPath, ImagePath)) {
if (fs_->exists(BackupPath)) fs_->rename(BackupPath, ImagePath);
if (fs_->exists(BackupPath) && !fs_->rename(BackupPath, ImagePath)) hold_ = true;
return false;
}
if (fs_->exists(BackupPath)) fs_->remove(BackupPath);
@@ -122,8 +122,11 @@ void RadioProfileCLI::begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool in
uint8_t bytes[ImageSize];
bool loaded = readImage(ImagePath, bytes, sizeof(bytes));
if (!loaded && readImage(BackupPath, bytes, sizeof(bytes))) {
if (fs_->exists(ImagePath)) fs_->remove(ImagePath);
loaded = fs_->rename(BackupPath, ImagePath);
// Use the verified backup even when repairing the interrupted save is
// impossible. Keep writes held so the sole committed image stays intact.
loaded = true;
hold_ = (fs_->exists(ImagePath) && !fs_->remove(ImagePath))
|| !fs_->rename(BackupPath, ImagePath);
}
if (!loaded) {
hold_ = fs_->exists(ImagePath) || fs_->exists(BackupPath);