Repository navigation
libobs: Serialize GPU encoder pool teardown with new starts - #736
Merged
zackees merged 2 commits intoOct 14, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-3297removes the last encoder, dropsgpu_encoder_activeat 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. Becauseobs_reset_video()checksobs_video_active()atlibobs/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.
Evidence
Three cases in
test_encoder_error_stopcover 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_startreproduces 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'sgpu_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_startis 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 insidepthread_cond_waiton 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 apthread_cond_waitwrapper 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 ofstruct obs_core_video_mix. The teardown condition is the only condition variable in that struct, andstart_gpu_encode()(C inobs.cin bothENABLE_RUST_LIBOBSmodes) is its only waiter. The worker drain'sos_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_drainsamplesobs_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.cmutants; not CI):Mutants that survive. These cases do not detect them:
whilereplaced by a one-shotif: 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.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_finishesstill 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.Commits:
093c1526649110fb911299f165e9789937ae0969(093c15266) on483c0f341, tests only (test/cmocka/test_encoder_error_stop.c,test/cmocka/tests.d/test_encoder_error_stop.cmake).ce6dde5a5b61e37eb5db80a0dbaca59d96971b3f(ce6dde5a5) on RED,libobs/obs.candlibobs/obs-internal.honly.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 usedASAN_OPTIONS=detect_leaks=1:abort_on_error=1. The Rust ON runs addedintercept_strlen=0, because without it the group probe aborts before any case runs:os_fread_utf8()passes a 3-byte unterminated buffer to the Rustastrcmp_nshim, which callsstrlen. That is pre-existing and unrelated to this change. It is fixed separately infix/dstr-n-bounded-read(PR link pending).Two consequences for reading any Rust ON + ASan result:
ASAN_OPTIONS, the probe abort is reported as "no display?", all 8 cases areSKIPPED, and ctest still exits 0. A Rust ON + ASan pass therefore counts only if the log has noSKIPPEDlines.intercept_strlen=0turns off ASan'sstrlenchecking 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):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:
GREEN, Rust OFF and Rust ON,
--repeat until-fail:5: all 5 repetitions passed in each mode, with no skips. Every repetition printed: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 inlibobs/obs-output.c:2993-2999,end_data_capture_thread()runs inline. Its GPU encoder stop can self-wait on worker inactivity (libobs/obs.c:3350here;:3287on 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
-Werrorandintercept_strlen=0(ON only) are local additions. Command:xvfb-run -a ctest --test-dir <build> -R '^test_encoder_error_stop$' -V --repeat until-pass:3for RED and--repeat until-fail:5for GREEN.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.