← Back to Pull requests

lwipUDP: fix pbuf leak in endPacket() and uninitialised members #560

Closed
unknown closed this pull request without merging on Nov 18, 2026 09:14 UTC
View on GitHub
unknown unknown commented on Nov 17, 2026 06:40 UTC
Author

Problem

On the Portenta C33, WiFiUDP stops sending after roughly a hundred datagrams. From then on endPacket() returns 0 for every packet, while WiFi.status() still reports WL_CONNECTED.

lwipUDP::endPacket() clears its pointer after a successful udp_sendto() without freeing the pbuf:

if (ERR_OK != udp_sendto(_udp.pcb, _data, ...)) {
    ...
    _data = pbuffer_free_data(_data);   // freed on failure
    ...
    return 0;
}

_data = NULL;                           // not freed on success

udp_sendto() does not take ownership of the pbuf, so each datagram leaks one. When the lwIP heap is used up, write() can no longer allocate and sending stops.

Changes

1. endPacket(): call pbuf_free(_data) after a successful udp_sendto(). It uses the same __disable_irq() / __enable_irq() guard as the other pbuf calls in this file. A single pbuf_free() is used (not pbuffer_free_data(), which loops until the pbuf is gone) so that a reference lwIP may still hold, for example in the ARP queue, stays valid.
2. Constructor: initialise _port, _data, _remaining and the POD members of _udp. The constructor was empty, so an instance that is not zero-filled (created with new or on the stack) could start with a garbage _udp.pcb. begin() then returns 0 because it believes a socket is already open.

Both changes are in libraries/lwIpWrapper/src/lwipUDP.cpp. Each is its own commit.

Test

Portenta C33, core 1.6.0 (same commit as current main), Wi-Fi, no shield or other hardware attached. The sketch sends one ~55 byte line every 200 ms and prints its own counters. A PC on the same network counts the datagrams it receives.

#include <WiFiC3.h>
#include "arduino_secrets.h"   // SECRET_SSID, SECRET_PASS, SERVER_IP, SERVER_PORT
static WiFiUDP udp;
void setup() {
  Serial.begin(115200);
  while (WiFi.begin(SECRET_SSID, SECRET_PASS) != WL_CONNECTED) delay(1000);
  udp.begin(0);
}
void loop() {
  static uint32_t seq = 0, sent = 0, fail = 0, last = 0;
  char line[128];
  int n = snprintf(line, sizeof(line), "{\"type\":\"r\",\"seq\":%lu,\"t\":%lu,\"mac\":\"2222\",\"d\":100}\n",
                   (unsigned long)seq++, (unsigned long)millis());
  bool ok = udp.beginPacket(IPAddress(SERVER_IP), SERVER_PORT) == 1;
  ok = ok && udp.write((const uint8_t*)line, n) == (size_t)n;
  ok = ok && udp.endPacket() == 1;
  ok ? sent++ : fail++;
  if (millis() - last >= 5000) { last = millis(); Serial.print("seq="); Serial.print(seq);
    Serial.print(" sent="); Serial.print(sent); Serial.print(" fail="); Serial.println(fail); }
  delay(200);
}

| | Board counters | Received on the PC |
|---|---|---|
| Core 1.6.0, unmodified | sent stops at 111, every later packet fails | 111, then nothing |
| With the endPacket() change | 526 sent, 0 failed (stopped by hand after ~2 min) | 100% of the 449 produced while the receiver ran |

Serial output, unmodified core:

34.70 seq=101 sent=101 fail=0
39.79 seq=126 sent=111 fail=15
44.90 seq=151 sent=111 fail=40
54.63 seq=201 sent=111 fail=90

Serial output, with the change:

110.54 seq=476 sent=476 fail=0
115.63 seq=501 sent=501 fail=0
120.18 seq=526 sent=526 fail=0

Diff: 401 lines (+300 / -101) across 5 files Reviewer: facchinm Review duration: 1594m
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 401 lines diff across 5 files
0.45
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.359
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #560"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 44.7% (MEDIUM) · Higher = larger blast radius
44.7%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 17.7% · Safe to merge
17.7%