Skip to content

fix(stl/net): OOM + concurrency hardening from the #3588 crash hunt - #3589

Merged
zackees merged 1 commit into
masterfrom
fix/net-stack-hardening-3588
Jul 2, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/net-stack-hardening-3588

Conversation

@zackees

@zackees zackees commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Hardening batch from root-causing the classic-ESP32 crash under WiFi + HTTP server + client traffic (#3588). The exact corrupter is still open (full forensic record on the issue); every change here is independently correct and verified.

Library fixes

  • StringHolder OOM: alloc failure → coherent empty state instead of UB (the code carried a literal "OOM is terminal" assumption; under WiFi+server classic ESP32 drops below 20 KB free and fl::malloc genuinely returns null; grow() also leaked the old buffer and left a null mData with stale capacity).
  • make_shared family null-safety: embedded new returns null with -fno-exceptions; the factories placement-constructed at an offset from null. Now return an empty shared_ptr.
  • SlabAllocator thread safety: the static per-type slab behind fl::map/set/multi_map/multi_set is shared across FreeRTOS tasks (esp_http_server builds Request/Response header maps on its own task) with zero synchronization. New fl::detail::slab_mutex (FreeRTOS mutex on ESP32 / atomic spinlock on host / no-op on single-threaded MCUs) guards allocate/deallocate/cleanup. The full fl/stl/mutex.h chain can't be used at the allocator layer (include cycle).
  • asio http Server (ESP32): httpd task stack 4 KB (IDF default) → 8 KB; route handlers execute ON that task running full fl:: machinery.

Bench fixes (AutoResearch)

Regression net

  • tests/fl/stl/json_cross_thread.cpp: cross-thread fl::json + map/string stress mirroring the device workload. ASAN-clean on host — the remaining corruption needs ESP32-specific machinery (documented on the issue).

Host: 347/347; lint clean; esp32dev/esp32c6 compile clean. C6 traffic loop now survives; the WROOM crash under concurrent traffic remains open in #3588.

Refs #3588, #3576.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added a new remote health check that reports heap integrity and memory/stack statistics on supported devices.
  • Bug Fixes

    • Improved HTTP server stability by increasing task stack size and avoiding fragile JSON construction in request handlers.
    • Hardened string and shared-memory utilities to handle allocation failures safely.
    • Added thread-safe locking for slab allocations to reduce cross-task corruption issues.
  • Tests

    • Added a cross-thread stress test for JSON, string, and map usage.

Hardening batch from root-causing the classic-ESP32 crash under
WiFi + HTTP-server + client traffic (#3588). The exact corrupter is
still open (see issue for the full forensic record); every change here
is independently correct and bench/host verified.

- StringHolder: allocation failure now degrades to a coherent empty
  state instead of UB (writes through null; realloc leaked the old
  buffer and left null mData with a stale capacity). WiFi + server
  pushes classic ESP32 under 20 KB free where fl::malloc genuinely
  returns null.
- make_shared / make_shared_with_deleter / make_shared_array: null
  control block or object (embedded new returns null with
  -fno-exceptions) now returns an empty shared_ptr instead of placement-
  constructing at an offset from null.
- SlabAllocator (backs fl::map/set/multi_map/multi_set via
  allocator_slab): the static per-type instance is shared across
  FreeRTOS tasks (e.g. esp_http_server building Request/Response header
  maps while the main loop mutates its own) with zero synchronization.
  Added fl::detail::slab_mutex (FreeRTOS mutex on ESP32, atomic spinlock
  on host, no-op on single-threaded MCUs — the full fl/stl/mutex.h chain
  can't be used at the allocator layer without an include cycle) and
  guarded allocate/deallocate/cleanup.
- asio http Server (ESP32): raise the esp_http_server task stack from
  the 4 KB IDF default to 8 KB — route handlers execute ON that task
  and run full fl::json/string/Response machinery.
- AutoResearchNet: HTTP client timeout 5 s → 2 s + watchdog feed
  between blocking esp_http_client_perform() calls (single-core
  ESP32-C6 hit a tick-hook WDT panic_abort mid-test); server route
  handlers now format responses with fl::snprintf instead of building
  fl::json on the httpd task (defensive pending the #3588 root cause).
- tests/fl/stl/json_cross_thread.cpp: cross-thread fl::json + map/string
  stress mirroring the device workload (ASAN-clean on host — the
  remaining corruption needs ESP32-specific machinery).

Bench: C6 no longer WDT-panics during runNetClientTest; WROOM crash
under concurrent traffic persists and stays open in #3588.
Host: 347/347, lint clean.

Co-Authored-By: Claude Fable 5 <[email protected]>
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Jul 2, 2026
@zackees
zackees merged commit 166f72a into master Jul 2, 2026
11 checks passed
@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 324fc659-7bfd-4d3c-8b1c-bd7489a28a2f

📥 Commits

Reviewing files that changed from the base of the PR and between dd81265 and b8eaa2f.

📒 Files selected for processing (8)
  • examples/AutoResearch/AutoResearchNet.cpp
  • examples/AutoResearch/AutoResearchRemoteSystemMethods.cpp
  • src/fl/stl/allocator.h
  • src/fl/stl/asio/http/server.cpp.hpp
  • src/fl/stl/detail/slab_mutex.h
  • src/fl/stl/detail/string_holder.cpp.hpp
  • src/fl/stl/shared_ptr.h
  • tests/fl/stl/json_cross_thread.cpp

📝 Walkthrough

Walkthrough

Changes harden ESP32 concurrency and memory-safety paths: AutoResearch examples avoid fl::json in HTTP handlers, reduce client timeout, add watchdog feeds, and expose a heapCheck RPC; a new slab_mutex synchronizes SlabAllocator; HTTPD stack size is increased; StringHolder and shared_ptr factories gain null-allocation handling; a cross-thread JSON test is added.

Changes

AutoResearch Example Hardening

Layer / File(s) Summary
HTTP status/leds handlers avoid fl::json, client timeout, watchdog
examples/AutoResearch/AutoResearchNet.cpp
/status and /leds handlers build JSON via snprintf/static strings instead of fl::json, client timeout reduced to 2000ms, and FastLED.watchdog().feed() added between blocking HTTP calls.
heapCheck RPC method
examples/AutoResearch/AutoResearchRemoteSystemMethods.cpp
ESP32 headers wrapped in extern "C" guards; new heapCheck RPC returns heap integrity and heap/stack metrics.

STL Synchronization and Allocation Safety

Layer / File(s) Summary
slab_mutex primitive
src/fl/stl/detail/slab_mutex.h
New header defines platform-specific slab_mutex (FreeRTOS semaphore, atomic spinlock, no-op) and slab_lock_guard RAII wrapper.
SlabAllocator synchronization
src/fl/stl/allocator.h
SlabAllocator gains an mMutex member and acquires slab_lock_guard in allocate, deallocate, and cleanup.
HTTPD stack size increase
src/fl/stl/asio/http/server.cpp.hpp
ESP32 HTTPD config now sets stack_size to 8192 to avoid heap corruption during route handling.
StringHolder allocation-failure handling
src/fl/stl/detail/string_holder.cpp.hpp
Both constructors check for null malloc results and set a coherent empty state; grow() uses a temporary pointer to safely handle realloc failure.
shared_ptr factory allocation-failure handling
src/fl/stl/shared_ptr.h
make_shared, make_shared_with_deleter, and make_shared_array now return an empty shared_ptr and clean up partial allocations on null results.
Cross-thread JSON stress test
tests/fl/stl/json_cross_thread.cpp
New test spawns responder/handler threads exercising fl::json/fl::string/fl::map concurrently and validates iteration counters.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
    participant Thread as Responder Thread
    participant Handler1 as Handler Thread 1
    participant Handler2 as Handler Thread 2
    participant Json as fl::json/fl::map
    participant Main as Test Main Loop

    Main->>Thread: start responder loop
    Main->>Handler1: start handler loop
    Main->>Handler2: start handler loop
    loop until counters reach 2000
        Thread->>Json: build & serialize RPC response
        Handler1->>Json: build headers/query map, serialize body
        Handler2->>Json: build headers/query map, serialize body
        Thread-->>Main: increment iteration counter
        Handler1-->>Main: increment iteration counter
        Handler2-->>Main: increment iteration counter
    end
    Main->>Thread: signal stop
    Main->>Handler1: signal stop
    Main->>Handler2: signal stop
    Main->>Main: join threads and check counters
Loading
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/net-stack-hardening-3588

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant