Screen share: a late publication of an ended share is not the next share's; last input never reads late (#923) #924

Closed
charles wants to merge 0 commits from fix-923-late-publication-next-share into textchat
Owner

Closes #923

Based on fix-921-share-teardown-publish-race (PR #922), which is not yet in textchat; it needs FakeRoomControl::hold_screen_publish. Merge #922 first; this PR's own changes are its two commits on top.

1. A late publication of an ended share was taken by the next share

Cause. on_video_published (fluxer-core go-live) took any sid while the running share's video_sid was None. The race goes like this:

  1. A share is stopped while its publication is still on its way.
  2. A new share starts at once, and its capture reports Started before the old publication lands.
  3. The old LocalTrackPublished arrives and the new share takes it. It announces self_stream, its own key and the start chime under a track that the queued unpublish is about to take down.
  4. The old sid's LocalTrackUnpublished does not clear video_sid.

The codec negotiation also took that sid as the share's publication.

Fix: each publication is tied to the share that asked for it.

  • VoiceCommand::PublishScreenShare carries a new ShareId.
    • The store gives each share a new one when it publishes.
    • Streams.last_share survives the share and logout, so ids are never reused in the process.
    • Streams::own_share() exposes the running share's id.
  • The session (fluxer-voice) keeps the id with the published share and reports every VoiceEvent::LocalTrackPublished of that share with it:
    • the publication itself, a codec republish, and a room-side republish (reconnect, software fallback);
    • a room-side publication with no share in the session is now dropped.
  • The go-live machine and the codec negotiation take a publication only if its id is the running share's (Voice::is_own_publication). Any other publication is ignored and logged at debug.
  • apps/fluxer/src/runtime.rs is not touched.

Tests (each failed before the fix):

  • Core: stream_golive::a_late_publication_of_an_ended_share_is_not_taken_by_the_next_share.
    • Before the fix the next share became Live { video_sid: Some("TR_my_video"), audio_sid: Some("TR_my_audio") }.
    • After the fix it waits for its own publication: no op 4, no chime, codec negotiation unchanged. It then goes live under TR_my_video_2.
  • Core proptest: any_event_order_keeps_golive_consistent now also sends publications of the share before the last one, and asserts that they change nothing. It also checks that share ids are never reused.
    • Before the fix it found a three-step counterexample: Start, CaptureStarted, Published(audio, earlier).
  • Runtime: screenshare_runtime::a_late_publication_of_a_stopped_share_is_not_taken_by_the_next_share, with the publication held.
    • Steps: share, stop while held, share again, then release only the first publication (new FakeRoomControl::release_one_screen_publish). The second publication stays held.
    • Before the fix: Live { video_sid: Some("TR_screen_1") } while the room had nothing published.
    • After the fix: the share waits, then goes live under TR_screen_2.
    • The fake's hold now uses up the permit a held publication gets, so one-at-a-time release works. Previously the permit went back to the semaphore.
  • Session: session_video::each_publication_is_reported_with_the_share_that_asked_for_it. The republish test also checks that the id carries over.

2. Flaky window_input_updates_last_input

Cause. This is a product rounding bug, not only a test bug.

  • last_input keeps whole microseconds after its origin, rounded down. It raised 0 to 1, because 0 means "no input yet".
  • The first input of the process is what initialises the origin. That input falls inside the origin's first microsecond, so it read back up to 1 µs in the future.
  • When the test's last_input() <= Instant::now() ran within that microsecond, it failed.

Fix.

  • The stored value is now the whole microseconds plus one, and is read back minus one (0 still means "none yet"). A mark is therefore never later than the input, and at most 1 µs earlier.
  • The test's lower bound already allowed for that 1 µs (now explained in the comment). Both bounds are kept as they were.
  • New unit test fluxer-ui app::tests::an_input_reads_back_no_later_than_it_happened checks the stamp at 0 ns, 1 ns, 999 ns, 1 µs, 1.5 µs and 1 h. Before the fix it failed deterministically with 0ns: read back after the input.
  • I could not make the original integration test fail locally: 0 failures in 300 runs alone.

Verification

All runs under dbus-run-session -- with FLUXER_NO_DESKTOP_NOTIFICATIONS=1.

  • FLUXER_SCREENSHOT_TESTS=1 cargo xtask ci: ==> xtask ci: all steps passed. LiveKit interop is skipped because FLUXER_LIVEKIT_TESTS is not 1, but its tests compile.
  • Load test: 50 consecutive rounds, each running all four affected test binaries, all passed. The binaries are screenshare_runtime, fluxer-ui app, stream_golive and session_video. Six niced copies of the same binaries looped alongside (load average 24–39).

Not covered

The session's room-side LocalTrackPublished path (reconnect republish) has no fake-room test: FakeRoom cannot emit it.

🤖 Generated with Claude Code

Closes #923 Based on `fix-921-share-teardown-publish-race` (PR #922), which is not yet in `textchat`; it needs `FakeRoomControl::hold_screen_publish`. Merge #922 first; this PR's own changes are its two commits on top. ## 1. A late publication of an ended share was taken by the next share **Cause.** `on_video_published` (fluxer-core go-live) took any sid while the running share's `video_sid` was `None`. The race goes like this: 1. A share is stopped while its publication is still on its way. 2. A new share starts at once, and its capture reports `Started` before the old publication lands. 3. The old `LocalTrackPublished` arrives and the new share takes it. It announces `self_stream`, its own key and the start chime under a track that the queued unpublish is about to take down. 4. The old sid's `LocalTrackUnpublished` does not clear `video_sid`. The codec negotiation also took that sid as the share's publication. **Fix: each publication is tied to the share that asked for it.** - `VoiceCommand::PublishScreenShare` carries a new `ShareId`. - The store gives each share a new one when it publishes. - `Streams.last_share` survives the share and logout, so ids are never reused in the process. - `Streams::own_share()` exposes the running share's id. - The session (fluxer-voice) keeps the id with the published share and reports every `VoiceEvent::LocalTrackPublished` of that share with it: - the publication itself, a codec republish, and a room-side republish (reconnect, software fallback); - a room-side publication with no share in the session is now dropped. - The go-live machine and the codec negotiation take a publication only if its id is the running share's (`Voice::is_own_publication`). Any other publication is ignored and logged at debug. - `apps/fluxer/src/runtime.rs` is not touched. **Tests** (each failed before the fix): - **Core:** `stream_golive::a_late_publication_of_an_ended_share_is_not_taken_by_the_next_share`. - Before the fix the next share became `Live { video_sid: Some("TR_my_video"), audio_sid: Some("TR_my_audio") }`. - After the fix it waits for its own publication: no op 4, no chime, codec negotiation unchanged. It then goes live under `TR_my_video_2`. - **Core proptest:** `any_event_order_keeps_golive_consistent` now also sends publications of the share before the last one, and asserts that they change nothing. It also checks that share ids are never reused. - Before the fix it found a three-step counterexample: `Start, CaptureStarted, Published(audio, earlier)`. - **Runtime:** `screenshare_runtime::a_late_publication_of_a_stopped_share_is_not_taken_by_the_next_share`, with the publication held. - Steps: share, stop while held, share again, then release only the first publication (new `FakeRoomControl::release_one_screen_publish`). The second publication stays held. - Before the fix: `Live { video_sid: Some("TR_screen_1") }` while the room had nothing published. - After the fix: the share waits, then goes live under `TR_screen_2`. - The fake's hold now uses up the permit a held publication gets, so one-at-a-time release works. Previously the permit went back to the semaphore. - **Session:** `session_video::each_publication_is_reported_with_the_share_that_asked_for_it`. The republish test also checks that the id carries over. ## 2. Flaky `window_input_updates_last_input` **Cause.** This is a product rounding bug, not only a test bug. - `last_input` keeps whole microseconds after its origin, rounded down. It raised 0 to 1, because 0 means "no input yet". - The first input of the process is what initialises the origin. That input falls inside the origin's first microsecond, so it read back up to 1 µs *in the future*. - When the test's `last_input() <= Instant::now()` ran within that microsecond, it failed. **Fix.** - The stored value is now the whole microseconds plus one, and is read back minus one (0 still means "none yet"). A mark is therefore never later than the input, and at most 1 µs earlier. - The test's lower bound already allowed for that 1 µs (now explained in the comment). Both bounds are kept as they were. - New unit test `fluxer-ui app::tests::an_input_reads_back_no_later_than_it_happened` checks the stamp at 0 ns, 1 ns, 999 ns, 1 µs, 1.5 µs and 1 h. Before the fix it failed deterministically with `0ns: read back after the input`. - I could not make the original integration test fail locally: 0 failures in 300 runs alone. ## Verification All runs under `dbus-run-session --` with `FLUXER_NO_DESKTOP_NOTIFICATIONS=1`. - `FLUXER_SCREENSHOT_TESTS=1 cargo xtask ci`: `==> xtask ci: all steps passed`. LiveKit interop is skipped because `FLUXER_LIVEKIT_TESTS` is not 1, but its tests compile. - Load test: 50 consecutive rounds, each running all four affected test binaries, all passed. The binaries are `screenshare_runtime`, fluxer-ui `app`, `stream_golive` and `session_video`. Six niced copies of the same binaries looped alongside (load average 24–39). ## Not covered The session's room-side `LocalTrackPublished` path (reconnect republish) has no fake-room test: `FakeRoom` cannot emit it. 🤖 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>
A share stopped while its publication was on its way, followed at once by a new share
whose capture started before the old publication landed, took the old publication:
on_video_published accepted any sid while video_sid was None. The new share announced
self_stream (and its own key, and the start chime) under a track that the queued
unpublish was about to take down, and the old sid's LocalTrackUnpublished did not
clear it.

Each publication is now tied to the share that asked for it:
- VoiceCommand::PublishScreenShare carries a ShareId, new for each share (kept in
  Streams across shares and accounts, so never reused in the process).
- The session keeps it with the published share and reports every
  VoiceEvent::LocalTrackPublished of that share with it, including a republish and a
  room-side republish after a reconnect (dropped when no share is published).
- The go-live machine and the codec negotiation take a publication only when its id
  is the running share's (Voice::is_own_publication).

Tests:
- stream_golive a_late_publication_of_an_ended_share_is_not_taken_by_the_next_share
  (failed before: the next share went Live { video_sid: Some(TR_my_video) }), and the
  event-order proptest now also sends publications of the share before (failed before
  in three steps).
- screenshare_runtime a_late_publication_of_a_stopped_share_is_not_taken_by_the_next_share
  with the held publication (failed before: Live { video_sid: Some(TR_screen_1) } with
  nothing published in the room). FakeRoomControl::release_one_screen_publish lets one
  held publication through; a held publication now uses up its permit.
- session_video each_publication_is_reported_with_the_share_that_asked_for_it; the
  republish test checks that the id carries over.

Refs #923

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
window_input_updates_last_input failed now and then with
last_input() <= Instant::now(). last_input keeps whole microseconds after its origin,
rounded down, but raised 0 to 1 because 0 means "no input yet". An input in the first
microsecond after the origin (the first input of the process is what initialises the
origin) therefore read back up to a microsecond in the future; the test's assertion,
reached within that microsecond, failed.

The stored value is now the whole microseconds plus one (0 still "none yet"), read
back minus one, so a mark is never later than the input and at most a microsecond
earlier, which the test's lower bound already allows. The test keeps both bounds.
A unit test (app::tests::an_input_reads_back_no_later_than_it_happened) checks the
stamp at 0 ns, under and over a microsecond and an hour: it failed before with
"0ns: read back after the input".

Refs #923

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:41:08 +00:00

Pull request closed

Sign in to join this conversation.
No description provided.