fix(mqtt-neighbors): budget the JSON pool separately from the publish buffer

ArduinoJson v7 hands out document-pool blocks in fixed 4096-byte chunks, so
the pool is not bounded by the size of the text it serialises to. Budgeting
it at NEIGHBORS_JSON_BUFFER_SIZE starved it once the table grew: a 50-entry
table needs 12541 B of pool against the 10240 B cap, and a starved allocator
sets doc.overflowed(), which makes buildNeighborsMessage return 0 and drop
the entire publish rather than truncating the tail.

Repeaters with roughly 40 or more neighbours therefore published no neighbors
message at all, silently, while smaller tables published normally.

Give the pool its own NEIGHBORS_DOC_POOL_BUDGET and add an explicit
NEIGHBORS_MAX_PUBLISH_ENTRIES cap alongside the existing text-size check.
This commit is contained in:
agessaman committed 2026-08-03 10:29:32 -07:00
1 parent 37444be7b5
commit 34037f202d
3 files changed
+16 -4

No files matched your search

+3 -2
View File
@@ -1940,7 +1940,8 @@ bool MyMesh::completeNeighborDiscoverEntry() {
size_t added = MQTTMessageBuilder::measureNeighborsMessageEntry(measured);
if (neighbor_discover_publish_count > 0) added++; // array comma
if (neighbor_discover_json_size + added >= MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE) {
if (neighbor_discover_json_size + added >= MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE ||
neighbor_discover_publish_count >= MQTTBridge::NEIGHBORS_MAX_PUBLISH_ENTRIES) {
neighbor_discover_truncated = true;
finishNeighborDiscover();
return false;
@@ -2026,7 +2027,7 @@ static bool neighborPublishEntryComesBefore(
// single-shot doc (overflow path removes+breaks, so no further growth after free).
struct NeighborsDocAllocator : ArduinoJson::Allocator {
size_t used = 0;
static const size_t kBudget = MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE;
static const size_t kBudget = MQTTBridge::NEIGHBORS_DOC_POOL_BUDGET;
void* allocate(size_t size) override {
if (used >= kBudget || size > kBudget - used) return nullptr;
+3 -2
View File
@@ -1826,7 +1826,8 @@ bool MyMesh::completeNeighborDiscoverEntry() {
size_t added = MQTTMessageBuilder::measureNeighborsMessageEntry(measured);
if (neighbor_discover_publish_count > 0) added++; // array comma
if (neighbor_discover_json_size + added >= MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE) {
if (neighbor_discover_json_size + added >= MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE ||
neighbor_discover_publish_count >= MQTTBridge::NEIGHBORS_MAX_PUBLISH_ENTRIES) {
neighbor_discover_truncated = true;
finishNeighborDiscover();
return false;
@@ -1910,7 +1911,7 @@ static bool neighborPublishEntryComesBefore(
// single-shot doc (overflow path removes+breaks, so no further growth after free).
struct NeighborsDocAllocator : ArduinoJson::Allocator {
size_t used = 0;
static const size_t kBudget = MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE;
static const size_t kBudget = MQTTBridge::NEIGHBORS_DOC_POOL_BUDGET;
void* allocate(size_t size) override {
if (used >= kBudget || size > kBudget - used) return nullptr;
+10
View File
@@ -515,6 +515,16 @@ public:
// persistent buffer and the mesh's transient build buffer.
static const size_t NEIGHBORS_JSON_BUFFER_SIZE = 10240;
// The ArduinoJson pool is NOT bounded by the text buffer: v7 hands out pool
// blocks in fixed 4096-byte chunks, so a table that just fits the text buffer
// can still need well over it in pool. Budgeting the pool at the text size
// starves it, and a starved pool sets doc.overflowed() — which drops the whole
// publish instead of truncating. Measured need is 12541 B for 50 entries.
static const size_t NEIGHBORS_DOC_POOL_BUDGET = 16384;
// Entries per publish; the neighbour cache and text buffer are the real limit.
static const int NEIGHBORS_MAX_PUBLISH_ENTRIES = MAX_NEIGHBOURS;
// Called by the mesh (Core 1) once a neighbor-discovery pass has built the
// table JSON. Copies it into the persistent PSRAM buffer and raises the
// publish-pending flag for the MQTT task; a request is dropped if one is