Skip to content

Fix VOD-audio session races (v0.1.8)#24

Merged
Soulhackzlol merged 3 commits into
mainfrom
fix/vod-audio-session-races
Jun 21, 2026
Merged

Fix VOD-audio session races (v0.1.8)#24
Soulhackzlol merged 3 commits into
mainfrom
fix/vod-audio-session-races

Conversation

@Soulhackzlol

Copy link
Copy Markdown
Owner

What this does

Fixes a race in the Twitch VOD-audio path that could make a stream go live
Source-Only and show several short sessions in Twitch Inspector instead of
one.

The egress supervisor wakes every ~2s and, for a Twitch destination with
VOD audio enabled, asks Twitch's API to allocate an IVS session, then
points egress at the returned URL. The old code spawned that API call on
every tick until one came back. When Twitch answered slower than the 2s
tick, the calls stacked: each allocated a separate IVS session and each
rewrote the override URL, which restarted egress. That is the
multi-session, wrong-broadcast-type behaviour in Inspector (one session
Transmuxed Source-Only, a later one Transcoded).

Two guards, both per-destination so multistreaming is unaffected:

  • Single-flight latch: only one session fetch is ever in flight. The claim
    re-checks the override under its lock, so a fetch that just completed
    can't let a duplicate slip through.

  • Session epoch: bumped on every publisher disconnect, under the same lock
    that holds the override. A fetch records the epoch when it starts and
    only applies its result if the epoch still matches. A request that
    returns after OBS disconnected gets discarded instead of writing a
    dead-session IVS URL into the next stream.

Notes for the reviewer

  • Scope is the VOD-audio background fetch only. The /obs/multitrack-config
    proxy (Enhanced Broadcasting via the registered service, and VOD+EB via
    the Launch button) is awaited inside OBS's own blocking request, so it
    always sets the override before the session begins and was never exposed
    to either race. Left as-is on purpose.
  • Non-Twitch destinations never enter this branch, so YouTube, Kick, and
    custom RTMP are unchanged.
  • begin_publish and publisher_token are untouched. I considered reusing
    publisher_token for the staleness check, but it is read by the egress
    pump for sequence-header re-sends and does not cover the gap between
    disconnect and reconnect, so a dedicated per-destination epoch is cleaner
    and keeps that path alone.
  • Also bumps Cargo.lock to the package version, which had been left at 0.1.6.

The egress supervisor wakes every ~2s and, for a Twitch destination with
VOD audio enabled, asks Twitch's API to allocate an IVS session and then
points egress at the returned URL. The old code spawned that API call on
every tick until one came back. When Twitch took longer than 2s to answer,
the calls piled up: each allocated a different IVS session and each
changed the override URL, which restarted egress. A single stream then
showed up as several short sessions in Twitch Inspector, and the first one
came up Source-Only.

Two guards, both per-destination so multistreaming is unaffected:

- Single-flight latch (try_claim_vod_fetch): only one fetch runs at a
  time. The claim re-checks the override under its mutex, so a fetch that
  just landed can't let a duplicate slip through.

- Session epoch: bumped on every publisher disconnect, under the same
  mutex that holds the override. A fetch records the epoch when it starts
  and only writes its result if the epoch still matches. A request that
  returns after OBS disconnected, with an IVS token now bound to a dead
  session, is discarded instead of poisoning the next stream.

Non-Twitch destinations never enter this branch. The synchronous
/obs/multitrack-config proxy is untouched: it runs inside OBS's blocking
request before the session starts, so it was never exposed to either race.

Also syncs Cargo.lock to the package version (was left at 0.1.6).
@Soulhackzlol Soulhackzlol self-assigned this Jun 20, 2026
@Soulhackzlol Soulhackzlol added the bug Something isn't working label Jun 20, 2026
complete_vod_fetch applies the session result (epoch-guarded) and releases
the in-flight latch in one place, so the whole latch lifecycle (claim in
try_claim_vod_fetch, release here) lives on the type that owns it instead
of being split into the supervisor closure. The closure is now just fetch,
complete, log.

Tests: release-on-every-path for complete_vod_fetch (success, stale
discard, failure), single-flight under real thread contention, and an
apply-vs-disconnect stress test that asserts the override is never left
stale regardless of which thread wins.
- Transcoded ladder is only guaranteed for Twitch Partners; Affiliates get
  it opportunistically and far less so above ~6 Mbps. Note that this is
  Twitch's allocation behaviour, not the proxy (OBS's native Twitch preset
  auto-caps bitrate, the Custom-server path InstantClone uses does not).
- Longest validated run is ~5h, ~10h cumulative across sessions.
- Reframe sync ring-append disk I/O as a deliberate trade-off (page cache
  is the async buffer) rather than a planned fix.
- Bump test count 185 -> 197.
@Soulhackzlol Soulhackzlol merged commit c23d6f4 into main Jun 21, 2026
4 checks passed
@Soulhackzlol Soulhackzlol deleted the fix/vod-audio-session-races branch June 21, 2026 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant