Skip to content

linux-pulseaudio: Restart an input whose stream dies while connected - #378

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/10072-pulse-stream-restart
Oct 12, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/10072-pulse-stream-restart

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Description

An Audio Input Capture (or Audio Output Capture) on PulseAudio or PipeWire now records again after its own stream dies while the connection to the sound server stays up. Before, it stayed silent until its settings were changed or OBS was restarted.

  • The record stream had only a read callback and no state callback (pulse-input.c line 347). A chosen device is connected with PA_STREAM_DONT_MOVE. The restart from plugins: Reconnect Linux capture after PipeWire restarts #109 (pulse_reconnected) runs only when the whole server connection is lost and comes back.
  • When the device goes away and comes back under the same name (switching virtual consoles as in the upstream report, a USB replug), pipewire-pulse leaves the stream unlinked: READY, source index 4294967295, no data, and no callback of any kind. PulseAudio kills it instead.
  • When the server kills the stream (its PipeWire node destroyed), it fails with "Entity killed" and nothing restarts it.
  • A device missing when the source is created failed once and was never retried.

The fix, all in plugins/linux-pulseaudio:

  • A stream state callback queues a restart when the stream fails while the context is still ready. If the context itself was lost, the plugins: Reconnect Linux capture after PipeWire restarts #109 path restarts the stream as before.
  • The wrapper subscribes to source events on every ready context (the first one and each reconnect). It passes new and removed sources to the inputs through a callback list like the reconnect one.
  • On the UI thread, pulse_stream_on_event() (new, in pulse-reconnect.c, no context or stream calls) decides what to do:
    • a removed device stops the stream recording from it, and the source waits;
    • a new device starts any input without a running stream, once;
    • a failed stream is restarted once.
  • A device that is gone for good is never polled: its source looks again only when some device appears, one start attempt per event. A stream on the default device is left to the server, which moves it.
  • The state callback is cleared before OBS disconnects a stream itself, so stopping or destroying a source does not trigger a restart.
  1. test/cmocka: Add failing test for a PulseAudio input whose stream dies: test_pulse_stream_restart builds the plugin's capture code into the test and runs it against a private PipeWire, WirePlumber and pipewire-pulse started by test_pulse_monitor_device.sh (own runtime directory, config and D-Bus, no hardware), with virtual sources it loads itself. libobs's source calls are stubbed with --wrap, so it needs no display. Each source counts the frames handed to obs_source_output_audio, and UI tasks run on their own thread. Cases:

    • the device is unloaded and loaded again under the same name. A second microphone must keep its stream (no restart) and its audio;
    • the server destroys the stream's node (pw-cli destroy);
    • the device is loaded only after the source was created;
    • the device is deleted for good (control). There must be no audio, at most one start attempt per device that appears while three unrelated devices come and go, no crash, and no leaked weak references.

    3 of 4 fail: no audio within 3 s after the replug, after the kill, or after the late plug-in. The deleted-device control passes.

  2. linux-pulseaudio: Restart an input whose stream dies while connected: 4 of 4 pass. test_pulse_reconnect gains three cases for pulse_stream_on_event(), which need no sound server: 10 of 10.

Motivation and Context

Fixes #377 (upstream obsproject/obs-studio#10072, reported by foresto).

Why this one: the audio stops with no error in the UI, so the problem often shows up only after a recording or stream has gone out without the microphone. The triggers are routine on Linux: switching consoles, replugging a USB microphone or headset, a driver reload. The only workarounds are reselecting the device or restarting OBS. #109 already covers the full server restart; this covers the per-stream cases it leaves out. No upstream PR exists.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), on current master:

  • test_pulse_stream_restart: 3 of 4 cases fail at the RED commit (same in both lanes) and 4 of 4 pass at GREEN. ctest -V shows every case run against the private server (no SKIPPED lines; about 17 s at RED, 9 s at GREEN).
  • At GREEN, 10 repeated runs: 10 of 10 pass.
  • The test does not need a display; it was run with no DISPLAY or WAYLAND_DISPLAY.
  • test_pulse_reconnect: 10 of 10, including the 3 new decision cases.
  • test_pulse_monitor_device (libobs: Keep PulseAudio monitoring on the chosen device #275), which shares the harness, still passes.
  • Full ctest at GREEN: 125 of 125 in each lane (rebased on current master).
  • clang-format, gersemi and ci/test_registration.py are clean.

CI: the Tier 2 Linux image (rust/tools/linux-validate/Dockerfile) has libpulse-dev but no PipeWire, WirePlumber or pipewire-pulse. So test_pulse_stream_restart skips there (exit 77), as test_pulse_monitor_device already does. The three new test_pulse_reconnect cases run in CI. Adding pipewire pipewire-pulse wireplumber pulseaudio-utils dbus to that image would let both end-to-end tests run; that is left for a separate change.

Not tested / not fixed:

  • The reporter's exact path (a virtual console switch revoking the device through logind) and a real USB replug. Both were emulated by unloading the device and loading it again under the same name. Real ALSA devices may come back with a different name if their profile changes, and then the source keeps waiting, as before.
  • A PulseAudio server (only pipewire-pulse was available). There the stream is killed instead of left unlinked, which the state callback handles; the kill path is covered by the node-destroy case.
  • Desktop audio input fails if screen resolution changes obsproject/obs-studio#8699 (desktop audio lost after a display mode change) may share the mechanism if the HDMI audio device goes away with the display. It was not reproduced. "Default" devices are left to the server, as before.
  • Each failed start still logs "An error occurred while getting the source info!" once per device that appears while the chosen one is missing.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

An Audio Input Capture on PulseAudio or PipeWire stays silent for good
once its own record stream dies while the connection to the server
stays up. Upstream, the reporter switched to another virtual console,
which takes the device away from the session; back on the desktop the
device returned under the same name, but OBS never recorded from it
again until the source's device was changed. Unplugging and replugging
a USB microphone, or the server killing the stream, end the same way.

pulse-input.c connects a chosen device with PA_STREAM_DONT_MOVE and
sets only a read callback: no stream state callback and no
subscription to source events. The restart added for a lost server
connection (pulse_reconnected) only runs when the whole context comes
back. On PipeWire 1.6.8 with pipewire-pulse and WirePlumber 0.5.17:

  - device removed and re-added under the same name: the stream stays
    READY but unlinked (source index 4294967295) and gets no data, with
    no state, moved or suspended callback; PulseAudio kills it instead
  - stream node destroyed: the stream goes FAILED ("Entity killed")
    and nothing restarts it
  - device missing when the source is created: the start fails once and
    is never retried, so a microphone plugged in later stays silent

test_pulse_stream_restart builds the linux-pulseaudio capture code into
the test and runs it against a private PipeWire, WirePlumber and
pipewire-pulse (test_pulse_monitor_device.sh: own runtime dir, config
and D-Bus, no hardware) with virtual sources it loads itself. libobs's
source calls are stubbed with --wrap, so no display is needed: each
source counts the frames handed to obs_source_output_audio, and UI
tasks run on a thread of their own. On master:

  replugged_source_resumes    no audio within 3 s after the replug
  killed_stream_restarts      no audio within 3 s after the kill
  late_source_starts          no audio within 3 s after plugging in
  deleted_source_stays_quiet  passes (control)

3 of 4 cases fail. The controls (a second microphone that is not
touched keeps its stream and its audio; a device deleted for good
causes no retry loop, no audio and no crash) pass. Exits 77 (skip)
without PipeWire, WirePlumber, pipewire-pulse or pactl.

Reported upstream in obsproject/obs-studio#10072 by foresto.

Refs obs-rust#377.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 14:44
An Audio Input Capture stays silent for good once its own record
stream dies while the connection to the server stays up: the server
kills the stream, or the device goes away and comes back under the
same name (switching to another virtual console and back, replugging
a USB microphone). The restart from obs-rust#109 only runs
when the whole server connection comes back.

Watch the stream and the server's devices:

  - a stream state callback queues a restart when the stream fails
    while the context is still ready (with the context lost,
    pulse_reconnected already restarts it)
  - the wrapper subscribes to source events on every ready context and
    passes new and removed sources to the inputs
  - a removed device stops the stream recording from it (pipewire-pulse
    leaves a stream that may not move unlinked and silent; PulseAudio
    kills it), and a new device starts any input that has no running
    stream, which also starts a source whose device was missing when it
    was created

The decision lives in pulse_stream_on_event() (pulse-reconnect.c),
without context or stream calls. Each event leads to at most one start
attempt, so a device that is gone for good is not polled: its source
only looks again when some device appears. A stream on the default
device is left to the server, which moves it.

test_pulse_stream_restart: 3 of 4 failing -> 4/4 against a private
PipeWire 1.6.8, pipewire-pulse and WirePlumber 0.5.17 (10/10 repeated
runs). test_pulse_reconnect gains three cases for the decision, which
run without a sound server: 10/10.

Fixes obs-rust#377.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=b92cfda608c4102ab19ee759b83e3b204db9d58d secs=146 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791816870,"gate":"rust/all/fmt","host":"linux-x86_64","key":"43d368cdc63ffbee96f8f424fb25ff18bca826236b64358a15264b317ff71cb2","lane":"lint","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":52,"stamp":"a6df21afe9b4561baff31ae98399fb4d","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
Ci-Attestation: {"at":1791816870,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"43d368cdc63ffbee96f8f424fb25ff18bca826236b64358a15264b317ff71cb2","lane":"lint","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":52,"stamp":"0781c02615b216aa99ce07842dd0d2f8","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
Ci-Attestation: {"at":1791816870,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"711aaf4d1c9da2312829d055fca4b93e1998e322b173ed0296b9a59a2ce7a175","lane":"rust-test","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":40,"stamp":"e34cb2cc9233bd8acca50ed5b20d7e47","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
Ci-Attestation: {"at":1791816870,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"570801778dbea9a4b654ff807958d72ed42b6138b5a3f597049426543b6541e2","lane":"tier2-linux","parents":["71acac7f7ae8f22917dcb634078dba6979daa11a"],"secs":55,"stamp":"07ef830ec41989672618504fae491db0","tree":"b92cfda608c4102ab19ee759b83e3b204db9d58d","v":1,"via":"run"}
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally (Tier 2 image + pipewire, pipewire-pulse, wireplumber, dbus):

  • RED 71acac7: test_pulse_stream_restart fails 3 of 4 (killed_stream_restarts, late_source_starts, replugged_source_resumes)
  • GREEN 1c97a2c: test_pulse_stream_restart 4/4; test_pulse_reconnect 10/10 (it was 7/7 before; the new cases only add assertions)

The Tier 2 image has no PipeWire, so plain CI skips the end-to-end test. Rebased on master, local gate passed; pushed as 761d041.

@zackees
zackees force-pushed the fix/10072-pulse-stream-restart branch from 1c97a2c to 761d041 Compare October 12, 2026 14:54
@zackees
zackees merged commit b0970bc into obs-rust:master Oct 12, 2026
23 checks passed
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 16, 2026
…ed (RED)

Fails, every run, in both lanes: failed_restart_is_retried ("produced
no audio within 3000 ms") and failing_stream_is_paced_then_given_up
("restarted 52 times in 1 s, 18 ms apart at the least, expected
100 ms"; seen once under load, when the restarts came further apart:
"still restarting the failing stream after 16 s (227 attempts)").

Follow-up of obs-rust#377/obs-rust#378. When a source's own stream fails while the
server stays up, the input restarts it once. If that restart fails
synchronously (the stream cannot be created or connected) while the
device is there and the server is connected, nothing retries it: only
a device appearing would. If the restarted stream keeps failing, the
input restarts it at once every time, with no pause and no limit;
since obs-rust#532 every restart also opens a new server connection.

test_pulse_stream_restart wraps pa_stream_new_with_proplist and
pa_stream_connect_record to make restarts fail on demand, and gains
three cases:
- the stream is killed and creating it fails 3 times while the device
  stays: audio must return within 3 s, with 4 creation attempts;
- every connected stream is ended right after it is ready: restarts at
  least PULSE_RESTART_RETRY_USEC apart, a give-up after at most
  PULSE_RESTART_MAX_ATTEMPTS, no restart after that, and audio again
  once a device appears;
- a source is destroyed while its restart is pending (control; the
  group teardown checks for leaked weak references).

The 4 cases from obs-rust#378 and the control pass.

Refs obs-rust#1029.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 16, 2026
A source restarts its stream when it fails while the server stays up
(obs-rust#378), but:
- if the restart fails synchronously (the stream cannot be created,
  connected or given a valid sample spec) while the device is there and
  the server is connected, nothing retries it until some device
  appears;
- if the server keeps failing the restarted stream, the source restarts
  it at once every time, with no pause and no limit, each time on a new
  server connection (obs-rust#532).

Route both through the restart counter the server reconnection already
uses (pulse_restart_should_retry(), pulse_call_later()):
- pulse_start_recording() now says why it failed: no server, no device,
  or anything else. A restart that fails while the device is there is
  retried every PULSE_RESTART_RETRY_USEC (100 ms). A missing device is
  still waited for (a new device event), so a device that is gone for
  good is not polled, and a lost server is still left to the
  reconnection, which retries missing devices as before.
- A failed stream is restarted at once the first time and after
  PULSE_RESTART_RETRY_USEC after that.
- Both count in the same restart_attempts. After
  PULSE_RESTART_MAX_ATTEMPTS (100) the source logs a warning, stops the
  stream and waits for a device to appear.
- The counter is reset when the stream delivers audio, instead of when
  a start call returns, so a stream that starts and then fails right
  away keeps counting. The read callback runs with the mainloop locked,
  so the UI thread reads and writes the counter under pulse_lock().
- A pending per-stream retry does nothing if a device event restarted
  the stream meanwhile.

The decisions are pulse_start_should_retry() and
pulse_stream_failed_should_restart() in pulse-reconnect.c, which need
no server; test_pulse_reconnect gains four cases for them (14 of 14).
test_pulse_stream_restart: 7 of 7.

Fixes obs-rust#1029.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 16, 2026
…ed (RED)

Fails, every run, in both lanes: failed_restart_is_retried ("produced
no audio within 3000 ms") and failing_stream_is_paced_then_given_up
("restarted 52 times in 1 s, 18 ms apart at the least, expected
100 ms"; seen once under load, when the restarts came further apart:
"still restarting the failing stream after 16 s (227 attempts)").

Follow-up of obs-rust#377/obs-rust#378. When a source's own stream fails while the
server stays up, the input restarts it once. If that restart fails
synchronously (the stream cannot be created or connected) while the
device is there and the server is connected, nothing retries it: only
a device appearing would. If the restarted stream keeps failing, the
input restarts it at once every time, with no pause and no limit;
since obs-rust#532 every restart also opens a new server connection.

test_pulse_stream_restart wraps pa_stream_new_with_proplist and
pa_stream_connect_record to make restarts fail on demand, and gains
three cases:
- the stream is killed and creating it fails 3 times while the device
  stays: audio must return within 3 s, with 4 creation attempts;
- every connected stream is ended right after it is ready: restarts at
  least PULSE_RESTART_RETRY_USEC apart, a give-up after at most
  PULSE_RESTART_MAX_ATTEMPTS, no restart after that, and audio again
  once a device appears;
- a source is destroyed while its restart is pending (control; the
  group teardown checks for leaked weak references).

The 4 cases from obs-rust#378 and the control pass.

Refs obs-rust#1029.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 16, 2026
A source restarts its stream when it fails while the server stays up
(obs-rust#378), but:
- if the restart fails synchronously (the stream cannot be created,
  connected or given a valid sample spec) while the device is there and
  the server is connected, nothing retries it until some device
  appears;
- if the server keeps failing the restarted stream, the source restarts
  it at once every time, with no pause and no limit, each time on a new
  server connection (obs-rust#532).

Route both through the restart counter the server reconnection already
uses (pulse_restart_should_retry(), pulse_call_later()):
- pulse_start_recording() now says why it failed: no server, no device,
  or anything else. A restart that fails while the device is there is
  retried every PULSE_RESTART_RETRY_USEC (100 ms). A missing device is
  still waited for (a new device event), so a device that is gone for
  good is not polled, and a lost server is still left to the
  reconnection, which retries missing devices as before.
- A failed stream is restarted at once the first time and after
  PULSE_RESTART_RETRY_USEC after that.
- Both count in the same restart_attempts. After
  PULSE_RESTART_MAX_ATTEMPTS (100) the source logs a warning, stops the
  stream and waits for a device to appear.
- The counter is reset when the stream delivers audio, instead of when
  a start call returns, so a stream that starts and then fails right
  away keeps counting. The read callback runs with the mainloop locked,
  so the UI thread reads and writes the counter under pulse_lock().
- A pending per-stream retry does nothing if a device event restarted
  the stream meanwhile.

The decisions are pulse_start_should_retry() and
pulse_stream_failed_should_restart() in pulse-reconnect.c, which need
no server; test_pulse_reconnect gains four cases for them (14 of 14).
test_pulse_stream_restart: 7 of 7.

Fixes obs-rust#1029.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 16, 2026
…it (obs-rust#1031)

* test/cmocka: Pulse stream restarts that fail are not retried or limited (RED)

Fails, every run, in both lanes: failed_restart_is_retried ("produced
no audio within 3000 ms") and failing_stream_is_paced_then_given_up
("restarted 52 times in 1 s, 18 ms apart at the least, expected
100 ms"; seen once under load, when the restarts came further apart:
"still restarting the failing stream after 16 s (227 attempts)").

Follow-up of obs-rust#377/obs-rust#378. When a source's own stream fails while the
server stays up, the input restarts it once. If that restart fails
synchronously (the stream cannot be created or connected) while the
device is there and the server is connected, nothing retries it: only
a device appearing would. If the restarted stream keeps failing, the
input restarts it at once every time, with no pause and no limit;
since obs-rust#532 every restart also opens a new server connection.

test_pulse_stream_restart wraps pa_stream_new_with_proplist and
pa_stream_connect_record to make restarts fail on demand, and gains
three cases:
- the stream is killed and creating it fails 3 times while the device
  stays: audio must return within 3 s, with 4 creation attempts;
- every connected stream is ended right after it is ready: restarts at
  least PULSE_RESTART_RETRY_USEC apart, a give-up after at most
  PULSE_RESTART_MAX_ATTEMPTS, no restart after that, and audio again
  once a device appears;
- a source is destroyed while its restart is pending (control; the
  group teardown checks for leaked weak references).

The 4 cases from obs-rust#378 and the control pass.

Refs obs-rust#1029.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* linux-pulseaudio: Retry failed stream restarts with a pause and a limit

A source restarts its stream when it fails while the server stays up
(obs-rust#378), but:
- if the restart fails synchronously (the stream cannot be created,
  connected or given a valid sample spec) while the device is there and
  the server is connected, nothing retries it until some device
  appears;
- if the server keeps failing the restarted stream, the source restarts
  it at once every time, with no pause and no limit, each time on a new
  server connection (obs-rust#532).

Route both through the restart counter the server reconnection already
uses (pulse_restart_should_retry(), pulse_call_later()):
- pulse_start_recording() now says why it failed: no server, no device,
  or anything else. A restart that fails while the device is there is
  retried every PULSE_RESTART_RETRY_USEC (100 ms). A missing device is
  still waited for (a new device event), so a device that is gone for
  good is not polled, and a lost server is still left to the
  reconnection, which retries missing devices as before.
- A failed stream is restarted at once the first time and after
  PULSE_RESTART_RETRY_USEC after that.
- Both count in the same restart_attempts. After
  PULSE_RESTART_MAX_ATTEMPTS (100) the source logs a warning, stops the
  stream and waits for a device to appear.
- The counter is reset when the stream delivers audio, instead of when
  a start call returns, so a stream that starts and then fails right
  away keeps counting. The read callback runs with the mainloop locked,
  so the UI thread reads and writes the counter under pulse_lock().
- A pending per-stream retry does nothing if a device event restarted
  the stream meanwhile.

The decisions are pulse_start_should_retry() and
pulse_stream_failed_should_restart() in pulse-reconnect.c, which need
no server; test_pulse_reconnect gains four cases for them (14 of 14).
test_pulse_stream_restart: 7 of 7.

Fixes obs-rust#1029.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: linux-pulseaudio: an audio input never restarts once its own stream dies while the server stays up (upstream #10072)

2 participants