Skip to content

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

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/pulse-restart-backoff-2
Oct 16, 2026
Merged

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

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1029

What and why

An Audio Input or Output Capture on PulseAudio or PipeWire now retries a stream restart that fails while its device is there, and no longer restarts a stream that the server keeps failing in a loop. Both gaps are in our #378 (follow-up of #377), found in its review. Reproduced in 0.1.0-beta.20261014.

Cause

In the release (0.1.0-beta.20261014 AppImage, private PipeWire + WirePlumber + pipewire-pulse, an LD_PRELOAD shim that fails the same two libpulse calls as the test):

  • Stream killed and its next 3 creations failed: one "Unable to create stream", then nothing for 5 s. The source records again only after an unrelated device is added.
  • Every connected stream ended once ready: 88 to 94 restarts in the first second and 1323 to 1398 in 15 s on an idle host (19 and 395 while the host was compiling). No "Giving up".

Fix (all in plugins/linux-pulseaudio)

  • 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. 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 (a single failure behaves as before), then after 100 ms each time.
  • Both count in the same restart_attempts. After PULSE_RESTART_MAX_ATTEMPTS (100) the source logs "Giving up restarting ..., waiting for a device to appear", stops the stream and waits for a device event.
  • The counter resets when the stream delivers audio, not 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 retry does nothing if a device event restarted the stream in the meantime.
  • The decisions live in pulse-reconnect.c, which makes no context or stream calls: pulse_start_should_retry() and pulse_stream_failed_should_restart().

Not changed

  • The pause is a fixed 100 ms, like the reconnection path's retries, not an exponential backoff. A server that keeps failing the stream sees about 10 attempts a second for about 10 s, then none until a device appears.
  • pulse_update() (choosing a device in the properties) still starts the stream once and does not retry a synchronous failure.
  • The source shows no failure state in the UI, as before.
  • Only pipewire-pulse was tested, not a PulseAudio server. The asynchronous failure is emulated in-process: the wrapped pa_stream_connect_record ends the stream on the mainloop once it is ready. A real rejection reaches the same state callback. Asking pipewire-pulse to record from a device that does not exist did not fail the stream; it recorded from the default device.

RED → GREEN

  • RED commit: c97c28a test/cmocka: Pulse stream restarts that fail are not retried or limited (RED). test_pulse_stream_restart wraps pa_stream_new_with_proplist and pa_stream_connect_record and gains 3 cases. failed_restart_is_retried and failing_stream_is_paced_then_given_up fail every run, 5/5 in each lane: "'capture A' produced no audio within 3000 ms" and "the failing stream was restarted 52-54 times in 1 s, 15-18 ms apart at the least, expected 100 ms". The 4 cases from linux-pulseaudio: Restart an input whose stream dies while connected #378 and the destroy control pass. (Seen once under load, when the restarts came further apart: "still restarting the failing stream after 16 s (227 attempts)".)
  • GREEN: 3146a80. 7/7 cases of test_pulse_stream_restart with ENABLE_RUST_LIBOBS OFF and ON, 10/10 runs alone in each lane, every case run against the private server (no skips), 25 s per run (the give-up case waits out the 100 retries). test_pulse_reconnect 14/14 (4 new cases for the decision functions; this one needs no server and runs in CI). With the 100 ms pause removed (*delay_usec = 0), failing_stream_is_paced_then_given_up fails ("52 times in 1 s, 16 ms apart"). Full ctest at -j8 (private Xvfb, system libobs-opengl masked, TMPDIR unset): C lane 258/258, Rust lane 258/258. Skips: open_crashes_without_obs_core in both lanes, as on master; in the C lane test_startup_cycle_stress also skipped once under load and passed alone (not touched here). All PipeWire/Pulse tests ran. clang-format 22.1.3, gersemi 0.25.0 and ci/test_registration.py clean.
  • The release case again, on this branch's OBS build (C lane): the failed creations are retried (3 failures, then recording again within 5 s); the failing stream restarts 10 times in the first second, 99 times in all, then "Giving up" once and no more restarts.
  • CI: as with linux-pulseaudio: Restart an input whose stream dies while connected #378, the Tier 2 image has no PipeWire, WirePlumber or pipewire-pulse, so test_pulse_stream_restart skips there (exit 77); test_pulse_reconnect runs.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): none
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not a port

AI note: Claude Code (Claude Opus 5.5) wrote the code, the tests, the commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

@rdoupe
rdoupe requested a review from zackees as a code owner October 16, 2026 07:36
@zackees zackees added the ci-full Run every CI job on this PR, including the release-gating OBS app builds and Flatpak label Oct 16, 2026
@zackees
zackees enabled auto-merge October 16, 2026 07:40
@zackees zackees closed this Oct 16, 2026
auto-merge was automatically disabled October 16, 2026 08:20

Pull request was closed

@zackees zackees reopened this Oct 16, 2026
@zackees
zackees force-pushed the fix/pulse-restart-backoff-2 branch from 3146a80 to 998df8a Compare October 16, 2026 08:20
@zackees
zackees enabled auto-merge October 16, 2026 08:20
zackees added a commit that referenced this pull request Oct 16, 2026
…ng Qt (#1046)

The Fast Gate's "Build Touched Linux Targets" step configured the tree
without Qt, but plugins/frontend-tools is a core module that calls
find_package(Qt6 REQUIRED Widgets). So every PR that touched a plugin failed
at configure (#1031, #1037, #1016). Install the distro Qt packages for that
step so configure passes. Only the touched non-Qt targets are built, and the
Qt build and tests stay in the path-scoped Qt lane.
@zackees zackees closed this Oct 16, 2026
auto-merge was automatically disabled October 16, 2026 08:40

Pull request was closed

@zackees zackees reopened this Oct 16, 2026
zackees added a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 16, 2026
…ix) (obs-rust#1045)

* test: Qt plugins opt out before requiring Qt (RED)

The Fast Gate (obs-rust#1013) configures the touched Linux targets with no Qt.
frontend-tools calls find_package(Qt6 REQUIRED) unconditionally, so that
configure fails on every PR that builds C (obs-rust#1016, obs-rust#1031, obs-rust#1037).

* frontend-tools: Skip the plugin when the frontend is off (GREEN)

frontend-tools is frontend UI and requires Qt. Return before
find_package(Qt6) when ENABLE_FRONTEND is OFF, as the AJA, DeckLink, VST
and websocket plugins do with their own options, so the Fast Gate's
Qt-free configure succeeds.
rdoupe and others added 2 commits October 16, 2026 01:50
…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]>
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
zackees force-pushed the fix/pulse-restart-backoff-2 branch from 998df8a to fa6b7c5 Compare October 16, 2026 08:51
@zackees
zackees enabled auto-merge October 16, 2026 08:51
@zackees
zackees added this pull request to the merge queue Oct 16, 2026
Merged via the queue into obs-rust:master with commit 29bd776 Oct 16, 2026
25 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 16, 2026
…bs-rust#1057)

* test/cmocka: A dropped stream connection leaves its Pulse source silent (RED)

Fails, every run, in both lanes (10 of 10 each):
dropped_connection_restarts ("after the server dropped its stream's
connection: 'capture A' produced no audio within 3000 ms") and
repeated_connection_drops ("after its connection was dropped 1 times:
'capture A' produced no audio within 3000 ms").

Follow-up of obs-rust#532, which gives each capture stream a server connection
of its own. When the server drops only that connection, the stream
fails with its context, and pulse_stream_state_changed() takes the
failed context for the lost shared connection: it leaves the restart
to the reconnection, which never comes because the shared connection
is fine. The source stays silent for good, and nothing is logged.

test_pulse_stream_restart gains five cases:
- the server drops the connection of one source's stream (pw-cli
  destroy on its client): audio must return within 3 s, and the other
  source must not restart;
- the connection is dropped 5 times, then twice in a row before the
  new stream delivers audio: audio returns each time, the source never
  gives up, and no connection is leaked;
- controls, which pass today: a source destroyed while its restart
  after a drop is pending leaks nothing; only the shared connection is
  dropped, and the reconnection restarts each source once; pipewire-
  pulse restarts, and each source is restarted once.

Refs obs-rust#1056.

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

* linux-pulseaudio: Restart a stream whose own connection was dropped

Since obs-rust#532 each capture stream has a server connection of its own
besides the shared one. When the server dropped only a stream's
connection, the stream failed with its context, and
pulse_stream_state_changed() took the failed context for the lost
shared connection: it left the restart to the reconnection, which
never came because the shared connection was fine. The source stayed
silent for good, with nothing in the log.

Check the shared connection instead (pulse_connected()). A stream whose
own connection is gone is restarted like any failed stream, through
the retry, pause and limit of obs-rust#1031. A lost shared connection or a
server restart is still left to the reconnection. If a stream's
failure is seen before the shared connection's, its restart finds no
server and does nothing, and the reconnection replaces the stream as
before, so each source is still restarted once.

test_pulse_stream_restart: 12 of 12, 10 of 10 runs in each lane.

Fixes obs-rust#1056.

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

ci-full Run every CI job on this PR, including the release-gating OBS app builds and Flatpak

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: linux-pulseaudio: a failed stream restart is never retried, and a stream that keeps failing restarts in a loop with no limit (follow-up of #378)

2 participants