Skip to content

Gate stream/start's sections before parsing them - #242

Merged
kahrendt merged 2 commits into
mainfrom
stream-start-gate-first
Nov 4, 2026
Merged

kahrendt merged 2 commits into
mainfrom
stream-start-gate-first

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Summary

From the second coherence pass on the allocation follow-ups (Sendspin/backlog#92). stream/start parsed every section, including the player codec_header copy and the visualizer types vector, for every connection before any role gate. So a never-admitted peer (one keyed with the published Sentinel PSK) could make the protocol task allocate up to 16 KiB, and an admitted non-owner up to 1 MiB, through ESPHome's aborting operator new. Its siblings server/state, server/command and group/update already gate first. The order dates from the initial commit; it is not a regression from the allocation series.

Change

  • Gate first: the dispatch decides the player, artwork and visualizer gates first and passes the taken set (EnumSet<SendspinRole>) to process_stream_start_message(). It parses only those sections; an untaken section is skipped unvalidated, as in server/state.
  • Move, not copy: the player and visualizer sections are moved into their roles, and GenerationSlot::write() takes T&& like merge().
  • One malformed-section rule: a malformed section rejects the whole message, as the player and artwork sections already did. The visualizer section follows it now (it used to drop only itself), and a missing types or rate_max, or a missing tracks_downbeats when beat is listed, rejects too, per roles/visualizer/v1.md "stream/start visualizer object".
  • Docs: role_accepts_traffic()'s doc, internals.md "Memory", the sendspin_alloc_tests sentence in CLAUDE.md and tests/README.md, and tools/stack_usage/README.md, re-derived: the protocol task's -Og bound goes 6,672 -> 6,688, and its stream/start chain 6,352 -> 5,296, so the deepest reachable chain is pairing confirm at 6,144. The 7,168 default is unchanged and covers the full -Og bound plus margin.

Tests

  • sendspin_alloc_tests: a FLAC stream/start with a long codec_header while the player role is not taken allocates 0; parsing before the gate makes it 1. The stream/* window adds an artwork + visualizer stream/start at 1 allocation (the parse's types vector); copying into the visualizer slot makes it 2.
  • Protocol.StreamStartParsesOnlyTheTakenSections: a parser that ignores the role set fails it.
  • Protocol.StreamStartRejectsSpectrumWithoutValidConfig and Protocol.StreamStartRejectsAnIncompleteVisualizerSection: a table with controls, each row also checking that the valid player section beside it is reset; dropping any of the checks fails its row.

stream/start parsed every section, including the player codec_header
copy and the visualizer types vector, for every connection before any
role gate, so a never-admitted peer could make the protocol task
allocate up to 16 KiB through an aborting operator new. Its siblings
(server/state, server/command, group/update) gate first.

- The dispatch decides each role gate first and passes the taken set to
  process_stream_start_message(), which parses only those sections.
- The player and visualizer sections are moved into their roles, and
  GenerationSlot::write() takes T&& like merge().
- One rule for a malformed section: it rejects the whole message. The
  visualizer section now follows it, and a missing types, rate_max or
  (with beat) tracks_downbeats rejects too, per roles/visualizer/v1.md
  "stream/start visualizer object".
- Allocation windows pin the move and the gate; the protocol task stack
  figures are re-derived (default unchanged at 7,168).
@kahrendt
kahrendt marked this pull request as ready for review November 4, 2026 17:48
@kahrendt
kahrendt requested a max review from Copilot November 4, 2026 17:49

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

Missing or malformed artwork channels and non-string types entries fail to reject the message, allowing neighboring player sections to start in violation of the fail-closed contract.

3 open issues
What changed in this PR

Moves stream/start role checks ahead of section parsing to avoid unnecessary heap allocations from connections that do not own those roles.

Changes:

  • Parses only the player, artwork, and visualizer sections accepted by their role gates.
  • Moves player and visualizer payloads rather than copying them.
  • Tightens visualizer validation and expands regression tests, allocation checks, and resource documentation.
File Description
tools/​stack_usage/​README.md Updates protocol-task stack bounds and sizing rationale.
tests/​test_visualizer_role.cpp Adapts tests to moved configurations and updates parser comments.
tests/​test_protocol.cpp Adds role-selection and visualizer-validation cases.
tests/​test_player_role.cpp Passes stream parameters by move.
tests/​test_json_arena.cpp Uses role-aware stream extraction.
tests/​test_client_lifecycle.cpp Adapts lifecycle tests to consuming stream handlers.
tests/​test_allocations.cpp Checks skipped-section allocations and visualizer payload moves.
tests/​README.md Documents expanded allocation coverage.
src/​visualizer_role.cpp Moves stream configuration into the generation slot.
src/​visualizer_role_impl.h Updates the stream-handler signature.
src/​protocol.cpp Adds role-selected section parsing and stricter visualizer validation.
src/​protocol_messages.h Documents the role-aware parser contract.
src/​player_role.cpp Accepts stream parameters by rvalue reference.
src/​player_role_impl.h Updates the player handler declaration.
src/​inbox.h Makes generation-slot writes accept rvalue references.
src/​client_dispatch.cpp Applies role gates before parsing and moves accepted payloads.
docs/​internals.md Updates message-field allocation behavior.
CLAUDE.md Documents additional allocation-test scenarios.

Review effort: Max

Comment thread src/protocol.cpp Resolved
Comment thread src/protocol.cpp Resolved
Comment thread src/client_dispatch.cpp Resolved
…r type

- An artwork stream/start object without a channels array rejects the
  message, per roles/artwork/v1.md "stream/start artwork object"; it
  was treated as absent.
- A visualizer types entry that is not a string rejects the message;
  an unrecognized type string is still skipped.
- The role-deactivation test comment says removed-role traffic is
  skipped unparsed.
@kahrendt
kahrendt merged commit d357123 into main Nov 4, 2026
16 checks passed
@kahrendt
kahrendt deleted the stream-start-gate-first branch November 4, 2026 18:18
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