Repository navigation
libobs: Check pthread init and thread start results with != 0 - #2163
Merged
Merged
Conversation
pthread functions return 0 or a positive error number. libobs checked some of them with "< 0", so these failures were never caught: - os_event_init() went on after a failed pthread_mutex_init() or pthread_cond_init() and returned 0 with an event whose lock was not initialized; - obs_context_data_init() went on after a failed init of the rename cache mutex, so the object was created around it; - obs_output_create() checks os_event_init() for its reconnect event with "< 0", so once that reports the failure, the output would be created without the event; - output_reconnect() and os_inhibit_sleep_set_active() checked pthread_create() with "< 0": the output stayed "reconnecting" with no thread, and the inhibitor reported success and would later join a thread that never started. test_pthread_init_failures interposes pthread_mutex_init(), pthread_cond_init(), pthread_mutex_destroy() and pthread_create() for its own thread, fails exactly the call it targets, and checks the return, the out-pointer and bnum_allocs(). Linux only. The hybrid libobs's Rust os_event_init() uses no pthread init, so its event cases check that the event still works; its Rust inhibitor already reports a failed thread start. Refs obs-rust#2152
pthread functions return 0 or a positive error number, so the "< 0" checks never caught a failure. os_event_init(), obs_context_data_init() (rename cache mutex), obs_output_create() (reconnect event, now that os_event_init() reports the failure), output_reconnect() and os_inhibit_sleep_set_active() (pthread_create) now check "!= 0". The error paths that follow are unchanged. A source whose context fails to initialize then reaches obs_source_destroy() with a context that was never added anywhere: its zeroed hash handle made obs_context_data_remove_name() dereference or corrupt the public source table, and the deferred destroy locked its NULL list mutex in obs_context_wait(). Both now skip a context that was never added. This path was already reachable when the source's signal handler failed to initialize. Fixes obs-rust#2152
This was referenced Oct 24, 2026
zackees
added a commit
to obs-rust/obs-browser
that referenced
this pull request
Oct 25, 2026
Since obs-rust/obs-studio#2163 a failed os_event_init() leaves the event NULL. obs_module_load() now fails cleanly if either startup event cannot be created, and ExecuteOnBrowser() returns without queueing a task that would signal a missing event. Fixes #4
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.
Fixes #2152
What and why
pthread functions return 0 or a positive error number, never a negative one. libobs checked some of them, and one
os_event_init()result that carries their error, with< 0, so these failures were never caught:os_event_init()(util/threading-posix.c.h): a failedpthread_mutex_init()orpthread_cond_init()was ignored, and it returned 0 with an event whose lock was not initialized.obs_context_data_init_wrap()(obs.c.h): a failed init of the rename cache mutex was ignored, so the source, output, encoder, service or canvas was created around it.The same-pattern survey of libobs, plugins, frontend, shared and deps found three more in libobs, fixed here too:
obs_output_create()(obs-output.c.h) checksos_event_init()for its reconnect stop event with< 0. Onceos_event_init()reports the failure, this check would let the output be created without that event. The test shows it: with only the two fixes above,obs_output_create()returned an output whosereconnect_stop_eventis NULL.output_reconnect()(obs-output.c.h) checkspthread_create()with< 0. When the reconnect thread did not start, the output stayed "reconnecting" with no thread.os_inhibit_sleep_set_active()(util/platform-nix-inhibit.c.h, the fallback without a portal or session D-Bus inhibitor) checkspthread_create()with< 0. It reported success with no thread, and deactivating it later would join a thread that never started.All five now check
!= 0. The error paths that follow are unchanged.Second bug, found by the test: with the rename cache check fixed,
obs_source_create()crashed instead of returning NULL.obs_context_data_init()frees and zeroes the context on failure, andobs_source_create_internal()then callsobs_source_destroy()on a context that was never added anywhere:obs_context_data_remove_name()ranHASH_DELETEwith the context's zeroed hash handle. With no public sources it dereferenced NULL. With some, it would have freed the public source table.obs_context_wait(), which locked the context's list mutex pointer. That pointer is NULL because the context was never inserted.Both now return early for a context that was never added (
hh.tbl/mutexstill NULL). This path was already reachable before this PR, when a source's signal handler failed to initialize.Platforms: the rename cache, output and context fixes apply everywhere (w32-pthreads also returns positive error numbers).
os_event_init()and the inhibitor are POSIX and Linux code. On glibc these inits practically never fail, so this is a robustness fix for resource exhaustion. Upstream has the same code.Hybrid libobs: its
os_event_init()(rust/obs-util/src/ffi/threading.rs,platform/unix/event.rs) is built onstd::sync::{Mutex, Condvar}and calls no pthread init, so it cannot fail this way. The RED expectations for the event cases cannot fail on the hybrid. There the test checks that the event still works and that nothing leaks, and it passes. The hybrid's Rust inhibitor already reported a failedstd::threadstart (that goes throughpthread_create()), so that case passes on the hybrid in RED as well. The context, output reconnect event and reconnect thread code is C on all three builds.Test:
test_pthread_init_failures(new, Linux only). It interposespthread_mutex_init(),pthread_cond_init(),pthread_mutex_destroy()andpthread_create(), which the executable exports throughlink_options: -Wl,--export-dynamic-symbol=...astest_canvas_flagsdoes. Each interposer fails the Nth call on the test's own thread and records the object it was given, so each case fails exactly the init it targets:os_event_init()(auto and manual) with the mutex init failing, then with the cond init failing: non-zero return, out-pointer unchanged,bnum_allocs()unchanged. On the cond failure, the mutex that was initialized is the one destroyed.obs_source_create()andobs_source_create_private()of a registered test input type, with only the first mutex init failing. A successful create first checks that this init is&source->context.rename_cache_mutex. The create must return NULL and allocate nothing.obs_output_create()with only its lastpthread_cond_init()failing. A successful create first checks that this init belongs tooutput->reconnect_stop_event. The create must return NULL and allocate nothing.obs_output_signal_stop(OBS_OUTPUT_DISCONNECTED)withpthread_create()failing: the output must not stay reconnecting.os_inhibit_sleep_set_active(true)withpthread_create()failing must return false. This case runs first, withDBUS_SESSION_BUS_ADDRESSpointed at nothing while the inhibitor is created, so the inhibitor takes the thread fallback. A failed bus connection is not cached.It is a new test rather than part of
test_threading.test_threadingis cross-platform and runs withoutobs_startup(), while these interposers are Linux-only, replace the symbols for the whole executable, and needobs_startup()(a display) for the context cases. libobs and libobs-c-reference call the interposed functions through the PLT. The interposers reached them on all three builds: each case checks its call count.RED → GREEN
0 == 0) for both the mutex and the cond failure. The source and output creates return an object (created is not false). The output stays reconnecting (reconnecting is not false). The inhibitor reports success without its thread.!= 0changes: the output event case failed untilobs-output.c.h:240changed. With that change, the source case segfaulted inobs_context_data_remove_name()and then inobs_context_wait(), which led to the second fix.test_pthread_init_failurespasses 6/6 on libobs, the hybrid libobs and libobs-c-reference.LD_PRELOAD=liblsan.so,fast_unwind_on_malloc=0) ontest_pthread_init_failures: RED on C and c-ref reports 598 bytes in 3 allocations. These are the inhibitor that the RED case deliberately leaves alive, because deactivating it would join a thread that never existed. The hybrid reports none. GREEN, on 52f4270a7 and again on 73d16b9: no leaks on any of the three.os_event_*(49 built per lane), on all three builds. 39 pass and 10 PipeWire tests exit 77 (skip, no PipeWire daemon) on each. Nothing fails. On 73d16b9:test_pthread_init_failures,test_threading,test_task,test_canvas_flags,test_audio_io,test_video_io,test_output_failed_start_encodersandtest_output_start_twiceall pass on all three.-noresetanddbus-run-session, a fresh HOME,ulimit -c 0and TMPDIR unset. The known flakes (test_deinterlace_2x_field_order is timing-sensitive (yadif 2x 60i into 60): startup underrun and load-induced hold #456, Flaky: real-time audio cmocka tests lose audio under machine load (test_vlc_playlist_gap, test_group_hide_transition_audio) #838) passed.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVCLocal gate on 52f4270a7:
--test-diff origin/master(3362 passed, 4 skipped by the nextest profile),--lane lintand--lane crosspassed. On 73d16b9, after the rebase:--test-diff origin/masterpassed again (3362 passed, 4 skipped), and so did clang-format.Not fixed here (reported): several callers ignore the result of
os_event_init()and then use the event:obs_queue_task()withwaitandobs_wait_for_destroy_queue()(obs.c.h);buffered-file-serializer.c.h, which also ignorespthread_mutex_init()andpthread_create();mac-capture(several),obs-browser,ffmpeg-muxandrtmp-services' ingest refresh wait.Before this PR, a failed init there gave a zeroed lock, which happens to work on glibc. Now the event pointer stays NULL, and the first wait would crash. These inits do not fail on glibc with default attributes. Checking those callers is a separate change.
os_sem_wait(...) < 0inshared/media-playbackis correct:os_sem_wait()returns -1 on failure.