Skip to content

Require fresh source starts after input streams end - #273

Merged
kahrendt merged 1 commit into
mainfrom
fix/source-start-authorization
Sep 16, 2026
Merged

kahrendt merged 1 commit into
mainfrom
fix/source-start-authorization

Conversation

@maximmaxim345

Copy link
Copy Markdown
Member

Clarify source start authorization, stopping, and receiver behavior. Changes beyond keyword normalization:

  • Change stop handling from a recommendation to a MUST: cancel pending startup, stop sending audio, and send client-stream/end if an input stream is open - stopping before startup completes must prevent the pending opening too.
  • Limit each start to one opening and require a newly received server start before reopening after client-stream/end. Repeated starts while starting or streaming cannot accumulate permission for later openings.
  • Explicitly require cancellation of pending startup and stopping on role removal or unavailability - returning to an active, available state must not revive an old request.
  • Express initial and reactivation authorization in terms of receiving a server start, not merely the server sending it. Distinguish opening an input stream from an in-place format update.
  • Prohibit treating an otherwise valid opening as unsolicited solely because the server sent its start before receiving the preceding end or availability update - messages in opposite directions can cross, so that command may still be fresh when the client receives it.
  • Replace ambiguous binary-message rejection guidance with mandatory connection closure for source audio without an open input stream or after the latest received client state reports unavailability.
  • Clarify that sending stop or removing the source role does not itself close the input stream at the server. Tolerate valid in-flight responses, including a delayed client-stream/start, until client-stream/end arrives, and process start/end messages in received order even when discarding audio.

Addresses part of #100.

@balloob balloob added this to the 1.0.0 RC1 milestone Sep 16, 2026
@kahrendt

Copy link
Copy Markdown
Contributor

Thanks!

@kahrendt
kahrendt merged commit 425f35e into main Sep 16, 2026
1 check passed
@kahrendt
kahrendt deleted the fix/source-start-authorization branch September 16, 2026 10:26
chrisuthe added a commit to Sendspin/aiosendspin that referenced this pull request Sep 17, 2026
…thout an open stream (#430)

Part of #386. Implements the server-side MUSTs of Sendspin/spec#273 for
`source@v1` ("Source command semantics", "Default streaming behavior",
"Source Audio Chunks").

- [x] **S-8** – A `client-stream/start` with no unused `start` is
flagged and opens nothing, so a strict server closes the connection. A
start answering a `start` that crossed a `stop`, an `available:
false`/`true` pair or a role removal is no longer flagged.
- [x] **S-12** – `client-stream/start`, `client-stream/end` and source
audio are tracked when no source role is active. Audio with no open
input stream, or after the latest `client/state` reported `available:
false`, is flagged, so a strict server closes the connection.

## How

- `SendspinConnection` tracks the client's input stream for the lifetime
of the connection:
- It counts every `start` sent. An opening `client-stream/start` uses
one.
- A `client-stream/start` on an already open stream replaces its format
and needs no `start`.
- `client-stream/end` closes the stream. `stop`, unavailability and role
removal leave it open.
- Because this state outlives the role instance, messages that arrive
after the role is removed are still handled in received order.
- A single flag is not enough. After `start`, `stop`, `start`, a
compliant client can send `client-stream/start`, `client-stream/end`,
`client-stream/start`, and all three are valid.
- `SourceV1Role` now only serves consumers:
  - It records each `start` it sends on the connection.
- It opens a stream handle only if a `start` was sent after its latest
`stop`, unavailability, removal or disconnect.
- Otherwise it discards the input stream until `client-stream/end`, with
no `SourceStreamStartedEvent` or `SourceStreamEndedEvent`.
- An authorized stream whose format is rejected or whose decoder cannot
be built still counts as open, so its chunks are dropped rather than
flagged.

## Limitations

- **Short stale stream:** after `start`, `stop`, `start`, the server
cannot tell which `start` the first opening answers. It serves that
opening as a real stream, so consumers may see a short stream that is
then replaced (started, ended, started). Discarding it instead could
stall a client that ignored the first `start`.
- **Extra leniency from repeated starts:** every `start` sent is
counted, including one the client ignores because a stream is already
pending or open. The server cannot tell such a repeat from a `start`
that crossed the client's own `client-stream/end`, so this only makes
the server more lenient and never flags a valid client.
- **No format checks on discarded streams:** format checks are skipped
for an input stream opened in discard mode.

## Compatibility

- **More tolerant:** the `client-stream/start` path now accepts more.
- **Stricter:** only source audio sent with no open input stream, or
after reporting `available: false`, is now rejected.
- Earlier spec text already required `client-stream/start` before the
first chunk and `client-stream/end` before `available: false`.
- The client SDK sends chunks only with an open stream, and ends the
stream before it reports unavailable.
- It opens a stream only with a synced clock, and it only loses sync
when it disconnects. So `_wire_available()` cannot report `available:
false` while a source stream is open.
- No `DEPRECATED(spec-pr-273)` marker is needed.

## Tests

- `tests/server/test_source_input_stream.py` (new) uses a real
connection, client and role. It covers:
  - a `stop` crossing an in-flight `start`;
  - an availability round trip;
- role removal with a `start` outstanding and with a stream open,
including a strict server closing on audio received after
`client-stream/end`;
  - reactivation;
  - an undecodable authorized stream.
- `tests/server/test_source_connection_dispatch.py` covers:
  - unsolicited openings;
  - one opening per `start`;
  - in-place replacement;
  - `start`s that cross the client's `client-stream/end`;
  - audio with no open stream or while unavailable;
  - quiet drops with no source role.
- The role-level tests for flagging unsolicited starts and dropping
audio while unavailable moved to the connection, which now owns those
rules.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants