Screen share: a share torn down while publishing leaves nothing published (#921) #922

Closed
charles wants to merge 0 commits from fix-921-share-teardown-publish-race into textchat
Owner

Closes #921

Cause: the test's timing (possibility 1), not a product race. The test did not wait long enough. The product leaves nothing published.

When the fake capture reports Started before the stream audio fails, this happens:

  1. The store goes Live { video_sid: None } and sends PublishScreenShare.
  2. On AudioFailed it sends UnpublishScreenShare and CaptureCommand::Stop, and goes straight to Idle. It skips Stopping because the gateway was never told about the share.
  3. The voice session runs commands one at a time, in order, from a single queue. Its select! is biased toward commands, and nothing is read while publish_screen is awaited. So the unpublish always follows the publish.
  4. The publish can still land after the capture has stopped. The test asserted at that moment and saw the publication in the short gap between publish and unpublish.

The usual order is AudioFailed before Started, which publishes nothing at all. That makes the CI failure rare.

Evidence

  • New knob FakeRoomControl::hold_screen_publish / release_screen_publish keeps a publication pending, so the order can be forced on purpose.
  • With the publication held and the share torn down, a temporary diagnostic sampled room.screen() right after the release:
    • In 3 runs × 3 teardowns (audio failed, stop, source closed), the publication was visible for about 10–50 µs, once about 1 ms.
    • Each time it was then unpublished.
    • Calls were always ["publish", "unpublish"] and the store was already Idle.
    • Room lost: the held publish fails and nothing is published.
  • That gap is exactly what line 555 asserted inside.
  • I could not reproduce the original CI failure without the hold. The unmodified test passed every time:
    • 150 runs alone under 5 niced copies
    • 100 runs of the whole binary under 10 copies (load average up to about 58)
    • 150 runs pinned to 2 CPUs with 4 copies

Why the product is safe

  • On every teardown from Live, the store sends UnpublishScreenShare (stop_share, drop_share).
  • The session runs it after the PublishScreenShare it follows, and takes down what that publish produced.
  • If the room is lost, the session's Disconnected handling drops the share. A publication that lands late is answered with the unpublish already queued.
  • The late LocalTrackPublished does nothing in the store while Idle: no self_stream, no own key, no chime.

Changes

  • stream_audio_that_cannot_start_ends_the_share_and_says_so now waits until the room has heard the share's last screen command (none, or an unpublish) before asserting that nothing is published.
  • Teardown while the publication is still pending, covered at three levels:
    • Runtime (apps/fluxer/tests/screenshare_runtime.rs): stream_audio_failing_…, stop_…, source_closed_… and room_lost_while_publishing_leaves_nothing_published.
    • Session (crates/fluxer-voice/tests/session_video.rs): unpublish_sent_while_publishing_takes_down_what_the_publication_produced.
    • Store (crates/fluxer-core/tests/stream_golive.rs): teardown_while_publishing_unpublishes_and_ignores_the_late_publication. It covers audio failed, stop, source closed, capture error and room lost.
  • The session.rs and golive.rs module docs state the ordering guarantee.
  • No product code changed. runtime.rs is not touched.

Verification (every run under dbus-run-session, FLUXER_NO_DESKTOP_NOTIFICATIONS=1)

  • screenshare_runtime (the whole binary, all 15 tests): 50 of 50 consecutive passes, with 6 niced copies of the same binary looping alongside. This was run on the final commit.
  • FLUXER_SCREENSHOT_TESTS=1 cargo xtask ci: ==> xtask ci: all steps passed.
    • The first run failed in an unrelated test, fluxer-ui window_input_updates_last_input (last_input() <= Instant::now()). The rerun passed.

Not fixed here (worth a separate issue): a late LocalTrackPublished from a share that already ended can be taken by a new share that is waiting for its own confirmation. The sequence:

  1. Stop while publishing.
  2. Start again at once, so the new capture is Started before the old publication lands.
  3. on_video_published takes any sid while video_sid is None, so the new share can announce self_stream early under the old sid.
  4. The old sid's LocalTrackUnpublished does not clear video_sid.

🤖 Generated with Claude Code

Closes #921 **Cause: the test's timing (possibility 1), not a product race.** The test did not wait long enough. The product leaves nothing published. When the fake capture reports `Started` before the stream audio fails, this happens: 1. The store goes `Live { video_sid: None }` and sends `PublishScreenShare`. 2. On `AudioFailed` it sends `UnpublishScreenShare` and `CaptureCommand::Stop`, and goes straight to `Idle`. It skips `Stopping` because the gateway was never told about the share. 3. The voice session runs commands one at a time, in order, from a single queue. Its `select!` is biased toward commands, and nothing is read while `publish_screen` is awaited. So the unpublish always follows the publish. 4. The publish can still land after the capture has stopped. The test asserted at that moment and saw the publication in the short gap between publish and unpublish. The usual order is `AudioFailed` before `Started`, which publishes nothing at all. That makes the CI failure rare. **Evidence** - New knob `FakeRoomControl::hold_screen_publish` / `release_screen_publish` keeps a publication pending, so the order can be forced on purpose. - With the publication held and the share torn down, a temporary diagnostic sampled `room.screen()` right after the release: - In 3 runs × 3 teardowns (audio failed, stop, source closed), the publication was visible for about 10–50 µs, once about 1 ms. - Each time it was then unpublished. - Calls were always `["publish", "unpublish"]` and the store was already `Idle`. - Room lost: the held publish fails and nothing is published. - That gap is exactly what line 555 asserted inside. - I could not reproduce the original CI failure without the hold. The unmodified test passed every time: - 150 runs alone under 5 niced copies - 100 runs of the whole binary under 10 copies (load average up to about 58) - 150 runs pinned to 2 CPUs with 4 copies **Why the product is safe** - On every teardown from `Live`, the store sends `UnpublishScreenShare` (`stop_share`, `drop_share`). - The session runs it after the `PublishScreenShare` it follows, and takes down what that publish produced. - If the room is lost, the session's `Disconnected` handling drops the share. A publication that lands late is answered with the unpublish already queued. - The late `LocalTrackPublished` does nothing in the store while `Idle`: no `self_stream`, no own key, no chime. **Changes** - `stream_audio_that_cannot_start_ends_the_share_and_says_so` now waits until the room has heard the share's last screen command (none, or an unpublish) before asserting that nothing is published. - Teardown while the publication is still pending, covered at three levels: - Runtime (`apps/fluxer/tests/screenshare_runtime.rs`): `stream_audio_failing_…`, `stop_…`, `source_closed_…` and `room_lost_while_publishing_leaves_nothing_published`. - Session (`crates/fluxer-voice/tests/session_video.rs`): `unpublish_sent_while_publishing_takes_down_what_the_publication_produced`. - Store (`crates/fluxer-core/tests/stream_golive.rs`): `teardown_while_publishing_unpublishes_and_ignores_the_late_publication`. It covers audio failed, stop, source closed, capture error and room lost. - The `session.rs` and `golive.rs` module docs state the ordering guarantee. - No product code changed. `runtime.rs` is not touched. **Verification** (every run under `dbus-run-session`, `FLUXER_NO_DESKTOP_NOTIFICATIONS=1`) - `screenshare_runtime` (the whole binary, all 15 tests): 50 of 50 consecutive passes, with 6 niced copies of the same binary looping alongside. This was run on the final commit. - `FLUXER_SCREENSHOT_TESTS=1 cargo xtask ci`: `==> xtask ci: all steps passed`. - The first run failed in an unrelated test, `fluxer-ui` `window_input_updates_last_input` (`last_input() <= Instant::now()`). The rerun passed. **Not fixed here (worth a separate issue):** a late `LocalTrackPublished` from a share that already ended can be taken by a *new* share that is waiting for its own confirmation. The sequence: 1. Stop while publishing. 2. Start again at once, so the new capture is `Started` before the old publication lands. 3. `on_video_published` takes any sid while `video_sid` is `None`, so the new share can announce `self_stream` early under the old sid. 4. The old sid's `LocalTrackUnpublished` does not clear `video_sid`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The intermittent failure of stream_audio_that_cannot_start_ends_the_share_and_says_so
was the test's timing, not a product race. When the fake capture reported Started
before the stream audio failed, the store had sent PublishScreenShare and then, on
the failure, UnpublishScreenShare and the capture stop. It ends the share at once
(the gateway was never told). The session carries out commands one at a time and in
order, so the unpublication always follows the publication. But the publication can
land a moment after the capture stopped, and the test asserted at that moment.

- The test now waits until the room has heard the share's last screen command before
  asserting that nothing is published.
- FakeRoomControl::hold_screen_publish / release_screen_publish keep a publication on
  its way, so the order can be driven on purpose.
- New tests for teardown while the publication is on its way: stream audio failed,
  stopped by the user, source closed, room lost. They cover it at the runtime level
  (screenshare_runtime.rs), in the session (session_video.rs) and in the store
  (stream_golive.rs: Unpublish and capture stop are sent, and the late
  confirmation announces nothing and plays no chime).
- The session and go-live docs state the ordering guarantee.

Refs #921

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
Owner

Merged into textchat by hand (the merge API returns 405); the commits are in the branch.

Merged into `textchat` by hand (the merge API returns 405); the commits are in the branch.
charles closed this pull request 2026-11-19 01:40:51 +00:00

Pull request closed

Sign in to join this conversation.
No description provided.