Skip to content

fix(mesh): plug pooled-object and driver leaks in core paths - #11450

Merged
thebentern merged 1 commit into
developfrom
fix/core-mesh-pool-leaks
Aug 13, 2026
Merged

fix(mesh): plug pooled-object and driver leaks in core paths#11450
thebentern merged 1 commit into
developfrom
fix/core-mesh-pool-leaks

Conversation

@thebentern

@thebentern thebentern commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Three small memory fixes in core mesh paths:

MeshService::sendQueueStatusToPhone — pool slot lost on enqueue failure

The function allocates from the 4-slot staticQueueStatusPool, then returns without releasing when toPhoneQueueStatusQueue.enqueue() fails. Its two siblings in the same file (sendMqttMessageToClientProxy, sendClientNotification) both release on exactly this failure. The full-queue guard makes the failure impossible single-threaded, but the numFree/dequeue/enqueue sequence is not atomic and this path runs concurrently from the main loop and (on nRF52) the Bluefruit BLE write callback (NRF52Bluetooth.cpp onToRadioWritehandleToRadiosendToMeshsendQueueStatusToPhone). Each occurrence permanently loses a pool slot; after four, QueueStatus to the phone is dead until reboot.

MessageStore::storeTextInPool — write through null pool pointer

resetMessagePool() leaves g_messagePool null when the boot malloc fails; the read side (getTextFromPool) guards for that, but the write side memcpy'd through the null pointer on every stored text message. Now returns offset 0, which the read side already maps to "".

RF95Interface — driver leaked when radio probe fails

init() does lora = new RadioLibRF95(&module) with no destructor anywhere in the class. When init() then fails (e.g. begin() returns CHIP_NOT_FOUND while probing), initLoRa() destroys the interface via unique_ptr<RadioInterface> and the driver object leaks. Hold it in a unique_ptr — every other radio interface holds its driver by value, so this is the only one that needed it. iface remains a non-owning alias (~RadioLibInterface does not delete it).

Testing

  • trunk fmt clean; native test suite run planned across the fix series before merge.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling when message storage allocation fails.
    • Prevented queue-status resources from remaining allocated when delivery fails.
    • Improved radio driver ownership and lifecycle management for more reliable operation.

- MeshService::sendQueueStatusToPhone: release the pooled QueueStatus
  when the toPhone queue enqueue fails, matching what
  sendMqttMessageToClientProxy and sendClientNotification already do.
  The full-queue guard makes this failure rare, but the check/enqueue
  sequence is not atomic and this path is reachable concurrently from
  the main loop and the nRF52 BLE write callback; each failure
  permanently lost one of the four pool slots, and after four losses
  the phone never receives QueueStatus again until reboot.

- MessageStore::storeTextInPool: bail out when the boot-time pool
  allocation failed instead of memcpy'ing through a null pointer. The
  read side (getTextFromPool) already guards and maps offset 0 to an
  empty string.

- RF95Interface: hold the RadioLibRF95 driver in a unique_ptr. It is
  constructed in init(), and when init() subsequently fails (e.g. chip
  probe NOT_FOUND) initLoRa() destroys the interface, leaking the
  driver; every sibling interface holds its driver by value so nothing
  else needed a destructor here.
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The changes add allocation-failure handling for message storage and queue-status delivery. They also give RF95Interface ownership of its RadioLibRF95 driver through std::unique_ptr.

Changes

Resource safety updates

Layer / File(s) Summary
Allocation failure handling
src/MessageStore.cpp, src/mesh/MeshService.cpp
storeTextInPool returns offset 0 when the message pool is unavailable. Failed queue-status enqueueing releases the allocated status object.
RF95 driver ownership and initialization
src/mesh/RF95Interface.h, src/mesh/RF95Interface.cpp
RF95Interface stores the driver in std::unique_ptr and assigns iface from the owned object during initialization.

Estimated code review effort: 2 (Simple) | ~10 minutes

Mergeability Score: ⚪ Minimal · up to 41c93

The PR addresses three localized resource-safety paths. The remaining comment-length adjustment does not affect runtime behavior, so no actionable merge-blocking risk remains after normal checks.

Possibly related PRs

Suggested labels: bugfix

Suggested reviewers: caveman99, jp-bennett

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the pull request's main changes: fixing pooled-object and driver memory leaks in core mesh paths.
Description check ✅ Passed The description clearly explains all three fixes and identifies the current testing status, although device attestations are not completed.
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 fix/core-mesh-pool-leaks

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.

@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.

@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.

🧹 Nitpick comments (1)
src/mesh/RF95Interface.h (1)

14-17: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Shorten the ownership comment. Keep the initialization and ownership rationale within the repository’s one- or two-line comment limit.

🤖 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/RF95Interface.h` around lines 14 - 17, Shorten the ownership comment
in RF95Interface to one or two lines while preserving that the driver is
uniquely owned and constructed during init() to avoid leaks if initialization
fails.

Apply the same fix in `@src/mesh/RF95Interface.h` at line 18.

Source: Coding guidelines

🤖 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.

Nitpick comments:
In `@src/mesh/RF95Interface.h`:
- Around line 14-17: Shorten the ownership comment in RF95Interface to one or
two lines while preserving that the driver is uniquely owned and constructed
during init() to avoid leaks if initialization fails.

Apply the same fix in `@src/mesh/RF95Interface.h` at line 18.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: bfa51b8c-1be2-44cd-89bb-0a1dd6806f7a

📥 Commits

Reviewing files that changed from the base of the PR and between fdb644e and 41c9310.

📒 Files selected for processing (4)
  • src/MessageStore.cpp
  • src/mesh/MeshService.cpp
  • src/mesh/RF95Interface.cpp
  • src/mesh/RF95Interface.h

@thebentern thebentern added the bugfix Pull request that fixes bugs label Aug 12, 2026
@thebentern
thebentern merged commit d4de613 into develop Aug 13, 2026
62 of 65 checks passed
@thebentern
thebentern deleted the fix/core-mesh-pool-leaks branch August 13, 2026 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request that fixes bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant