Skip to content

Keep the configuration JSON generator off the ESP8266 stack - #1344

Merged
gskjold merged 1 commit into
mainfrom
fix/esp8266-config-json-stack
Dec 4, 2026
Merged

gskjold merged 1 commit into
mainfrom
fix/esp8266-config-json-stack

Conversation

@gskjold

@gskjold gskjold commented Dec 3, 2026

Copy link
Copy Markdown
Member

Why

On 2.6.0, opening the configuration page on an ESP8266 reader (reported on a Pow-K) reboots the device. The reset reason varies between WDT reset, soft WDT reset and software restart. 2.5.7 is stable on the same device, and ESP32 readers are not affected.

The configuration JSON moved into AmsJsonGenerator::generateConfigurationJson() (#1169). That function keeps its 768-byte scratch buffer and all 15 config structs in a single stack frame. The ESP8266 has a 4 kB stack for the whole loop() call chain. Add handleClient(), the request handler, snprintf_P (with float formatting) and sendContent(), and the stack overflows. The memory next to it gets corrupted, which explains why the reset reason changes from one reboot to the next.

Stack frame sizes from -fstack-usage on the esp8266 env:

Frame
2.5.7 AmsWebServer::configurationJson() 1904 B
2.6.0 generateConfigurationJson(…, JsonSink&) 2816 B
this PR 1296 B (esp32: 1328 B)

Changes

  • The scratch buffer is allocated on the heap with malloc() for the duration of the call. If the allocation fails, the generator returns without emitting anything instead of crashing.
  • Each section reads only the config structs it needs, inside its own block, so the structs are no longer all on the stack at the same time. The GPIO section reads MeterConfig again for the HAN pins.

The JSON sections, their order and their arguments are unchanged.

Verification

  • pio run -e esp8266 -e esp32 builds.
  • Ran -fstack-usage before and after (numbers above).
  • No test covers this JSON, so I extracted the ordered EMIT(...) calls from 2.6.0 and from this branch and compared them: all 18 are identical.
  • Not yet tested on hardware. Opening the configuration page on an ESP8266 device should be checked before release.

Refs #1342

🤖 Generated with Claude Code

Opening the configuration page on an ESP8266 reader rebooted it on 2.6.0
with a varying reset reason (WDT, soft WDT, software restart). The
generator that replaced configurationJson() held its 768-byte scratch
buffer and every config struct in one 2816-byte frame, against a 4 kB
stack for the whole loop; 2.5.7's handler used 1904 bytes.

Allocate the scratch buffer on the heap and read each section's config
structs in their own block. The frame is now 1296 bytes on ESP8266.
The emitted JSON is unchanged.

Refs #1342

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: cb92ef5

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

@gskjold
gskjold merged commit 5ffd8d0 into main Dec 4, 2026
8 checks passed
@gskjold
gskjold deleted the fix/esp8266-config-json-stack branch December 4, 2026 09:09
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.

1 participant