Skip to content

esp32: UDP SYNC (notifier) sends second packet with random data when MAX_NUM_SEGMENTS > 39 #5951

In progress

Description

@softhack007

esp32 boards with PSRAM have MAX_NUM_SEGMENTS=64, while normal esp32 builds use MAX_NUM_SEGMENTS=32, and esp8266 uses MAX_NUM_SEGMENTS=16.

The UDP notifier (WLED SYNC) does not work as expected in the 64 segment configuration. It always sends WLEDPACKETSIZE (2345 bytes for all 64 segments) notification data, for example when changing brightness or activating a preset.

Unfortunately, the arduino-esp32 core automatically splits the UDP datagram at byte 1460, and the notification receiver cannot re-assemble the split message. The second datagram usually contains random junk (uninitialized memory). Depending on pure coincidence, handleNotifications() will either ignore the data, parse it as a second change notification that supersedes the first, treat it as hyperion / raw RGB, or handle it as a HTTP or JSON API request.

The problem occurs whenever MAX_NUM_SEGMENTS > 39, and was confirmed during testing in PR #5932.

PR #5932 will provide a partial fix by ensuring that the second packet only contains valid data, and the content will be ignored unless users have more than 39 active segments. However, a full solution that properly syncs all 64 segments is not available yet.


static constexpr size_t WLEDPACKETSIZE = 41+(WS2812FX::getMaxSegments()*UDP_SEG_SIZE); // make sure this is known at compile-time

byte udpOut[WLEDPACKETSIZE]; //TODO: optimize size to use only active segments

WLED/wled00/udp.cpp

Lines 196 to 199 in fce41f2

IPAddress broadcastIp = ~uint32_t(WLEDNetwork.subnetMask()) | uint32_t(WLEDNetwork.gatewayIP());
notifierUdp.beginPacket(broadcastIp, udpPort);
notifierUdp.write(udpOut, WLEDPACKETSIZE); // TODO: add actual used buffer size
notifierUdp.endPacket();

File NetworkUDP.cpp, NetworkUDP::write(), line ~277

size_t NetworkUDP::write(uint8_t data) {
  if (tx_buffer_len == 1460) {
    endPacket();
    tx_buffer_len = 0;
  }
  tx_buffer[tx_buffer_len++] = data;
  return 1;
}

size_t NetworkUDP::write(const uint8_t *buffer, size_t size) {
  size_t i;
  for (i = 0; i < size; i++) {
    write(buffer[i]);
  }
  return i;
}

the split at 1460 is hard-coded inside arduino-esp32 !!

Activity

  1. added
    confirmed
    on Nov 3, 2026
  2. changed the title [-]esp32: UDP SYNC (notifer) sends second packet with random data when MAX_NUM_SEGMENTS > 39[/-] [+]esp32: UDP SYNC (notifier) sends second packet with random data when MAX_NUM_SEGMENTS > 39[/+] on Nov 3, 2026
  3. softhack007 commented on Nov 3, 2026

    @softhack007
    MemberAuthor

    It seems that AsyncUDP has a similar problem - see espressif/arduino-esp32#12162

  4. softhack007 commented on Nov 3, 2026

    @softhack007
    MemberAuthor

    @coderabbitai please scan the current source code for other locations where UDP messages of > 1460 bytes can be created and might get spit by the framework UDP class (WiFiUDP, maybe AsyncUDP too). Provide a short answer, listing the WLED features that might be affected by the problem.

  5. coderabbitai commented on Nov 3, 2026

    @coderabbitai
    Contributor
    Analysis chain

    Script executed:

    #!/bin/bash
    rg -n -C2 --glob '*.{cpp,h,hpp,c,ini}' '\b(WiFiUDP|AsyncUDP|NetworkUDP|beginPacket|beginPacketMulticast|endPacket|writeTo|broadcastTo|sendTo)\b' .

    Length of output: 10089


    Script executed:

    #!/bin/bash
    gh issue view 5951 --repo wled/WLED --json title,body,comments
    gh issue view 12162 --repo espressif/arduino-esp32 --json title,body,comments

    Length of output: 9819


    Script executed:

    #!/bin/bash
    ast-grep outline wled00/udp.cpp
    ast-grep outline wled00/net_debug.cpp
    ast-grep outline wled00/src/dependencies/e131/ESPAsyncE131.cpp
    ast-grep outline wled00/src/dependencies/espalexa/Espalexa.h --match 'respond|Udp|UDP|SSDP'
    ast-grep outline usermods/udp_name_sync/udp_name_sync.cpp
    ast-grep outline usermods/audioreactive/audio_reactive.cpp --match 'transmit|Transmit|UDP|Sync|sync'
    sed -n '1,60p' wled00/udp.cpp
    sed -n '700,900p' wled00/udp.cpp
    cat -n wled00/net_debug.cpp
    rg -n -C3 --glob '*.{cpp,h,hpp,c,ino}' '\b(sendto|sendmsg|beginMulticastPacket)\s*\(|\b(udp|Udp|UDP)\s*\.\s*(write|print|printf|send|broadcast|connect)\s*\(' .
    rg -n -C2 '\b(DDP_MAX_DATALEN|WLEDPACKETSIZE|MAX_NUM_SEGMENTS|NTP_PACKET_SIZE)\b' wled00/udp.cpp wled00/ntp.cpp wled00/const.h wled00/src/dependencies/e131

    Length of output: 19369


    Script executed:

    #!/bin/bash
    sed -n '1,180p' wled00/src/dependencies/e131/ESPAsyncE131.cpp
    sed -n '285,325p' wled00/src/dependencies/espalexa/Espalexa.h
    sed -n '1,85p' usermods/udp_name_sync/udp_name_sync.cpp
    sed -n '1190,1235p' usermods/audioreactive/audio_reactive.cpp
    rg -n -C3 '\b(DDP_CHANNELS_PER_PACKET|ArtPollReply|audioSyncPacket|transmitData)\b' wled00/src/dependencies/e131/ESPAsyncE131.h wled00/e131.cpp usermods/audioreactive
    rg -n -C3 '\bNetDebug\b|\bDEBUG_(PRINT|PRINTLN|PRINTF|PRINTF_P)\s*\([^;\n]*(json|Json|buffer|Buffer|buf|payload|Payload|data|Data)' --glob '*.{cpp,h,ino}' .
    rg -n -C2 --iglob '*.{cpp,h,hpp,c,ino}' '(AsyncUDP|WiFiUdp|WiFiUDP|NetworkUDP|UDPContext|UdpContext|udp_send|udp_sendto|\.sendTo\s*\(|\.broadcast\s*\(|\.broadcastTo\s*\(|\.writeTo\s*\(|\.reply\s*\()' .
    git rev-parse --short HEAD

    Length of output: 59049


    @softhack007 I scanned the current source (fce41f26), including usermods.

    • WLED Sync: The known case. notify() sends 41 + 36 × maxSegments bytes. This exceeds 1460 bytes at 40 segments and reaches 2345 bytes at 64 segments (wled00/udp.cpp, Line 198).
    • UDP network debug (WLED_DEBUG_HOST): One additional potential case. NetworkDebugPrinter::write() passes the full buffer to WiFiUDP without a size limit (wled00/net_debug.cpp, Line 42). Long debug messages, such as logged MQTT payloads or HTTP Pull Light Control JSON responses, can exceed 1460 bytes.

    No other oversized transmit path found. DDP output uses at most 1450 bytes, including its header. Art-Net output uses at most 530 bytes. Audio sync, NTP, discovery, Alexa discovery, and segment-name sync use smaller packets.

    The current source uses AsyncUDP only for reception. The linked upstream issue describes transmit truncation, not splitting, so I found no current WLED transmit path exposed to that AsyncUDP problem.

    This was source inspection only, not a device test.


    Learnings used
    Learnt from: softhack007
    Repo: wled/WLED PR: 0
    File: :0-0
    Timestamp: 2026-10-03T14:59:43.661Z
    Learning: In wled/WLED, softhack007 prefers contributors to answer maintainer questions directly with short explanations in their own words, even if the explanations are technically imprecise or the English is imperfect. Contributors should state uncertainty and actual test results rather than relay unverified AI-generated technical answers. AI may assist with translation or wording, but should not manufacture explanations that shift fact-checking and AI-session supervision onto maintainers.
    

    You are interacting with an AI system.

  6. softhack007 commented on Nov 3, 2026

    @softhack007
    MemberAuthor

    @coderabbitai please check if we have other cases in WLED where a buffer with partly uninitialized data can be sent by UDP.

  7. coderabbitai commented on Nov 3, 2026

    @coderabbitai
    Contributor
    Analysis chain

    Script executed:

    #!/bin/bash
    git rev-parse --short HEAD
    rg -n -C3 --glob '*.{cpp,h,hpp,c,ino}' '\b(beginPacket|beginPacketMulticast|endPacket|writeTo|broadcastTo|sendTo|sendto|sendmsg)\s*\(' .
    gh issue view 5951 --repo wled/WLED --json title,body
    ast-grep outline wled00/udp.cpp
    ast-grep outline wled00/net_debug.cpp
    ast-grep outline wled00/ntp.cpp
    ast-grep outline wled00/e131.cpp
    ast-grep outline usermods/udp_name_sync/udp_name_sync.cpp
    ast-grep outline usermods/audioreactive/audio_reactive.cpp --match 'transmit|Transmit|sync|Sync'

    Length of output: 13729


    Script executed:

    #!/bin/bash
    sed -n '1,205p' wled00/udp.cpp
    sed -n '697,885p' wled00/udp.cpp
    sed -n '386,590p' wled00/e131.cpp
    sed -n '250,277p' wled00/ntp.cpp
    cat -n wled00/net_debug.cpp
    cat -n usermods/udp_name_sync/udp_name_sync.cpp
    ast-grep outline wled00/src/dependencies/espalexa/Espalexa.h --match 'respond|Udp|SSDP'
    sed -n '287,325p' wled00/src/dependencies/espalexa/Espalexa.h
    sed -n '1170,1235p' usermods/audioreactive/audio_reactive.cpp
    rg -n -C4 --glob '*.{cpp,h,hpp,c,ino}' '\b(audioSyncPacket|transmitData|ArtPollReply|NetDebug)\b' .
    rg -n -C3 --glob '*.{cpp,h,hpp,c,ino}' '(AsyncUDP|WiFiUDP|WiFiUdp|NetworkUDP|udp_send|\.writeTo\s*\(|\.broadcast\s*\(|\.broadcastTo\s*\(|\.sendTo\s*\(|\.reply\s*\()' .

    Length of output: 53451


    Script executed:

    #!/bin/bash
    ast-grep outline wled00/src/dependencies/e131/ESPAsyncE131.h
    sed -n '145,220p' wled00/src/dependencies/e131/ESPAsyncE131.h
    rg -n -C5 --glob '*.{cpp,h,hpp,c,ino}' '\brealtimeBroadcast\s*\(|\b(DEBUGOUT|NetDebug)\s*\.\s*write\s*\(' .
    ast-grep outline wled00/bus_manager.h --match 'BusNetwork|setPixelColor|show|Bus'
    rg -n -C6 'class BusNetwork|realtimeBroadcast|_data\s*=|calloc|malloc' wled00/bus_manager.h
    rg -n -C3 --glob '*.{cpp,h,hpp,c,ino}' '\b(serverDescription)\b' wled00/wled.h wled00/cfg.cpp wled00/set.cpp
    ast-grep outline wled00/src/dependencies/e131/ESPAsyncE131.cpp
    sed -n '20,175p' wled00/src/dependencies/e131/ESPAsyncE131.cpp
    rg -n -C2 --glob '*.{cpp,h,hpp,c,ino}' '\b(udp|UDP|fftUdp|notifierUdp|notifier2Udp|rgbUdp|ntpUdp|espalexaUdp|debugUdp)\s*(\.|->)\s*(write|print|printf|send|broadcast|reply)\s*\(' .

    Length of output: 15878


    Script executed:

    #!/bin/bash
    ast-grep outline wled00/bus_manager.cpp --match 'BusNetwork'
    sed -n '695,810p' wled00/bus_manager.cpp
    nl -ba wled00/e131.cpp | sed -n '485,505p'
    nl -ba wled00/e131.cpp | sed -n '570,587p'

    Length of output: 6441


    @softhack007 I checked the UDP transmit paths at fce41f26, including usermods. I found one additional case: Art-Net discovery replies (ArtPollReply).

    • wled00/e131.cpp, Line 387 declares ArtPollReply artnetPollReply; without initialization.
    • Line 492 and Line 493 use strlcpy() for reply_short_name and reply_long_name. Bytes after the string terminators remain uninitialized.
    • Line 576 formats reply_node_report but also leaves its unused bytes uninitialized.
    • Line 585 sends the complete 239-byte packet, including these uninitialized bytes. This can expose stack contents to the requester. It does not require packet splitting.

    Clear the complete object before filling its fields. For example, at the start of prepareArtnetPollReply():

    memset(reply, 0, sizeof(*reply));

    I found no additional cases in the other inspected transmit paths. NTP and audio sync clear their buffers. Segment-name sync and Alexa discovery send only initialized string data. DDP and Art-Net pixel output use a zero-initialized pixel buffer.

    The known WLED Sync case also sends uninitialized bytes when fewer than the maximum segments are active, even when the packet stays below 1460 bytes.

    This was source inspection only, not a device test. I followed your supplied guidance to distinguish inspected findings from test results.


    Learnings used
    Learnt from: softhack007
    Repo: wled/WLED PR: 0
    File: :0-0
    Timestamp: 2026-10-03T14:59:43.661Z
    Learning: In wled/WLED, softhack007 prefers contributors to answer maintainer questions directly with short explanations in their own words, even if the explanations are technically imprecise or the English is imperfect. Contributors should state uncertainty and actual test results rather than relay unverified AI-generated technical answers. AI may assist with translation or wording, but should not manufacture explanations that shift fact-checking and AI-session supervision onto maintainers.
    

    You are interacting with an AI system.

  8. added this to the 16.1 milestone on Nov 30, 2026
  9. softhack007 commented on Dec 1, 2026

    @softhack007
    MemberAuthor

    bugfix is in #5932, un-linking this bug report from 16.1 to remove a duplicate toDo entry.

  10. removed this from the 16.1 milestone on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugconfirmed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions