Skip to content

audio: remove a session's virtual sink when the session stops - #599

Open
xerhab wants to merge 2 commits into
games-on-whales:betafrom
xerktech:XERK-2685-session-sink-upstream
Open

xerhab wants to merge 2 commits into
games-on-whales:betafrom
xerktech:XERK-2685-session-sink-upstream

Conversation

@xerhab

@xerhab xerhab commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Summary: A session's audio sink is now removed when the session stops, not when its first app exits. Apps started on a running session through the API no longer lose their audio when another one exits.

Builds on #578. Only the last commit is new here; I'll rebase once #578 lands.

Why

Every runner started on a session (its app, plus any started through /api/v1/runners/start) was given the session's virtual sink and deleted it when it exited. So the first runner to exit removed the sink while the others were still running. Runners started after that get a PULSE_SINK that no longer exists, and the session's pulsesrc fails with "Internal data stream error". I mentioned this as a follow-up in #578.

What changed

  • The StopStreamEvent handler deletes the stopping session's sink, and session runners get no audio_sink. A lobby still deletes its sink when its single runner exits.
  • /resume replaces the StreamSession instance, so it now carries the sink over, the same way it carries over the display and devices. Without that, the resumed session would never remove its sink.
  • Tests: stopping a session deletes only its own sink, a runner exiting leaves the sink alone, and /resume keeps the same sink. They run without a PulseAudio daemon by checking the sink's deleted flag.

Risk

Low. Sinks are created exactly as before; only the moment they're removed changes. Every session path I tested ends with no sinks or modules left in PulseAudio.

Testing

QA result: pass. I ran the real Wolf binary with a real PulseAudio:

  • Two runners: with runners/start "sleep 60" and then runners/start true, beta drops the sink while sleep is still running. With this change the sink stays, and sessions/stop removes it.
  • Resume and cancel: HTTPS launch, 3× resume, cancel keeps one sink throughout and leaves none afterwards. Stopping a session before its sink finishes loading leaves no leak.

Not tested: a lobby runner exiting (QA had no waylanddisplaysrc), Docker runners, and a real Moonlight client's audio stream.

Test evidence
  • Mutants: removing the stop-time delete fails "Stopping a session removes its virtual audio sink". Restoring the runner's audio_sink fails "A runner exiting leaves its session's virtual audio sink alone". Dropping the /resume carry-over fails the resume test and leaks a sink on every resume against real Wolf.
  • The new runner test ran 400 times with 0 failures. wolftests ~[DOCKER]: the only failures are the same environment-dependent ones as on the base.
  • clang-format 18.1.8 is clean.

Follow-ups

None.

… exits and a missing server

- Several runners exiting on one session aborted Wolf: delete_virtual_sink()
  called promise::get_future() each time, and the second call threw
  future_already_retrieved. The index is now read through a shared_future,
  and only the first delete unloads the module.
- libpulse's plain pa_mainloop isn't thread-safe, yet queue_op ran
  pa_context_* calls on API and runner threads while the mainloop iterated
  on its own, so bursts of runner starts and exits corrupted memory
  (SIGSEGV, queue.c asserts). It now uses pa_threaded_mainloop and runs
  every op under its lock; ops queued while connecting run once READY.
  delete_virtual_sink no longer blocks under the lock: if the module is
  still loading, the load callback unloads it, and holds its own VSink
  reference (released if libpulse cancels the request). The sink spec is
  formatted before queueing, since sink_name is a view of the caller's
  string.
- With no reachable PulseAudio and no fallback container, run() read
  ->server from an empty optional and segfaulted; runners also got an empty
  pulse mount and PULSE_SERVER that Docker rejects. Wolf now starts without
  audio instead.
Every runner started on a session (its app, plus any started through the
API's runners/start) was handed the session's virtual sink and deleted it
on exit. So the first runner to exit removed the sink while the others
still ran: later runners got a PULSE_SINK that no longer existed and the
session's pulsesrc failed with "Internal data stream error".

The sink now belongs to the session: the StopStreamEvent handler deletes
it when the session is removed, and session runners get no audio_sink.
A lobby keeps deleting its sink when its single runner exits.

/resume replaces the StreamSession instance, so it now carries the audio
sink over like the Wayland display and devices; otherwise the resumed
session would never remove it.

This branch has not been deployed

No deployments
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.

1 participant