Repository navigation
Stop mDNS before Wi-Fi teardown in initConnection() (#5945) - #5965
Conversation
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes.
|
| Layer / File(s) | Summary |
|---|---|
Stop network services before Wi-Fi teardown wled00/wled.cpp |
On ESP32, WLED::initConnection() logs the shutdown and stops mDNS and, when enabled, ArduinoOTA before Wi-Fi teardown. |
Estimated code review effort: 2 (Simple) | ~8 minutes
Change: Bug fix
Possibly related issues
- random crashes (ESP32-C5) when changing from 2.4Ghz to 5Ghz Wi-Fi (same network name) #5945 — The issue reports an ESP32-C5 crash during a Wi-Fi band change and identifies mDNS
join_groupin the backtrace. This change stops mDNS before Wi-Fi teardown.
Possibly related PRs
- wled/WLED#5719 — The HomeWizard usermod uses mDNS for meter discovery, which may be affected while mDNS is stopped during Wi-Fi teardown.
Suggested labels: bug, AI
Suggested reviewers: dedehai
Merge Risk: 
Minimal · previous review f3191
-
ESP32 network services are stopped before Wi-Fi teardown. No actionable merge-blocking risk was established.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passedFull details: Docstring Coverage
-
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 1 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
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.
|
thanks, the change looks reasonable. Needs testing on a real -C5 (and on -S3 for regression tests) before merging. |
|
@coderabbitai are there other protocol handlers that should be stopped before Wi-Fi teardown, for example asyncWebserver? |
|
Tip For best results, initiate chat on the files or code changes. Analysis chain
The best candidate for further investigation is E1.31 multicast. Other services worth checking are the UDP receivers, MQTT, and the captive-portal DNS server. Before merging, I recommend the requested real ESP32-C5 and ESP32-S3 tests. Exercise repeated reconnects with active HTTP/WebSocket traffic and E1.31 multicast, then verify service recovery, including AP fallback. I have not run those tests. Learnings used
You are interacting with an AI system. |
Hello and thank you for taking a look at it, currently i do not have an ESP32-C5 or an s3 to test it on which is why i went for an emulator testing .so i was wondering if someone with an actual board could possibly test it ?ill update the pr according to the result. |
|
Tested this on an ESP32-S3 (N16R8, Stock
Not tested: whether mDNS still answers after the reconnect, and I can post the decoded backtraces if that helps. The test runs were AI-assisted. |
|
Thanks @Tycorc, this is really helpful. Summary: the #5945 mDNS crash is gone on both the S3 and the classic ESP32 with this PR. ESP32-S3: 3 mDNS crashes on main → 0 with this PR (10 resets, each followed by a forced reconnect). The classic ESP32 also showed a separate UDP crash (notifier2Udp.parsePacket() → pbuf_free). This PR doesn't touch it, and it appears on both images (1 in 39 on main, 3 in 40 with the PR). That sample is too small to say whether the PR changes how often it happens. I'll open a follow-up issue for it, and the decoded backtraces would be very welcome there. @softhack007, the S3 regression check you asked for is done. Still untested: A physical C5 Let me know if anything else is needed for this PR |
@coderabbitai can we address these crashes in a similar way, for example by adding |
|
Tip For best results, initiate chat on the files or code changes. Analysis chain
For an ESP32 experiment, immediately before #ifdef ARDUINO_ARCH_ESP32
udpConnected = false;
udp2Connected = false;
udpRgbConnected = false;
ntpConnected = false;
notifierUdp.stop();
notifier2Udp.stop();
rgbUdp.stop();
ntpUdp.stop();
#endifAll four flags matter. This is not yet a confirmed fix. The backtrace identifies where the crash occurs, not why the buffer becomes invalid. Closing a socket could also encounter the underlying problem. The restart paths need care:
I would repeat Tycorc’s forced-reconnect test with active UDP traffic and compare crash counts against the current PR. Check node broadcasts, UDP sync, RGB input and NTP after reconnect. The requested physical C5 test is still needed before merging; the S3 test should also be repeated for this additional change. mDNS response after reconnect and the enabled ArduinoOTA path remain untested. I have not run hardware tests. Learnings used
You are interacting with an AI system. |
@coderabbitai that might be another (long-existing) bug, please investigate. |
|
Tip For best results, initiate chat on the files or code changes. Analysis chain
The time-settings POST handler can call For example, if NTP was disabled and In the configured Tasmota ESP32 core, A correction to my earlier reply: I have not reproduced this race or established that it caused the reported Learnings used
You are interacting with an AI system. |
|
@softhack007 I had minimax analyze this as well. I am not familiar enough with the network code to judge it, but here is the full analysis. edit: this is the (probably a bit overboarding) patch it suggested, not sure if it builds on top of this PR or main as I did not check any of it. Could contain useful fixes or just a bunch of AI overly-cautios guards. |
@coderabbitai I'm still not sure if an explicit stop is necessary before wifi teardown and reconnecting to another router. Please review the related arduino-esp32, esp-idf and lwip framework code for a clearer answer. |
|
Tip For best results, initiate chat on the files or code changes. Analysis chain
The source gives a more specific picture:
There is still an important interval before The crash remains unexplained. A receive releases an lwIP packet buffer, and closing a socket also drains queued buffers. Therefore, moving I suggest comparing the current PR, flags-only, and a synchronized stop-before-teardown build under the same repeated reconnect and UDP traffic test. Check UDP recovery after a changed IP and during AP fallback. The decoded backtraces would help identify the failing buffer-release path. I have not run these tests. The requested physical C5 test remains outstanding; any added change should also be regression-tested on the S3. Learnings used
You are interacting with an AI system. |
|
@softhack007 a user report on discord was: works on ESP32, S3 was still crashing. Although it was not a fresh install on that device so could be some other legacy / config issue there. |
|
I think we should also stop all WiFiUDP receivers before the teardown, at least flag them as "unconnected" (and wait a few 100ms) so that all handlers stop processing - I'll do a quick test this week, then update this PR if needed. Edit: restarting all UDP handlers might also solve the AP mode problem reported in #5709, in case that one is not related to using the wrong broadcast address. @coderabbitai please double-check if this will work. Edit2:
|
|
Tip For best results, initiate chat on the files or code changes. Analysis chain
For I suggest comparing the current PR, a flags-only build, and a synchronized stop-and-restart build under repeated reconnects with UDP traffic. Check for crashes during both receive and socket close, then check UDP sync, RGB input, NTP, and AP fallback. Repeat the S3 regression test for any added change. The requested physical C5 test remains needed before merging; mDNS response after reconnect and enabled ArduinoOTA also remain untested. I have not run hardware tests. Learnings used
You are interacting with an AI system. |
|
@softhack007 update from said user: it works on S3 as well. IMHO we should get this merged and refine later. |
|
@DedeHai thanks, then let's merge 👍 |
|
Thanks @softhack007 for the review and for merging, and thanks @Tycorc and @DedeHai for the testing and the Discord confirmation. I'd be happy to help with other issues too. I work mostly in TypeScript, React and Node. I don't have ESP32 boards, so I'm best on software-only tasks (code review, small fixes, docs, or testing in an emulator). If there are any open issues that fit that, I'd appreciate a pointer. |
What it does
The issue was that when an ESP32-C5 switches the Wi-Fi band and reconnects, the board could crash, and a reboot fixes it. So what we did is stop mDNS before the Wi-Fi interface is destroyed. It is started again once the board has reconnected.
Additional references and context
How it works
We updated
initConnection()inwled00/wled.cpp. BeforeWiFi.disconnect(true)it callsMDNS.end(), only ifinterfacesInited && strlen(cmDNS) > 0, meaning mDNS is actually running. It is inside#ifdef ARDUINO_ARCH_ESP32, so ESP8266 is unchanged. No restart code was added, becauseinitInterfaces()already callsMDNS.end()andMDNS.begin()once the board is connected.AI assistance: yes
Testing and Validation
Emulator: first we tested the unchanged code to recreate the error. It panicked on cycle 2 of 10 (backtrace:
join_groupin mDNS). With the change, there were no errors in three runs of 10 cycles. The emulator has one band, so there was no real 5 GHz connection. A temporary test harness triggered the band change and is not part of this PR.Checklist
Summary by CodeRabbit