Skip to content

libobs: Serialize GPU encoder pool teardown with new starts - #736

Merged
zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/gpu-encode-stop-start-race
Oct 14, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/gpu-encode-stop-start-race

Conversation

@chriscoveries

Copy link
Copy Markdown
Contributor

Draft. Opened so upstream PR CI can run it. Our own CI is down, so this hasn't been through our usual CI runs: see our note on #699.

Problem

Stopping the last GPU texture encoder on a video mix can overlap a new encoder starting on that same mix. The old stop can then destroy the pool and worker the new encoder just created, so it stops receiving frames.

Root cause

On obs-rust master 483c0f341 (this branch's base), libobs/obs.c:3273-3297 removes the last encoder, drops gpu_encoder_active at line 3278, waits for the GPU worker at line 3287, and frees the pool at line 3294. A concurrent start can see an empty encoder list and initialize a replacement pool before that stale free. Because obs_reset_video() checks obs_video_active() at libobs/obs.c:1556, dropping the active count before the wait also lets reset pass while stop still uses the mix.

Fix

Add a per-mix GPU encoder lifecycle state and condition variable. Starts pin the mix before waiting for teardown. The last stop marks teardown under the lifecycle mutex, releases that mutex before draining the worker, frees under the graphics and GPU encoder locks, then clears teardown and broadcasts to every waiting start under the lifecycle mutex. It unlocks before dropping the mix pin, and the pin decrement is the final access to the mix. Pool-creation lock order is lifecycle → graphics → GPU encoder mutex. The GPU encoder mutex is not held across the worker drain, so lease callbacks remain able to return frames.

  • Holding the GPU encoder mutex across the drain was rejected because lease callbacks need it.
  • A test-only libobs hook was rejected. The tests block a production texture callback to hold the drain open, and observe the new wait from the test executable.
  • A shared event was replaced with a per-mix condition variable and broadcast because POSIX event signaling wakes only one waiter.

Evidence

Three cases in test_encoder_error_stop cover this change. Each scenario runs in its own child process with a watchdog. The broadcast case has its own result codes (30-40). The race and lifetime cases share two generic codes, 15 (A's stop hung) and 28 (A's stop never removed it from the list), but each has its own distinguishing failure code (19 and 18).

  • last_gpu_encoder_stop_serializes_same_mix_start reproduces the original race, independent of how the fix works. Encoder A is held in its texture callback. The case waits until A's last stop has removed A from the mix's GPU encoder list (read under the mix's gpu_encoder_mutex), so the stop has committed to freeing the pool and to waiting for the worker. B then starts while the test holds the graphics lock, and A is released. B must start, receive 10 more frames after A's end-capture, and finish its own asynchronous stop, each within 5 s, in 3 process-isolated cycles. Without the fix, A's stale free destroys B's new pool (result 19, B gets no frames).
  • gpu_encoder_teardown_wakes_every_waiting_start is specific to the fix and does not reproduce the race. With A's teardown held open as above, B and C start. Both must be observed inside pthread_cond_wait on the same condition variable and mutex, with that mutex then free, before A is released. Both must then return from start, encode 10 more frames each, and stop. The observer is a pthread_cond_wait wrapper exported from the test executable. It counts a wait only on B's or C's thread, and only when the condition variable and mutex are both members of struct obs_core_video_mix. The teardown condition is the only condition variable in that struct, and start_gpu_encode() (C in obs.c in both ENABLE_RUST_LIBOBS modes) is its only waiter. The worker drain's os_event_wait() is not observed, because with Rust ON it is a Rust futex Condvar. Without the fix this case fails with result 30 ("no GPU encoder start waited on the video mix's teardown condition variable"), before A is released. It never runs the stale-pool race.
  • gpu_encoder_stop_keeps_mix_active_during_drain samples obs_video_active() once the last stop has removed its encoder, while the worker is still blocked. Without the fix the mix is already inactive there (result 18).

Mutants that are caught (local runs, Rust OFF, single-edit obs.c mutants; not CI):

  • Broadcast replaced by a single signal: caught in the normal schedule, because the start that is not woken never returns. Four runs failed only the broadcast case, with results 35, 34, 34 and 35; the race case passed each time. This is not an exact notification-count assertion: POSIX permits extra wakes, and nothing observes the broadcast call itself.
  • Condition wait replaced by a spin (unlock, sleep 1 ms, relock): 3 of 3 runs failed only the broadcast case, with result 32, because neither start ever reaches the condition wait. CPU cost is not measured.

Mutants that survive. These cases do not detect them:

  • Predicate while replaced by a one-shot if: passed 5 of 5 runs. Both starts sleep until A's single broadcast, which comes after teardown is cleared, so a one-shot check behaves the same. No early or spurious wake is injected. A future test can kill it without a libobs hook. While A is still held and both waits are admitted, the test locks the mix's lifecycle mutex, broadcasts on its teardown condition and unlocks. It then requires both waiter counts to reach 2 (both starts re-entered the wait) and both starts to still be pending.
  • Pin decrement moved before the final lifecycle unlock: passed 5 of 5 runs. The lifetime sample comes long before that window, B and C hold their own pins at A's final unlock, and no obs_reset_video() is scheduled between the moved decrement and the unlock. Catching it would need a seam to make a reset win that few-instruction window.

Other test limits:

  • gpu_encoder_failure_during_last_stop_finishes still uses one fixed 100 ms sleep before its stop-finished-early check (test/cmocka/test_encoder_error_stop.c:1335). Under load this can only make the check pass vacuously, never fail falsely. It passes on RED and GREEN, so it is not part of the RED evidence.
  • Result 30 is assigned to any start that returns without a recorded wait, including one that failed early for an unrelated reason. On a fixed build, such an environmental start failure would be misreported as "this build has no start-side teardown wait".
  • The RED race reproduction relies on glibc handing the graphics mutex to waiters in FIFO order: B queues on it more than a second before A, and the 1000 ms and 100 ms windows only order that queue. That is robust in practice (6 of 6 local RED repetitions failed with 19) but not guaranteed by POSIX. The lifetime case's RED result 18 does not depend on scheduling.

Commits:

  • RED: 093c1526649110fb911299f165e9789937ae0969 (093c15266) on 483c0f341, tests only (test/cmocka/test_encoder_error_stop.c, test/cmocka/tests.d/test_encoder_error_stop.cmake).
  • GREEN: ce6dde5a5b61e37eb5db80a0dbaca59d96971b3f (ce6dde5a5) on RED, libobs/obs.c and libobs/obs-internal.h only.

Local RED → GREEN on a Linux x86-64 machine, labelled local because CI does not use these flags: GCC 14.2.0, RelWithDebInfo, -Werror -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize-recover=all -fsigned-char, UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1, built with -j2. The Rust OFF runs used ASAN_OPTIONS=detect_leaks=1:abort_on_error=1. The Rust ON runs added intercept_strlen=0, because without it the group probe aborts before any case runs: os_fread_utf8() passes a 3-byte unterminated buffer to the Rust astrcmp_n shim, which calls strlen. That is pre-existing and unrelated to this change. It is fixed separately in fix/dstr-n-bounded-read (PR link pending).

Two consequences for reading any Rust ON + ASan result:

  • With default ASAN_OPTIONS, the probe abort is reported as "no display?", all 8 cases are SKIPPED, and ctest still exits 0. A Rust ON + ASan pass therefore counts only if the log has no SKIPPED lines.
  • intercept_strlen=0 turns off ASan's strlen checking for the whole ON test process, not just that one call. The OFF runs keep full ASan coverage.

RED, Rust OFF, xvfb-run -a ctest -R '^test_encoder_error_stop$' -V --repeat until-pass:3 (one of three identical repetitions):

67: [       OK ] raw_active_replay_buffer_restarts_after_error
67: [       OK ] raw_idle_replay_buffer_starts_after_error
67: [       OK ] texture_error_does_not_deadlock
67: [       OK ] texture_error_idle_replay_buffer_starts
67: GPU encode stop/start race: cycle 1/3 failed: encoder B did not receive frames after encoder A finished end-capture (result 19)
67: [  FAILED  ] last_gpu_encoder_stop_serializes_same_mix_start
67: GPU encode teardown broadcast: no GPU encoder start waited on the video mix's teardown condition variable: a start completed while encoder A's teardown was held (this build has no start-side teardown wait; fix-specific check, not a reproduction of the stop/start race) (result 30)
67: [  FAILED  ] gpu_encoder_teardown_wakes_every_waiting_start
67: GPU encoder mix lifetime: video mix became inactive while its GPU worker was still draining (result 18)
67: [  FAILED  ] gpu_encoder_stop_keeps_mix_active_during_drain
67: [       OK ] gpu_encoder_failure_during_last_stop_finishes
67: [  PASSED  ] 5 test(s).
0% tests passed, 1 tests failed out of 1

RED, Rust ON: the same three failures in all 3 repetitions. The race failed in cycle 2/3 in the first repetition and in cycle 1/3 in the other two:

67: GPU encode stop/start race: cycle 2/3 failed: encoder B did not receive frames after encoder A finished end-capture (result 19)
67: [  FAILED  ] last_gpu_encoder_stop_serializes_same_mix_start
67: GPU encode teardown broadcast: no GPU encoder start waited on the video mix's teardown condition variable: a start completed while encoder A's teardown was held (this build has no start-side teardown wait; fix-specific check, not a reproduction of the stop/start race) (result 30)
67: [  FAILED  ] gpu_encoder_teardown_wakes_every_waiting_start
67: GPU encoder mix lifetime: video mix became inactive while its GPU worker was still draining (result 18)
67: [  FAILED  ] gpu_encoder_stop_keeps_mix_active_during_drain

GREEN, Rust OFF and Rust ON, --repeat until-fail:5: all 5 repetitions passed in each mode, with no skips. Every repetition printed:

67: GPU encode stop/start race: B kept its pool, encoded and stopped in 3/3 cycles
67: [       OK ] last_gpu_encoder_stop_serializes_same_mix_start
67: GPU encode teardown broadcast: B and C were both in the teardown wait (1, 1 waits), then both restarted and encoded frames
67: [       OK ] gpu_encoder_teardown_wakes_every_waiting_start
67: [       OK ] gpu_encoder_stop_keeps_mix_active_during_drain
67: [       OK ] gpu_encoder_failure_during_last_stop_finishes
67: [  PASSED  ] 8 test(s).
100% tests passed, 0 tests failed out of 1

Fork CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.

Compatibility & risk

No public C API or ABI changes. The change serializes GPU texture encoder starts against last-encoder teardown for one mix and keeps that mix active until teardown finishes. Raw encoders and non-GPU output paths are unchanged.

Known, not addressed: if pthread_create() fails in libobs/obs-output.c:2993-2999, end_data_capture_thread() runs inline. Its GPU encoder stop can self-wait on worker inactivity (libobs/obs.c:3350 here; :3287 on master). That was already possible on master. The existing worker-inactive wait is unchanged, and this patch adds no teardown-condition wait to that inline fallback.

Platforms tested: Linux x86-64, Rust OFF and ON. Platforms not tested: Windows. No macOS integration run; the cases are Linux-only (they fork and need xvfb and OpenGL).

How this was tested

  • RED → GREEN: local runs on the exact RED and GREEN blobs, with the flags above. ASan, UBSan, -Werror and intercept_strlen=0 (ON only) are local additions. Command: xvfb-run -a ctest --test-dir <build> -R '^test_encoder_error_stop$' -V --repeat until-pass:3 for RED and --repeat until-fail:5 for GREEN.
  • Fork CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.
  • Real runs: not applicable. The cmocka cases exercise production encoder start, worker callbacks, last-stop drain, asynchronous end-capture, teardown and restart. No full OBS session was run.

Status

Status: draft. Opened so upstream PR CI can run it.


Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.

Add regression tests for the last GPU texture encoder stop on a video mix
racing a new start on the same mix.

last_gpu_encoder_stop_serializes_same_mix_start reproduces the original
bug through public outputs: A's last stop is held in its worker drain
while B starts, and B must keep a working pool, encode frames and stop.

gpu_encoder_teardown_wakes_every_waiting_start is specific to the fix: two
starts must both be in the per-mix teardown condition wait, observed by a
pthread_cond_wait interposer in the test executable, and both must resume.
Without that wait it fails with an explicit no-condvar-wait result.

gpu_encoder_stop_keeps_mix_active_during_drain samples obs_video_active()
once the last stop has removed its encoder.
@zackees
zackees marked this pull request as ready for review October 14, 2026 23:09
@zackees
zackees self-requested a review as a code owner October 14, 2026 23:09
@zackees
zackees merged commit e1427b2 into obs-rust:master Oct 14, 2026
22 checks passed
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.

2 participants