Skip to content

Commit 847be34

Browse files
committed
fix(mqtt): keep throttled nodes administrable — priority-aware shed + stale expiry
Device testing at 'set dutycycle 1' on a busy mesh showed the node becoming un-administrable within ~2 minutes: the shed policy dropped its own CLI responses along with repeats, and parked retransmissions (which never expire) absorbed every budget refill. RxReservePacketManager now sheds by priority below the RX reserve — only pri > 1 outbound (multi-hop flood repeats, adverts, trace) is refused, so the node's own responses/ACKs (pri 0) and login/PATH replies (pri 1) still queue; below an emergency floor (reserve/2) everything is shed to protect capture. Queued packets untransmitted 30 s past their scheduled time are expired at dequeue via a pointer-keyed age table (the pool is a fixed set of packets, so pool_size slots cover every key). Under normal load the queue drains in milliseconds and neither policy triggers.
1 parent 1c9c629 commit 847be34

3 files changed

Lines changed: 84 additions & 20 deletions

File tree

MQTT_INTERNALS.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ RX processing needs a free packet from the static pool before `logRx()` (and thu
4646
MQTT uplink) can run — `Dispatcher::checkRecv()` silently discards received data when
4747
the pool is empty. Because the outbound queue holds pool packets with no expiry,
4848
duty-cycle throttling can park the entire pool waiting on TX budget, capping capture at
49-
the TX rate. Observer builds therefore use `RxReservePacketManager` (fork-owned,
50-
`src/helpers/RxReservePacketManager.h`), which refuses to queue retransmissions once
51-
the free pool drops below a reserve (a quarter of the pool) — the node sheds repeat
52-
load it has no TX budget for, and capture continues at full rate. Non-observer builds
53-
keep the upstream pool behavior.
49+
the TX rate — and the parked repeats absorb every budget refill, starving the node's
50+
own CLI responses and making it un-administrable over the mesh. Observer builds
51+
therefore use `RxReservePacketManager` (fork-owned,
52+
`src/helpers/RxReservePacketManager.h`): below the RX reserve (a quarter of the pool)
53+
it sheds only low-priority outbound (multi-hop flood repeats, adverts, trace), keeping
54+
the node's own responses/ACKs queueable; below a smaller emergency floor it sheds
55+
everything to keep capture alive. Queued packets still untransmitted 30 s past their
56+
scheduled time are expired at dequeue, so under throttle the queue holds only fresh
57+
traffic and admin responses reach the trickle of TX budget. Non-observer builds keep
58+
the upstream pool behavior.
5459

5560
### `/mqtt_prefs` file format
5661

RESTORE_UPSTREAM_NOTES.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ Found during the on-device duty-cycle load test (`set dutycycle 1`): queued
4848
retransmissions hold static-pool packets with no expiry, so heavy throttling parks the
4949
whole pool in the send queue. `Dispatcher::checkRecv()` then drops received packets
5050
before `logRx()` runs, capping MQTT capture at the TX rate (each TX frees one packet
51-
for one RX). This is inherent upstream behavior — the old fork's `next_tx` spacing had
52-
the same steady-state drain — but it defeats the observer's purpose. Mitigated on
53-
observer builds by `RxReservePacketManager` (`src/helpers/RxReservePacketManager.h`):
54-
retransmissions are shed once the free pool drops below a quarter of the pool, keeping
55-
RX capture at full rate. See MQTT_INTERNALS.md "Capture vs. duty-cycle throttling".
51+
for one RX). Parked repeats also absorb every budget refill, starving the node's own
52+
CLI responses — device-confirmed: a 1%-duty node on a busy mesh became
53+
un-administrable within ~2 minutes. This is inherent upstream behavior — the old
54+
fork's `next_tx` spacing had the same steady-state drain — but it defeats the
55+
observer's purpose. Mitigated on observer builds by `RxReservePacketManager`
56+
(`src/helpers/RxReservePacketManager.h`): priority-aware shedding below a pool
57+
reserve (own responses/ACKs stay queueable) plus 30 s expiry of stale queued
58+
outbound. See MQTT_INTERNALS.md "Capture vs. duty-cycle throttling".
5659

5760
## Phase 2 — CAD and FEM RX gain (NOT done; needs care + device testing)
5861

src/helpers/RxReservePacketManager.h

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,83 @@
88
// discards the received bytes before logRx() when allocNew() fails. Under duty-cycle
99
// throttling the outbound queue can park the entire pool waiting on TX budget, which
1010
// starves RX allocation and silently caps MQTT capture at the TX rate (each completed
11-
// TX frees exactly one packet for exactly one more RX).
11+
// TX frees exactly one packet for exactly one more RX). Parked retransmissions also
12+
// absorb every budget refill, starving the node's own CLI responses/ACKs and making
13+
// a heavily-throttled node un-administrable over the mesh.
1214
//
13-
// This manager sheds *retransmissions* instead: once the free pool drops below the
14-
// reserve, outbound packets are refused (freed straight back to the pool) so RX
15-
// allocation — and therefore capture — continues at full rate. The node was already
16-
// dropping traffic in that state; this chooses to drop repeats it has no TX budget
17-
// for anyway, rather than capture.
15+
// Two policies fix this, both confined to this manager:
16+
//
17+
// 1. Priority-aware shedding. Below the RX reserve, only low-priority outbound
18+
// (priority > 1: multi-hop flood repeats, adverts, trace) is refused; the node's
19+
// own responses/ACKs (pri 0) and login/PATH replies (pri 1) still queue. Below the
20+
// smaller emergency floor everything is shed to keep capture alive.
21+
// 2. Stale-packet expiry. A queued packet still untransmitted STALE_OUTBOUND_MS past
22+
// its scheduled time is dropped at the next dequeue — a repeat delayed that long is
23+
// noise (the flood has long since propagated), and a CLI response that old has
24+
// already timed out at the client. Under normal load the queue drains in
25+
// milliseconds and this never triggers; under throttle it frees the pool and lets
26+
// fresh traffic (including admin responses) compete for the trickle of TX budget.
1827
class RxReservePacketManager : public StaticPoolPacketManager {
19-
int _rx_reserve;
28+
int _rx_reserve, _emergency_floor;
29+
int _cap;
30+
// scheduled_for per queued packet, keyed by packet pointer. The pool is a fixed set
31+
// of _cap Packet objects, so _cap slots cover every possible key with no eviction.
32+
struct AgeEntry { mesh::Packet* pkt; uint32_t scheduled_for; };
33+
AgeEntry* _ages;
34+
35+
static const uint32_t STALE_OUTBOUND_MS = 30000;
36+
static const uint8_t MAX_PROTECTED_PRI = 1; // pri 0-1 = own responses/ACKs/replies
37+
38+
void recordAge(mesh::Packet* packet, uint32_t scheduled_for) {
39+
int empty = -1;
40+
for (int i = 0; i < _cap; i++) {
41+
if (_ages[i].pkt == packet) { _ages[i].scheduled_for = scheduled_for; return; }
42+
if (empty < 0 && _ages[i].pkt == NULL) empty = i;
43+
}
44+
if (empty >= 0) { _ages[empty].pkt = packet; _ages[empty].scheduled_for = scheduled_for; }
45+
}
46+
47+
bool lookupAge(const mesh::Packet* packet, uint32_t* scheduled_for) const {
48+
for (int i = 0; i < _cap; i++) {
49+
if (_ages[i].pkt == packet) { *scheduled_for = _ages[i].scheduled_for; return true; }
50+
}
51+
return false;
52+
}
53+
2054
public:
2155
RxReservePacketManager(int pool_size, int rx_reserve)
22-
: StaticPoolPacketManager(pool_size), _rx_reserve(rx_reserve) {}
56+
: StaticPoolPacketManager(pool_size), _rx_reserve(rx_reserve),
57+
_emergency_floor(rx_reserve / 2), _cap(pool_size) {
58+
_ages = new AgeEntry[pool_size];
59+
for (int i = 0; i < pool_size; i++) { _ages[i].pkt = NULL; _ages[i].scheduled_for = 0; }
60+
}
2361

2462
void queueOutbound(mesh::Packet* packet, uint8_t priority, uint32_t scheduled_for) override {
25-
if (getFreeCount() < _rx_reserve) {
26-
MESH_DEBUG_PRINTLN("RxReservePacketManager: pool below RX reserve, shedding outbound");
63+
int free_count = getFreeCount();
64+
if (free_count < _emergency_floor
65+
|| (free_count < _rx_reserve && priority > MAX_PROTECTED_PRI)) {
66+
MESH_DEBUG_PRINTLN("RxReservePacketManager: pool below RX reserve, shedding outbound (pri %d)", (int)priority);
2767
free(packet);
2868
return;
2969
}
70+
recordAge(packet, scheduled_for);
3071
StaticPoolPacketManager::queueOutbound(packet, priority, scheduled_for);
3172
}
73+
74+
mesh::Packet* getNextOutbound(uint32_t now) override {
75+
// Expire queued packets that have waited too long past their scheduled time.
76+
for (int i = getOutboundTotal() - 1; i >= 0; i--) {
77+
mesh::Packet* pkt = getOutboundByIdx(i);
78+
uint32_t scheduled_for;
79+
if (pkt && lookupAge(pkt, &scheduled_for)
80+
&& (int32_t)(now - scheduled_for) > (int32_t)STALE_OUTBOUND_MS) {
81+
MESH_DEBUG_PRINTLN("RxReservePacketManager: dropping stale queued outbound");
82+
removeOutboundByIdx(i);
83+
free(pkt);
84+
}
85+
}
86+
return StaticPoolPacketManager::getNextOutbound(now);
87+
}
3288
};
3389

3490
// The packet manager for an app build: observer builds reserve a quarter of the pool

0 commit comments

Comments
 (0)