Fix crash when saving 2D config with panel size larger than configured LED count #4973
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Enabling 2D mode and setting a panel size larger than the number of configured LEDs causes the ESP to crash with heap corruption. The crash only occurs when LEDs are ON (not OFF).
From the crash logs and debug output, the issue manifests as:
Root Cause
A race condition in the 2D configuration save process:
When saving 2D config in
set.cpp
, the following sequence occurs:strip.deserializeMap()
is called, which callssetUpMatrix()
setUpMatrix()
suspends the service loop, updatesSegment::maxWidth
andSegment::maxHeight
to the new panel dimensions, then immediately callsresume()
strip.makeAutoSegments(true)
recreates segments with correct dimensionsSolution
Defer
resume()
until after segments are recreated to ensure the service loop never runs with inconsistent state:In
wled00/FX_2Dfcn.cpp
:resume()
fromsetUpMatrix()
success pathresume()
in error path whereresetSegments()
creates valid stateIn
wled00/set.cpp
:resume()
after bothdeserializeMap()
andmakeAutoSegments()
completeIn
wled00/wled.cpp
:resume()
after segment operations in initialization pathsThis ensures the strip service loop remains suspended during the entire critical section where matrix dimensions and segments are being updated.
Testing
Impact
This is a minimal surgical fix that:
Fixes the crash described in the issue where enabling 2D with larger panel size than LED count would cause heap corruption and device reboot.
Co-authored-by: @DedeHai
Original prompt
This section details on the original issue you should resolve
<issue_title>Saving 2D config with higher LED count than configured leads to crash</issue_title>
<issue_description>### What happened?
I think this was discussed on Discord. When enabling 2D before setting the right amount of LEDs in config WLED just crashes.
I did some investigation on this but was not able to find where exactly this happes
To Reproduce Bug
Enable 2D and set the panel size larger than the number of configured LEDs.
Expected Behavior
Save the 2D setting without crashing.
Install Method
Self-Compiled
What version of WLED?
0.16
Which microcontroller/board are you seeing the problem on?
ESP32-C3
Relevant log/trace output
Anything else?
No response
Code of Conduct
<agent_instructions>#4808 does NOT fix this.
The crash does not happen if leds are turned off.
Very rarely, it does get to the point where it saves. If then changing back to 1D, it still crashes and does not save. Increasing _pixels[] buffer (4x the amount of LEDs so more than large enough) does not help at all so its not that.</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@blazoncek As you marked it confirmed, can you provide a crash dump? @DedeHai sure. Apologies, I wanted to open this before I forget :)