Repository navigation
Open connect_to's connection without operator new or URL copies - #254
Merged
Merged
Conversation
connect_to() built its outbound connection with std::make_shared, so on ESPHome an exhausted internal heap aborted, and copied the URL into the protocol task's request slot and again into the transport. The ESP accept path's ConnectionBlockAllocator moves into a shared private header (src/connection_block.h), and the outbound connection is built in one block from the platform_malloc family the same way, internal RAM first with PSRAM fallback; a block that cannot be allocated skips the connect, keeping any earlier attempt. Nothing is reserved for a path most consumers never call. The public connect_to() takes its URL by value, and every hop to the transport moves it. A new alloc window covers connect_to() through admission (0).
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The by-value URL parameter creates a string temporary on the protocol task’s deepest path, leaving a potential ESP stack-usage increase unmeasured.
1 open issue
What changed in this PR
This PR moves outbound connect_to() setup onto the library’s failure-aware platform heap and removes internal URL copies, aligning it with ESP connection handling.
Changes:
- Share the connection-block allocator between ESP accepts and outbound connections, handling allocation failure without replacing an earlier attempt.
- Move URLs through lifecycle dispatch and update example callers.
- Add an allocation regression test and synchronize API, memory, and test documentation.
| File | Description |
|---|---|
tests/test_allocations.cpp |
Add outbound admission allocation coverage and transport classification. |
tests/README.md |
Update allocation-test coverage descriptions. |
src/esp/ws_server.cpp |
Use the shared allocator for accepted connections. |
src/connection_manager.h |
Update the internal connection signature and failure documentation. |
src/connection_manager.cpp |
Allocate outbound connections from the platform heap and handle failure. |
src/connection_block.h |
Introduce the shared connection-block allocator. |
src/client.cpp |
Move the URL into the lifecycle request slot. |
src/client_dispatch.cpp |
Move lifecycle requests and forward URL ownership. |
include/sendspin/client.h |
Update API signatures and allocation-failure documentation. |
examples/tui_client/tui.cpp |
Move the selected server URL into connect_to(). |
examples/tui_client/main.cpp |
Move the startup connection URL. |
examples/source_client/main.cpp |
Move the startup connection URL. |
examples/basic_client/main.cpp |
Move the startup connection URL. |
docs/internals.md |
Document connection allocation and URL ownership changes. |
docs/integration-guide.md |
Explain how callers avoid URL copies. |
CLAUDE.md |
Update the allocation-test coverage summary. |
Review effort: Max
kahrendt
pushed a commit
that referenced
this pull request
Nov 12, 2026
…#263) ## Summary Since #254, `ConnectionBlockAllocator::allocate()` fails its compile-time check on Raspbian's ARMv6 toolchain: ```text src/connection_block.h:55:33: error: static assertion failed: CONNECTION_BLOCK_BYTES must hold the connection's control block ``` `CONNECTION_BLOCK_BYTES<Conn>` reserved `sizeof(Conn) + 32` for the object and the control block `std::allocate_shared()` puts it in. Raspbian's libstdc++ uses the mutex lock policy for `shared_ptr`, even though the compiler reports lock-free atomics, so the control block embeds a `pthread_mutex_t` and needs 48 bytes beyond `Conn`. sendspin-cpp-cli's linux-armv6 job (Raspberry Pi Zero and Pi 1) can't move its pin past 3a3722a until this builds. | | Raspbian bookworm, ARMv6 | Debian bookworm, armhf | | --------------------------------- | ------------------------ | ---------------------- | | `__GCC_ATOMIC_INT_LOCK_FREE` | 2 | 2 | | `std::__default_lock_policy` | `_S_mutex` | `_S_atomic` | | `sizeof(std::_Sp_counted_base<>)` | 36 | 12 | | Control block overhead | 48 (over 32) | 24 (fits) | ## Change `CONNECTION_BLOCK_BYTES<Conn>` is now `sizeof(Conn) + 32 + sizeof(std::mutex)`. libstdc++'s policy mutex wraps the same `pthread_mutex_t` that `std::mutex` does, so this covers it without naming libstdc++ internals in the library: - Raspbian ARMv6: 56 bytes allowed, 48 needed. - 64-bit libstdc++ with the mutex policy: 72 allowed, 64 needed. - ESP-IDF: `std::mutex` is newlib's 4-byte `pthread_mutex_t` handle, so each connection block grows by about 4 bytes. Nothing else changes: no static RAM, flash, stack or allocation count. The 32 bytes already fit there. A flat 64 would be simpler, but leaves no headroom for a 64-bit mutex-policy libstdc++. Sizing from the exact control block type would need libstdc++ internals. ## Tests New `test_connection_block.cpp` places the host `SendspinClientConnection` in a reserved block, the way `connect_to()` does: - `HoldsConnectionUnderDefaultPolicy`: the control case. The object lands inside the block, and dropping the last reference frees it (LSan checks the free in the sanitizer build). - `HoldsConnectionUnderMutexPolicy` (libstdc++ only): builds through `std::__allocate_shared<Conn, _S_mutex>`, so CI's ubuntu jobs build the ARMv6 layout. With the old `+ 32` it fails to compile (`1688 <= 1656`); with the fix it passes. A 32-bit armhf job would not have caught this, because armhf uses the atomic policy. Full suite on Linux (g++ 13, ASan/UBSan): 760/760 pass. The new file also compiles with clang-cl.
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
connect_to()built its outbound connection withstd::make_shared, so on ESPHome, whereoperator newis internal-RAM-only, an exhausted heap aborted the device. It also copied the URL into the protocol task's request slot and again into the transport. Most consumers never callconnect_to(), so this removes both without reserving any memory for it. Part of Sendspin/backlog#92.API:
connect_to(const std::string&)becomesconnect_to(std::string url). Every existing call still compiles; a caller passing a named string now copies it at the call site, a temporary orstd::movecosts nothing.Change
ConnectionBlockAllocatormoves into a shared private header,src/connection_block.h(make_connection_in_block<Conn>()), and builds the outbound connection too: one block fromplatform_malloc_internal()(internal RAM first, PSRAM fallback, safe for the client connection for the same reasons as the server one) for the object and its control block, freed with the connection.apply_lifecycle_requests()(now takingLifecycleRequests&&),ConnectionManager::connect_to()and the connection's constructor; a replaced URL is still destroyed outside the request mutex. The examples move theirs.Tests
Allocations.ConnectToThroughAdmissiondrivesconnect_to()against the outbound fake server through admission and measures 0 library allocations with a URL longer than either standard library's inline string. Mutants caught:make_sharedput back, and dropping the move at each of the three library hops (each 1 allocation).SendspinClientConnection::startand::send_ws_frameas host transport, so anything else in that class still counts as library.Examples compile, and an esp32 build (ESP-IDF 5.5.4) compiles and links. Stack re-measured on the final tree (ESP-IDF 5.5.4, esp32): every task matches
tools/stack_usage/README.md, the protocol task at 6,544 B -Os / 6,688 B -Og bounded and 5,920 / 6,144 reachable;apply_lifecycle_requests()keeps its own 64 B frame off the deepest chain.