Skip to content

Single json buffer - #2336

Merged
Aircoookie merged 13 commits into
masterfrom
single-json-buffer
Dec 4, 2021
Merged

Aircoookie merged 13 commits into
masterfrom
single-json-buffer

Conversation

@Aircoookie

@Aircoookie Aircoookie commented Nov 11, 2021 •

Copy link
Copy Markdown
Member

JSON buffer is statically allocated, this should help reduce memory fragmentation and also offer a small performance benefit.
Need to check that this works if a network request comes in while something in the loop is using the buffer (IR/config/ledmap/preset cycle/serial/MQTT)

By @blazoncek

Comment thread wled00/cfg.cpp Resolved
Comment thread wled00/cfg.cpp Resolved
Comment thread wled00/json.cpp Resolved
@Aircoookie
Aircoookie marked this pull request as draft November 12, 2021 13:10
@blazoncek

Copy link
Copy Markdown
Contributor

Config and ledmap are safe. I can see problems on serial, MQTT or network access.

@blazoncek

Copy link
Copy Markdown
Contributor

I have identified potential lockup in:

  • onAlexaChange() : applyPreset() attempts to lock buffer
  • handle buttons : same as above
  • presetFallback() in IR : same as above
  • handleNightlight() : same as above
  • time macros : same as above
  • handlePlaylist() : same as above
  • handleSet() if fileDoc not assigned
  • applyMacro() but this looks like an unused function

Oddly though, ESP8266 never locked up.

@blazoncek

Copy link
Copy Markdown
Contributor

Got it!

Strip time[ms]: 0/1
IR recv: 0xFF30CF
IR JSON buffer requested.
API req: win&PL=2
Apply preset JSON buffer requested.

... and there was a lock-up.

@blazoncek

blazoncek commented Nov 15, 2021 •

Copy link
Copy Markdown
Contributor

Recent commits are without locking up ESP. Looks promising.
I will update every locking call with a cleaner code though.

Edit: With multiple (>2) simultaneous requests to ESP8266 I can see occasional reboot.
But then I can see those on non-single-JSON ESPs too. :)

Dadded debugging information.
@blazoncek

Copy link
Copy Markdown
Contributor

A lot less reboots on ESP8266 since latest push. 😀

@blazoncek
blazoncek marked this pull request as ready for review November 16, 2021 21:03
@blazoncek

Copy link
Copy Markdown
Contributor

Upadte: ESP8266 (and ESP32) running smooth for 2 days now. No issues whatsoever.

Aircoookie added a commit that referenced this pull request Nov 28, 2021
(to be implemented in #2336 )
Comment thread wled00/ir.cpp Resolved
Comment thread wled00/util.cpp
{
unsigned long now = millis();

while (jsonBufferLock && millis()-now < 1000) delay(1); // wait for a second for buffer lock

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I thought to myself that this can't possibly work, because delay() in ESP8266 network callbacks will crash and burn.

Turns out that this does in fact work, because ESP8266 and 32 differ in behavior:
ESP8266 would be stuck for a second and not reply to the request in the best case and crash in the worst case if it encountered a locked buffer in a network callback. Luckily this should never happen as the ESP8266 can only handle network callbacks wherever yield() is placed. As such, as long as we make sure we never call yield() while the buffer is locked, we are 100% fine. In fact, buffer lock logic is not required at all on ESP8266 since there is simply no way for two interactions that both need the buffer to intersect.

On ESP32, the buffer lock is needed and works as intended! Due to being dual core, network callbacks are run on core 1 while the main loop is run on core 0. If core 1 handles a network request, it will wait out the lock, as (unlike with 8266) the loop task will continue to run, thus only blocking for a few milliseconds at most.

I'm interested in how ESP32-S2 would behave here, given that it is single core. I would assume same as ESP8266, but it might also be running two tasks on the same core (and rapidly switching between them).
In both cases, it should work fine.

I think this is ready for merge and public testing as part of 0.13.0-b6 :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Never thought about implications of net callbacks since I was unaware of the limitations.
Still, this code is now running bug free for a month on a ESP01, D1 mini and D1 min32.
I went so far as to install it on all of my production units. Not a single one of them has crashed since!

@Aircoookie
Aircoookie merged commit 66bad2b into master Dec 4, 2021
@Aircoookie
Aircoookie deleted the single-json-buffer branch December 4, 2021 00:05
aenertia pushed a commit to aenertia/WLED that referenced this pull request Aug 18, 2026
(to be implemented in wled#2336 )
aenertia pushed a commit to aenertia/WLED that referenced this pull request Aug 18, 2026
* Single/static JSON buffer for all requests.

* Missing json.cpp changes.

* Async fix.

* Added conditional compile (WLED_USE_DYNAMIC_JSON).

* Advanced locking with time-out.

* Missing releaseJSONBufferLock() on error response.

* Fix for config saving.

* Fixes and optimisations.
Dadded debugging information.

* Fix for ledmaps.

* No unsolicited serial sending if GPIO1 allocated

* Stray semicolons

* Fix JSON ledmap

Co-authored-by: Blaz Kristan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants