Skip to content

fix(mesh): skip pre-encryption packet copy when MQTT does not need it#10434

Open
DatanoiseTV wants to merge 1 commit into
meshtastic:developfrom
DatanoiseTV:fix/router-mqtt-alloc-hoist
Open

fix(mesh): skip pre-encryption packet copy when MQTT does not need it#10434
DatanoiseTV wants to merge 1 commit into
meshtastic:developfrom
DatanoiseTV:fix/router-mqtt-alloc-hoist

Conversation

@DatanoiseTV
Copy link
Copy Markdown
Contributor

Router::send unconditionally allocCopy/release'd a pre-encryption clone
of every relayed packet, even on builds where MQTT was excluded
(MESHTASTIC_EXCLUDE_MQTT) or runtime-disabled (mqtt module nullptr or
moduleConfig.mqtt.enabled=false) or when the packet was a relay rather
than from us. The copy is only fed to MQTT::onSend, so guard the alloc
on the same predicate the call site already used. On nodes near pool
exhaustion this halves the alloc rate on the hot path.

Split out from #10424 per @thebentern's request — single-concern PR.

Build verification

pio run -e t-deck-tft succeeds, no new warnings.

Attestations

  • I have tested that my proposed changes behave as described — review/static-analysis only, not on-air.
  • On-hardware testing requested from community: build-verified t-deck-tft only.

Router::send unconditionally allocCopy/release'd a pre-encryption clone
of every relayed packet, even on builds where MQTT was excluded
(MESHTASTIC_EXCLUDE_MQTT) or runtime-disabled (mqtt module nullptr or
moduleConfig.mqtt.enabled=false) or when the packet was a relay rather
than from us. The copy is only fed to MQTT::onSend, so guard the alloc
on the same predicate the call site already used. On nodes near pool
exhaustion this halves the alloc rate on the hot path.
@github-actions github-actions Bot added needs-review Needs human review bugfix Pull request that fixes bugs labels May 9, 2026
@thebentern thebentern requested a review from Copilot May 9, 2026 11:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Reduces packet-pool pressure in Router::send by avoiding an unconditional pre-encryption packet clone when MQTT is compile-time excluded or runtime-disabled, aligning allocation behavior with the existing MQTT publish guard.

Changes:

  • Allocate the pre-encryption p_decoded clone only when MQTT publishing will occur.
  • Guard packetPool.release() calls to avoid releasing a null pointer.
  • Use the existence of p_decoded to gate mqtt->onSend() and release in the MQTT-enabled build.

Comment thread src/mesh/Router.cpp
Comment on lines +379 to +384
const bool needDecodedForMqtt = moduleConfig.mqtt.enabled && isFromUs(p) && mqtt;
if (needDecodedForMqtt) {
DEBUG_HEAP_BEFORE;
p_decoded = packetPool.allocCopy(*p);
DEBUG_HEAP_AFTER("Router::send", p_decoded);
}
Comment thread src/mesh/Router.cpp
Comment on lines +396 to 399
if (p_decoded) {
mqtt->onSend(*p, *p_decoded, chIndex);
packetPool.release(p_decoded);
}
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 needs-review Needs human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants