Skip to content

Hold the stream/* messages' bounded fields inline - #241

Merged
kahrendt merged 2 commits into
mainfrom
stream-messages-bounded
Nov 4, 2026
Merged

kahrendt merged 2 commits into
mainfrom
stream-messages-bounded

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

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 per stream/end and stream/clear, the artwork channel array per stream/start, the source's codec header per input stream, and a new FLAC decoder per codec header). The player's codec_header and stream/start's visualizer types sit in public types and are left for the public-API group.

Change

  • Stream roles: stream/end and stream/clear parse roles into StreamRoles (std::optional<EnumSet<SendspinRole>>) instead of a std::vector<std::string> compared by name in dispatch.
  • Artwork channels: ServerArtworkStreamObject::channels (and the role's streamed_channels) is an InlineVector of at most ARTWORK_MAX_SLOTS, which moves to protocol_messages.h. A longer array rejects the stream/start, per roles/artwork/v1.md "stream/start artwork object" and the fail-closed rule; it was silently truncated before. InlineVector gains copy construction and assignment constrained to trivially copyable elements, so the shared_ptr tables stay neither copyable nor movable.
  • Source codec header: client-stream/start views the FLAC encoder's header (std::optional<std::string_view>), which the encoder owns for the role's life.
  • FLAC decoder: SendspinDecoder creates it on the first FLAC header and reset()s it for each later stream instead of a make_unique per header; the object (about 300 bytes) stays allocated from the first FLAC stream until stop().
  • Docs: internals.md "Memory" rows and inline list updated; tools/stack_usage/README.md re-derived (protocol task -Og bound 6,704 -> 6,672, its reachable stream/start chain 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_tests adds a stream/end + stream/clear window 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).
  • Parser tables for the roles and the four-channel cap, and 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.cpp pins the copy contract with static_asserts.

- 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).

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 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
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

Comment thread src/inline_vector.h Resolved
Comment thread src/decoder.cpp Resolved
Comment thread src/protocol.cpp Resolved
Comment thread tests/test_decoder.cpp Resolved
Comment thread tests/test_protocol.cpp Resolved
Comment thread tests/test_protocol.cpp Resolved
- 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
kahrendt merged commit e3c861c into main Nov 4, 2026
16 checks passed
@kahrendt
kahrendt deleted the stream-messages-bounded branch November 4, 2026 15:40
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.
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