Repository navigation
Hold the stream/* messages' bounded fields inline - #241
Merged
Merged
Conversation
- stream/end and stream/clear parse their roles into an EnumSet (StreamRoles) instead of a std::vector<std::string> compared by name. - stream/start's artwork channels are an InlineVector of at most ARTWORK_MAX_SLOTS; a longer array rejects the message (roles/artwork/v1.md "stream/start artwork object"). InlineVector copies when its element is trivially copyable and stays neither copyable nor movable otherwise. - client-stream/start views the FLAC encoder's codec header. - The decoder keeps its FLAC decoder across streams and reset()s it, instead of a make_unique per codec header. - The allocation test adds a stream/end + stream/clear window (0; 4 with the string list); the stack figures are re-derived (protocol task -Og 6,672, its reachable stream/start chain 6,352, default unchanged).
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The new InlineVector copy support makes ClientHelloMessage copyable and movable, violating its self-view contract and allowing copied MAC-address views to dangle when the original message is destroyed.
6 open issues
Delete ClientHelloMessage copy and move operations · New Update docs for retained FLAC decoder after stream abandonment · New Warn on non-array roles while preserving fallback · New Check decoder identity immediately after reset · New Assert artwork limit against the specified cap · New Test present non-array roles fallback · New
What changed in this PR
Reduces per-stream heap allocations in sendspin-cpp by keeping bounded message fields inline, viewing encoder-owned headers, and reusing the FLAC decoder.
Changes:
- Replace role-name lists and artwork channel vectors with bounded inline containers, rejecting artwork arrays longer than four channels.
- Return source codec-header views and retain the FLAC decoder across streams.
- Expand allocation and behavior tests, and update memory and stack-budget documentation.
| File | Description |
|---|---|
tools/stack_usage/README.md |
Refresh protocol-task stack bounds; retain the existing default. |
tests/test_source_role.cpp |
Adapt FLAC tests to codec-header views. |
tests/test_protocol.cpp |
Cover stream-role parsing and artwork channel limits. |
tests/test_inline_vector.cpp |
Assert the constrained copy contract. |
tests/test_decoder.cpp |
Check decoding and decoder retention across streams. |
tests/test_client_lifecycle.cpp |
Test role-selective stream-end and stream-clear delivery. |
tests/test_artwork_role.cpp |
Adapt artwork fixtures to inline channels. |
tests/test_allocations.cpp |
Check allocation-free stream role-list handling. |
tests/lifecycle_test_fixtures.h |
Count visualizer stream-clear callbacks. |
tests/CMakeLists.txt |
Document private decoder-member access in tests; comment-only change. |
src/source_task.h |
Declare codec-header views and document their lifetime. |
src/source_task.cpp |
Return views of encoder-owned codec headers. |
src/protocol.cpp |
Parse inline role sets and enforce the artwork channel cap. |
src/protocol_messages.h |
Define inline message fields, header views, and the shared artwork limit. |
src/inline_vector.h |
Allow copying for trivially copyable element types. |
src/decoder.h |
Document decoder retention and reset requirements; comment-only change. |
src/decoder.cpp |
Retain and reuse the FLAC decoder object. |
src/client_dispatch.cpp |
Dispatch stream-end and stream-clear using enum-set membership. |
src/artwork_role_impl.h |
Store remembered artwork channels inline. |
docs/internals.md |
Update the allocation model and retained-decoder description. |
Review effort: Max
- A stream/end or stream/clear whose roles is present but not an array is rejected and logged (docs/conventions.md "Protocol validation") instead of applying to every role; absent or null still means all. - ClientHelloMessage deletes its copies: the new InlineVector copy had made it copyable, and a copy's MAC address view would point into the original. - abandon_stream() and playback-sync.md say the FLAC decoder object is kept; the decoder test checks it survives the reset itself; the channel-cap test compares against the spec's 4.
kahrendt
added a commit
that referenced
this pull request
Nov 4, 2026
## Summary From the second coherence pass on the allocation follow-ups (Sendspin/backlog#92). The handshake logs peer text only up to 32 bytes, but after it three sites a connection keyed with the published Sentinel PSK reaches logged peer text at full length: up to 16 KiB per message, enough to stall the protocol task on a UART console. `determine_message_type()` also compared `type` as a `const char*`, so a type with an embedded NUL (`"server/hello\u0000x"`) dispatched as the known one. Separately, a present non-array `server/activate` `active_roles` was silently taken as omitted, keeping the prior roles, while its sibling `stream/*` `roles` rejects. ## Change - **Log bound:** `PEER_TEXT_LOG_BYTES` (32) and `peer_text_for_log()` (a UTF-8-safe cut) move from `noise_handshake.cpp` to `inline_text.h`. They bound the unhandled message `type`, an unrecognized enum value from `read_enum_field()` (server/activate's `pairing.method`/`pairing.format`), and the `pair/abort` reason. Every other peer string is bounded where it is parsed. - **Message type:** `determine_message_type()` reads `type` once as a `string_view` and compares with `==`. - **active_roles:** a present non-array value logs and rejects the activate, like `read_stream_roles()`; absent or null still keeps the prior set. A rejected activate leaves a nursery connection to the 30 s establish reap and an admitted one with its prior state. - **Docs:** `conventions.md` says peer text no field bounds is logged only up to the cap. - **Test port:** the inbound-off lifecycle test moves off 19105, which #238 and #241 both picked, so a parallel ctest run no longer fails one of them. ## Tests - `Protocol.DetermineMessageType` gains an embedded-NUL row; a `strcmp` comparison fails it. - `Protocol.ServerActivateReadsActiveRoles` gains null, string and object rows; restoring the nullopt fallback fails the string and object rows, dropping the null branch fails the null row. - `Protocol.UnrecognizedPeerTextIsLoggedOnlyUpToTheBound` (pairing method and pair/abort reason) and `EncryptedLifecycle.AnUnhandledTypeIsLoggedOnlyUpToTheBound` capture stderr and check 32 bytes are logged and 33 are not; dropping each cut fails its test. ## Not in this PR - A non-string entry in `activities`, `active_roles` or `stream/*` `roles` is still skipped, so `active_roles:[7]` on a connection that is not playback-capable passes the "MUST be empty" rule. - Because the pairing index is bumped only after a successful parse, a server that counts a parse-rejected pairing activate falls one index ahead until the next handshake.
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
Group E of the allocation follow-ups (Sendspin/backlog#92): the
stream/*messages. Their bounded fields were heap-held on the protocol task or the sync task for every stream (a role-name list perstream/endandstream/clear, the artwork channel array perstream/start, the source's codec header per input stream, and a new FLAC decoder per codec header). The player'scodec_headerandstream/start's visualizer types sit in public types and are left for the public-API group.Change
stream/endandstream/clearparserolesintoStreamRoles(std::optional<EnumSet<SendspinRole>>) instead of astd::vector<std::string>compared by name in dispatch.ServerArtworkStreamObject::channels(and the role'sstreamed_channels) is anInlineVectorof at mostARTWORK_MAX_SLOTS, which moves toprotocol_messages.h. A longer array rejects thestream/start, per roles/artwork/v1.md "stream/start artwork object" and the fail-closed rule; it was silently truncated before.InlineVectorgains copy construction and assignment constrained to trivially copyable elements, so the shared_ptr tables stay neither copyable nor movable.client-stream/startviews the FLAC encoder's header (std::optional<std::string_view>), which the encoder owns for the role's life.SendspinDecodercreates it on the first FLAC header andreset()s it for each later stream instead of amake_uniqueper header; the object (about 300 bytes) stays allocated from the first FLAC stream untilstop().internals.md"Memory" rows and inline list updated;tools/stack_usage/README.mdre-derived (protocol task -Og bound 6,704 -> 6,672, its reachablestream/startchain 6,368 -> 6,352; the 7,168 default is unchanged, every other task unchanged).A non-array
roles(e.g."roles":"player") still applies the message to every role, as before.Tests
sendspin_alloc_testsadds astream/end+stream/clearwindow with role lists: 0, and 4 with the old string list.ClientLifecycle.StreamEndAndClearReachOnlyTheNamedRoles: each message reaches only the roles it names, using a visualizer callback on the same ordered event ring as the barrier; the two dispatch mutants that ignore the list fail it (both survived the suite before).Decoder.FlacStreamAfterAResetDecodesOnTheSameDecoder, which also checks the decoder object is kept; mutants for the role mapping, the cap and the decoder rebuild fail them.test_inline_vector.cpppins the copy contract withstatic_asserts.