Skip to content

Release packets the interface declines to send - #11087

Merged
caveman99 merged 2 commits into
developfrom
nolora-packet-leak
Jul 20, 2026
Merged

Release packets the interface declines to send#11087
caveman99 merged 2 commits into
developfrom
nolora-packet-leak

Conversation

@caveman99

@caveman99 caveman99 commented Jul 20, 2026

Copy link
Copy Markdown
Member

RadioLibInterface::send() returns ERRNO_SHOULD_RELEASE without releasing when to is NODENUM_BROADCAST_NO_LORA. Only MeshService::sendToMesh honoured that return; other callers discarded it and leaked a packet pool slot each time.

isBroadcast() accepts NODENUM_BROADCAST_NO_LORA and nothing validates the to field of an inbound packet, so a received frame addressed to it reached the rebroadcast path and the ack path.

Changes:

  • NextHopRouter::perhapsRebroadcast returns early for NODENUM_BROADCAST_NO_LORA.
  • NextHopRouter::perhapsRebroadcast, NextHopRouter::doRetransmissions, RoutingModule::sendAckNak and the MQTT ack path release on ERRNO_SHOULD_RELEASE.

Tests in test_nexthop_routing cover the guard, a normal broadcast control, and the release path.

Summary by CodeRabbit

  • Bug Fixes

    • Prevent rebroadcast attempts for broadcasts meant to avoid LoRa transmission.
    • Improved packet ownership handling when send operations return “should release,” reducing packet leak risk across retransmissions, relays, ACK/NAK sending, and MQTT local ACK handling.
    • Hardened retransmission and relay forwarding paths to correctly release packets when a send declines ownership.
  • Tests

    • Added/extended coverage for no-LoRa broadcast guard behavior and declined-send scenarios, including validation of rebroadcast attempts reaching the radio send stub.

RadioLibInterface::send() returns ERRNO_SHOULD_RELEASE without releasing when
to is NODENUM_BROADCAST_NO_LORA. Callers that discarded the return value leaked
a pool slot per packet.

Skip rebroadcast of NODENUM_BROADCAST_NO_LORA and honour the return value in
NextHopRouter, RoutingModule::sendAckNak and the MQTT ack path.
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 3de70516-a442-413f-8172-0530460477f9

📥 Commits

Reviewing files that changed from the base of the PR and between 6912988 and 1b9a8ea.

📒 Files selected for processing (1)
  • src/mesh/NextHopRouter.cpp

📝 Walkthrough

Walkthrough

The routing changes prevent rebroadcasting no-LoRa broadcasts and release packets when send operations request ownership return. ACK paths adopt the same handling, with tests covering ordinary, filtered, and declined rebroadcast attempts.

Changes

Routing packet lifecycle

Layer / File(s) Summary
Rebroadcast guards and ownership
src/mesh/NextHopRouter.cpp
perhapsRebroadcast() filters NODENUM_BROADCAST_NO_LORA and releases cloned packets when sends return ERRNO_SHOULD_RELEASE; retransmission fallbacks apply the same handling.
ACK packet release handling
src/modules/RoutingModule.cpp, src/mqtt/MQTT.cpp
Local ACK/NAK sends now conditionally release packets when sendLocal() returns ERRNO_SHOULD_RELEASE.
Rebroadcast test coverage
src/mesh/NextHopRouter.h, test/test_nexthop_routing/test_main.cpp
Test-only access, a mock radio interface, rebroadcast candidates, and three registered tests cover ordinary broadcasts, no-LoRa broadcasts, and declined sends.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested labels: bugfix, mesh

Suggested reviewers: ndoo, nomdetom

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: releasing packets when a send call declines them.
Description check ✅ Passed The description covers the bug, the code changes, and testing, with only the optional attestation checklist omitted.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nolora-packet-leak

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/mesh/NextHopRouter.cpp (1)

406-438: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Release directed retransmission copies on ERRNO_SHOULD_RELEASE.

While the broadcast branch (lines 435-438) was correctly updated to release the copied packet if send() returns ERRNO_SHOULD_RELEASE, the directed retransmission branches immediately above it were missed. This will cause a packet pool memory leak if a directed packet's transmission is declined or fails synchronously (e.g., if the interface is disabled).

Apply the same conditional release logic to the directed branches.

🐛 Proposed fix for the missing release checks
-                        if (auto *copy = packetPool.allocCopy(*p.packet))
-                            FloodingRouter::send(copy);
+                        if (auto *copy = packetPool.allocCopy(*p.packet)) {
+                            if (FloodingRouter::send(copy) == ERRNO_SHOULD_RELEASE)
+                                packetPool.release(copy);
+                        }
                     } else {
 `#if` NEXTHOP_EARLY_FLOOD_ON_UNVERIFIED
                         // M4 (gated): if the route isn't proven healthy, don't spend a second directed
                         // attempt - start flooding one retry sooner to cut recovery latency. A verified
                         // route (fresh, zero recent failures) keeps the unchanged directed-retry path so
                         // the sparse-mesh happy path is untouched.
                         RouteHealth *h = findRouteHealth(p.packet->to);
                         bool verified = h && h->consecutiveFailures == 0 && !isRouteStale(*h, now);
                         if (!verified) {
                             p.packet->next_hop = NO_NEXT_HOP_PREFERENCE;
                             meshtastic_NodeInfoLite *sentTo = nodeDB->getMeshNode(p.packet->to);
                             if (sentTo)
                                 sentTo->next_hop = NO_NEXT_HOP_PREFERENCE;
-                            if (auto *copy = packetPool.allocCopy(*p.packet))
-                                FloodingRouter::send(copy);
+                            if (auto *copy = packetPool.allocCopy(*p.packet)) {
+                                if (FloodingRouter::send(copy) == ERRNO_SHOULD_RELEASE)
+                                    packetPool.release(copy);
+                            }
                         } else {
-                            if (auto *copy = packetPool.allocCopy(*p.packet))
-                                NextHopRouter::send(copy);
+                            if (auto *copy = packetPool.allocCopy(*p.packet)) {
+                                if (NextHopRouter::send(copy) == ERRNO_SHOULD_RELEASE)
+                                    packetPool.release(copy);
+                            }
                         }
 `#else`
-                        if (auto *copy = packetPool.allocCopy(*p.packet))
-                            NextHopRouter::send(copy);
+                        if (auto *copy = packetPool.allocCopy(*p.packet)) {
+                            if (NextHopRouter::send(copy) == ERRNO_SHOULD_RELEASE)
+                                packetPool.release(copy);
+                        }
 `#endif`
                     }
                 } else {
                     // Note: we call the superclass version because we don't want to have our version of send() add a new
                     // retransmission record
                     if (auto *copy = packetPool.allocCopy(*p.packet)) {
                         if (FloodingRouter::send(copy) == ERRNO_SHOULD_RELEASE)
                             packetPool.release(copy);
                     }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/mesh/NextHopRouter.cpp` around lines 406 - 438, Update the directed
retransmission branches in NextHopRouter’s send flow, including both the
early-flood and verified-route paths, to check FloodingRouter::send or
NextHopRouter::send for ERRNO_SHOULD_RELEASE and release the copied packet
through packetPool when returned. Preserve the existing routing decisions and
copy allocation behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/mesh/NextHopRouter.cpp`:
- Around line 406-438: Update the directed retransmission branches in
NextHopRouter’s send flow, including both the early-flood and verified-route
paths, to check FloodingRouter::send or NextHopRouter::send for
ERRNO_SHOULD_RELEASE and release the copied packet through packetPool when
returned. Preserve the existing routing decisions and copy allocation behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 36723a80-aedf-4226-a929-59ef8a260683

📥 Commits

Reviewing files that changed from the base of the PR and between 829ff80 and 6912988.

📒 Files selected for processing (5)
  • src/mesh/NextHopRouter.cpp
  • src/mesh/NextHopRouter.h
  • src/modules/RoutingModule.cpp
  • src/mqtt/MQTT.cpp
  • test/test_nexthop_routing/test_main.cpp

@caveman99
caveman99 merged commit 290967f into develop Jul 20, 2026
4 of 6 checks passed
@caveman99
caveman99 deleted the nolora-packet-leak branch July 20, 2026 11:43
@github-actions

Copy link
Copy Markdown
Contributor

⚡ Try this PR in the Web Flasher

Note

Building this pull request… the flash button, badges and supported-board
list will appear here automatically once CI finishes.

vidplace7 pushed a commit that referenced this pull request Jul 27, 2026
relayOpaquePacket() allocates a copy and returns Router::send(relay) == ERRNO_OK, discarding ERRNO_SHOULD_RELEASE. The interface returns that for NODENUM_BROADCAST_NO_LORA, so the copy is never freed and one pool slot leaks per frame.

The opaque path is reached for packets on a channel we have no key for, so no key or PSK is needed: a frame with an unknown channel hash, to=NODENUM_BROADCAST_NO_LORA, a nonzero id and hop_limit>0 leaks a slot, and roughly MAX_PACKETS of them exhaust the pool until reboot.

#11087 fixed this pattern in perhapsRebroadcast and the retransmission paths but did not cover relayOpaquePacket, which was added separately with the opaque relay path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants