Skip to content

fix(network): stream WiFi API responses to prevent heap exhaustion - #10

Closed
meimeiesther wants to merge 23 commits into
aBER0724:masterfrom
meimeiesther:fix/wifi-heap-panic
Closed

fix(network): stream WiFi API responses to prevent heap exhaustion#10
meimeiesther wants to merge 23 commits into
aBER0724:masterfrom
meimeiesther:fix/wifi-heap-panic

Conversation

@meimeiesther

Copy link
Copy Markdown

Summary

Stream WiFi API responses to prevent heap exhaustion on low-memory devices.

Problem

Under the prior implementation, handleWifiScan and handleWifiList built their full JSON response strings using repeated String += concatenation on the heap. On the ESP32-C3, this can exhaust contiguous heap space needed by the WiFi driver's TX queue, triggering an abort() in lmacRetryTxFrame.

Root cause was confirmed via addr2line decoding of a crash report stack trace.

Changes

src/network/CrossPointWebServer.cpp

  • handleWifiScan: stream each network entry individually using sendContent instead of concatenating into a single String; increase buf from 256 → 320 bytes; add overflow guard to skip malformed entries
  • handleWifiList: stream each credential individually; use appendEscapedJsonString into a stack buffer; increase escapedSsid from 128 → 256 bytes to safely cover worst-case SSID escaping

Testing

Verified fix resolves the crash on device by reproducing the heap-exhaustion condition (scanning in a WiFi-dense environment under reader load) and confirming clean operation post-patch.

aBER0724 and others added 23 commits June 3, 2026 17:54
Integrates the SD-card firmware update flow originally contributed by @donutboyy
  in aBER0724#3, with conflicts resolved against current `master`.

  Includes follow-up fixes for SDK pointer preservation, dark redrive handling,
  PlatformIO/CJK font generation settings, OTA installer stack usage, render-path
  string allocation, formatting, and cppcheck.

  Verified with:
  - `pio run`
  - `bin/clang-format-fix`
  - `pio check --fail-on-defect low --fail-on-defect medium --fail-on-defect high`

  Credit to @donutboyy for the original SD firmware update contribution.
…update

fix: Fix OTA update memory pressure
- EpdFontData.h: add glyphMissHandler + glyphMissCtx for SD font on-demand loading
- EpdFontFamily.h: new STRIKETHROUGH/SUP/SUB style bits (bitmask-compatible)
- EpdFont.cpp: getGlyph falls back to glyphMissHandler when codepoint not in interval table
- EpdFont.h: minor upstream alignment
- FontDecompressor.h/.cpp: ligature prewarming
- SdCardFont.h/.cpp: core SD card font data types (compiles standalone)
- Retained: fork all.h (OpenDyslexic includes)

Deferred: SdCardFontManager/Registry (need GfxRenderer API changes)

Build: SUCCESS, RAM: 31.9%, Flash: 87.8%
- GfxRenderer.h: upstream SdCardFont/BiDi/strip/region cache APIs + fork dark mode fields
- FontCacheManager: upstream constructor with SdCardFonts param
- Bitmap.h/cpp: upstream HalFile migration
- GfxRenderer.cpp: updated signatures to match header (BidiBaseDir default param)

Build: SUCCESS, RAM: 31.9%, Flash: 88.0%
Replace dynamic String concatenation in handleWifiScan and handleWifiList with incremental chunked HTTP streaming (sendContent) to avoid heap fragmentation under low-memory conditions.

Under the prior code, building a full JSON response string via String += on the heap could exhaust contiguous heap space needed by the WiFi driver's TX queue, triggering an abort() in lmacRetryTxFrame.

Changes:
- handleWifiScan: stream each network entry individually; increase buf from 256 to 320 bytes; add overflow guard to skip malformed entries
- handleWifiList: stream each credential individually; use appendEscapedJsonString into a stack buffer; increase escapedSsid from 128 to 256 bytes to safely cover worst-case SSID escaping

Root cause confirmed via addr2line decoding of crash report stack trace.
@aBER0724
aBER0724 force-pushed the master branch 3 times, most recently from a7193f3 to ae32abf Compare September 2, 2026 09:02
@aBER0724

aBER0724 commented Sep 2, 2026

Copy link
Copy Markdown
Owner

Integrated the focused Wi-Fi response streaming fix locally on current master, then strengthened it to use watchdog-safe nonblocking chunk writes and corrected overflow handling so skipped entries cannot produce malformed JSON. Added a focused regression test.

Verification completed before closing:

  • Wi-Fi API streaming regression test passed
  • WebServer low-memory/watchdog regression test passed
  • cppcheck passed with low/medium/high defects enabled
  • default and sticky firmware builds succeeded

Integrated by master commits 23f0cf2 and e45d82b. The remaining historical changes on this branch are stale or already superseded, so this PR is now closed as integrated/superseded.

@aBER0724 aBER0724 closed this Sep 2, 2026
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