Repository navigation
Hold the ESP accept path's sessions in fixed storage - #246
Merged
Merged
Conversation
- 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.
Contributor
There was a problem hiding this comment.
🟡 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
- 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.
7 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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), anew std::shared_ptrsession slot, and apush_backinto astd::vectorof pending upgrades, all through ESPHome's internal-RAMoperator new, which aborts on failure.ConnectionManager::closing_was the onestd::vectoramong the manager's bounded tables.Change
SendspinWsServer::pending_is anInlineVectorofMAX_SESSIONS(7, httpd's defaultmax_open_sockets), andstop()/tick()release entries one at a time instead of through temporary vectors.server_max_connectionsis clamped to 7 on ESP with a warning, like the existing stack clamps.weak_ptr, never the slot.allocate_sharedinto a block reserved withplatform_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 defaultSTDATOMIC_S32C1I_SPIRAM_WORKAROUNDcovers the atomics on ESP32/S3, and the object holds no FreeRTOS object or portMUX.httpd_accept_conn(), which closes the fd again; before, another task could reopen that fd in between.InlineVectorofMAX_OPEN_CONNECTIONS. An accept refused by the final tick is released at once, like a nursery-full rejection, instead of parked untilfinish_stop().internals.md"Memory" and the ESP ownership steps (free_fn follows close_fn directly, whatever is queued), the integration-guide clamp, andtools/stack_usage/edges.jsonfor the new free_fn. Stack figures unchanged.Tests
StopRefusesAQueuedAcceptWithAShutdownGoodbye's final-tick row also expectsclosing_empty; parking the refused accept fails it, and releasing it without the goodbye hangs the goodbye wait.