Skip to content

Hold the ESP accept path's sessions in fixed storage - #246

Merged
kahrendt merged 2 commits into
mainfrom
esp-accept-bounded
Nov 4, 2026
Merged

kahrendt merged 2 commits into
mainfrom
esp-accept-bounded

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Another allocation follow-ups (Sendspin/backlog#92): the ESP accept path. Every accepted httpd session ran make_shared<SendspinServerConnection> (about 1.2 KB, the largest per-connection block), a new std::shared_ptr session slot, and a push_back into a std::vector of pending upgrades, all through ESPHome's internal-RAM operator new, which aborts on failure. ConnectionManager::closing_ was the one std::vector among the manager's bounded tables.

Change

  • Pending upgrades: SendspinWsServer::pending_ is an InlineVector of MAX_SESSIONS (7, httpd's default max_open_sockets), and stop()/tick() release entries one at a time instead of through temporary vectors. server_max_connections is clamped to 7 on ESP with a warning, like the existing stack clamps.
  • Session slots: each session's httpd context is a slot in a fixed array the free_fn resets. httpd runs every open, close and free callback on its own task and frees the context right after close_fn; queued send workers hold a weak_ptr, never the slot.
  • Connection object: allocate_shared into a block reserved with platform_malloc_internal (internal RAM preferred, PSRAM fallback), so an accept that cannot get one refuses the session instead of aborting. The fallback is safe: IDF's default STDATOMIC_S32C1I_SPIRAM_WORKAROUND covers the atomics on ESP32/S3, and the object holds no FreeRTOS object or portMUX.
  • Double close: a session open_fn refuses has no context, and its close_fn now leaves the close to httpd_accept_conn(), which closes the fd again; before, another task could reopen that fd in between.
  • closing_: an InlineVector of MAX_OPEN_CONNECTIONS. An accept refused by the final tick is released at once, like a nursery-full rejection, instead of parked until finish_stop().
  • Docs and tools: internals.md "Memory" and the ESP ownership steps (free_fn follows close_fn directly, whatever is queued), the integration-guide clamp, and tools/stack_usage/edges.json for the new free_fn. Stack figures unchanged.

Tests

  • StopRefusesAQueuedAcceptWithAShutdownGoodbye's final-tick row also expects closing_ empty; parking the refused accept fails it, and releasing it without the goodbye hangs the goodbye wait.
  • ESP builds: IDF 5.5.4 (default, no-opus, source-only) and IDF 6.1 (default); the double-close ordering was checked against IDF 5.5.4, 6.0.3 and 6.1 sources.
  • Run on an ESP32-S3 with no issues.

- SendspinWsServer's pending upgrades are an InlineVector of
  MAX_SESSIONS (7, httpd's default max_open_sockets), and stop() and
  tick() release entries one at a time instead of through temporary
  vectors; server_max_connections is clamped to 7 on ESP.
- Each httpd session's context is a slot in a fixed array the free_fn
  resets, not a new std::shared_ptr per accept.
- The connection object is allocate_shared into a block reserved with
  platform_malloc_internal, so an accept that cannot get one refuses
  the session instead of aborting in operator new. A refused session's
  close_fn leaves the close to httpd_accept_conn(), which closes it
  again otherwise.
- ConnectionManager::closing_ is an InlineVector of MAX_OPEN_CONNECTIONS:
  an accept refused at stop is released at once, like a nursery-full
  rejection, instead of parked.
@kahrendt
kahrendt marked this pull request as ready for review November 4, 2026 19:51
@kahrendt
kahrendt requested a max review from Copilot November 4, 2026 19:51

Copilot AI left a comment

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.

🟡 Changes recommended

The public header omits the ESP-only seven-connection clamp, and the white-box test’s access to closing_ lacks the required inventory entry and justification.

2 open issues
What changed in this PR

This PR reduces allocation risk in the ESP inbound connection path by moving session bookkeeping into fixed-capacity storage and using fallible platform allocation for accepted connection objects.

Changes:

  • Replace dynamic session slots and pending-upgrade storage with bounded containers, capping ESP connections at seven.
  • Allocate connection objects through the platform heap and avoid double-closing refused sessions.
  • Bound shutdown retention, test immediate release of refused accepts, and update ownership documentation and stack-analysis mappings.
File Description
tools/​stack_usage/​edges.json Map session cleanup to the new release callback.
tests/​test_client_lifecycle.cpp Assert that final-tick refusals are not retained for shutdown cleanup.
src/​esp/​ws_server.h Define fixed session slots, bounded pending upgrades, and the connection cap.
src/​esp/​ws_server.cpp Implement fallible allocation, slot cleanup, bounded reaping, and double-close prevention.
src/​connection_manager.h Replace shutdown retention storage with a bounded container.
src/​connection_manager.cpp Release refused accepts immediately and use bounded shutdown cleanup storage.
docs/​internals.md Update connection ownership, lifetime, and allocation descriptions.
docs/​integration-guide.md Document the ESP connection-limit clamp.

Review effort: Max

Comment thread src/esp/ws_server.cpp Resolved
Comment thread tests/test_client_lifecycle.cpp Resolved
- server_max_connections' header doc names the clamp to 7 on ESP.
- The lifecycle test's private-access inventory names closing_ and why
  no peer-visible outcome would do.
@kahrendt
kahrendt merged commit 682f485 into main Nov 4, 2026
16 checks passed
@kahrendt
kahrendt deleted the esp-accept-bounded branch November 4, 2026 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants