Repository navigation
libobs, plugins: Check os_event_init in the callers that waited on a NULL event - #2188
Merged
Merged
Conversation
Since a failed os_event_init() leaves the event pointer untouched, these libobs callers that ignore its result go on with a NULL event: - obs_queue_task() with wait waits on it, and the task thread signals it; - obs_wait_for_destroy_queue() does the same with the graphics and audio threads; - buffered_file_serializer_init() also ignores its mutex and thread results, reports success, and its I/O thread waits on the NULL event; - os_inhibit_sleep_create() returns an inhibitor without its stop event, which then reports success without doing anything. test_pthread_init_failures fails each of those inits through its interposers. The cases that can crash run in a forked child each, which starts OBS itself. They check that the call fails cleanly or, for the waits, still runs the task once on its thread and waits for it, and that nothing leaks. The wait cases need video and audio, so the test now loads the OpenGL module like test_source_profiler. The hybrid libobs's Rust events make no pthread init, so those cases cannot fail there; its Rust serializer does start its thread through pthread_create(). Refs obs-rust#2164
A failed os_event_init() leaves the event pointer untouched, NULL in all of these, and each went on to wait on or signal it. obs_queue_task() with wait and obs_wait_for_destroy_queue() now wait without the event: the task thread sets a flag instead, and the caller polls it every millisecond. The task still runs exactly once, on the thread it was queued to, and the call still returns only after it ran. Running it inline on the caller's thread would break that thread's guarantee (a graphics task outside the graphics thread, an audio task racing the audio thread), and returning without waiting would either drop the task or let it run after the caller's param, often on its stack, is gone. A warning is logged. buffered_file_serializer_init() also ignored its data mutex and I/O thread results. It now fails the open on any of the four, undoes what it set up, closes the file and logs an error. The hybrid's Rust serializer aborted (a panic through the FFI) when its thread did not start; it now fails the open the same way. os_inhibit_sleep_create() without a portal or session D-Bus inhibitor returned an inhibitor without its stop event, which then reported success without doing anything. It now returns NULL, as it already does in a Flatpak without the portal, and logs an error. The paths where the inits succeed are unchanged. Refs obs-rust#2164
…thread cannot be set up open_output_file() ignored the results of the writer's mutex, two event and thread inits. With an event that could not be created, the writer thread waited on or signalled a NULL event, and the free joined a thread that might never have started. The four are now set up together in start_io_thread(), which undoes what it set up when one fails. The open then closes the file and fails the recording with FFM_ERROR, the way it does when the file cannot be opened. test_ffmpeg_mux_helper fails each of the two event inits through a wrapper and checks that the open fails without starting the writer. Refs obs-rust#2164
…be created ingest_refresh_wait_init() ignored os_event_init(), and the refresh then reset and signalled the NULL event and the caller waited on it. Without the event, the refresh still runs, but there is nothing to wait on: the wait ends at once and the caller goes on with the ingest list it has, the default or the cached one. test_ingest_refresh_wait checks that starting, ending and waiting on a refresh without the event neither crashes nor waits. Refs obs-rust#2164
Four places ignored os_event_init() and then used the event: - display capture's stream stop event, which the stream's stopped callback signals and the stream teardown waits on; - window capture's capture and stop events, and its capture thread, whose pthread_create() result was ignored too: the destroy then signalled NULL events and joined a thread that never started; - ScreenCaptureKit video and audio capture's stream start event, which the start handler signals and the init waits on. Each now fails: display capture and ScreenCaptureKit capture do not start the stream (the create fails, an update leaves the source without a stream, as when its display is gone), and window capture's create undoes what it set up and returns NULL. Each logs an error. The stream teardowns also now clear the event they destroy. An init that returned before making a new event (display capture with its display gone, ScreenCaptureKit with no audio configured or a stream output that could not be added) left the destroyed pointer behind, and the next teardown destroyed it again; a failed event init now leaves it NULL. The ScreenCaptureKit inits that make the event only to return early without a stream are unchanged: that event is only destroyed. macOS only: built and run on no machine here. Refs obs-rust#2164
… fails CheckPermissionWithPrompt() ignored os_event_init() for the event it waits on until the access prompt is answered. The completion handler then signalled, and the caller waited on, an uninitialized pointer. Without the event, it now shows the prompt but does not wait for the answer, and logs a warning: the permission is reported as not yet determined, as it is while the prompt is open. macOS only: built and run on no machine here. Refs obs-rust#2164
rdoupe
force-pushed
the
fix/event-init-callers
branch
from
October 24, 2026 17:05
976ee6b to
3eaea16
Compare
6 of 7 tasks
This was referenced Oct 24, 2026
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 #2164
What and why
Since #2163, a failed
os_event_init()returns its error and leaves the event pointer as it was. These callers ignored the result and then waited on or signalled the event, so a failed init (resource exhaustion; it does not happen on glibc with default attributes) would crash them:obs_queue_task()withwait, andobs_wait_for_destroy_queue()(obs.c.h): the caller waited on the NULL event and the task thread signalled it.buffered_file_serializer_init()(util/buffered-file-serializer.c.h), which also ignored its data mutex and I/O thread results: it reported success, its I/O thread waited on the NULL event, and the free joined a thread that might never have started.os_inhibit_sleep_create()(util/platform-nix-inhibit.c.h, the fallback without a portal or session D-Bus inhibitor): no crash, but it returned an inhibitor without its stop event, which then reported success without inhibiting anything.open_output_file()), which also ignored its mutex and thread results.ingest-refresh-wait.h).pthread_create()was ignored too); ScreenCaptureKit video and audio capture's stream start event.git grepof everyos_event_init(caller: the macOS camera and microphone access prompt in the frontend (frontend/utility/platform-osx.mm.hpp), which waited on an uninitialized pointer.What each does now:
obs_queue_task()withwaitandobs_wait_for_destroy_queue(): without the event, the task thread sets a flag in the (stack) wait info instead of signalling, and the caller polls it every millisecond. A warning is logged. The task still runs exactly once, on the thread it was queued to, and the call still returns only after it ran. The two other choices break the contract. Running the task inline on the caller's thread would run a graphics task outside the graphics thread, or an audio task concurrently with the audio thread. Returning without waiting would either drop the task, or let it run after the caller returned and itsparam, which a waiting caller usually keeps on its stack, is gone.buffered_file_serializer_init(): fails the open on any of its four inits, undoes what it set up, closes the file and logs an error. The hybrid's Rust serializer called.expect()on its thread spawn, so a failedpthread_create()aborted the process (a panic through the FFI). It now fails the open the same way.os_inhibit_sleep_create(): returns NULL and logs an error, as it already returns NULL in a Flatpak without the portal. Its callers handle NULL.start_io_thread(), which undoes what it set up. On failure the open closes the file and fails withFFM_ERROR, as it does when the file cannot be opened.Every path where the inits succeed is unchanged.
Second bug, found while fixing mac-capture (fixed here): the display capture and ScreenCaptureKit stream teardowns destroyed their event without clearing the pointer. An update re-runs teardown then init. An init that returned before making a new event (display capture whose display is gone; ScreenCaptureKit with no audio configured, or a stream output that could not be added) left the freed pointer behind, and the next teardown or the destroy freed it again. This needs no init failure. The teardowns now clear the pointer, which also makes a failed event init leave it NULL.
Classification of every
os_event_init(call on master (product code; test code not listed):libobs/media-io/audio-io.c.hstop eventlibobs/obs-output.c.hstopping and reconnect stop eventslibobs/obs-video-gpu-encode.c.hlibobs/obs.c.hhotkey stop and ready eventslibobs/obs.c.hobs_queue_task()with waitlibobs/obs.c.hobs_wait_for_destroy_queue()libobs/util/buffered-file-serializer.c.h(two events, plus mutex and thread)libobs/util/platform-nix-inhibit.c.hcreatelibobs/util/task.c.hfrontend/utility/platform-osx.mm.hpp(two access prompts)plugins/linux-alsa,linux-v4l2(input, udev)plugins/mac-capture/coreaudio-retry.h,mac-audio.c.hplugins/mac-capture/mac-display-capture.m.hplugins/mac-capture/mac-window-capture.m.h(two events, plus thread)plugins/mac-capture/mac-sck-*-capture.m.hstop eventsplugins/mac-capture/mac-sck-video-capture.m.hstart event before an early return without a stream (4 places)plugins/mac-capture/mac-sck-{video,audio}-capture.m.hstart event before starting the streamplugins/obs-ffmpeg/ffmpeg-muxwriter (two events, plus mutex and thread)plugins/obs-ffmpeghls-mux, mpegts, output, sourceplugins/obs-outputs/rtmp-stream.c.h(five events)plugins/rtmp-services/service-specific/ingest-refresh-wait.hplugins/vlc-videodrain gate, sourceshared/happy-eyeballs,shared/obs-scriptingplugins/obs-browser(submodule)obs_module_load()andBrowserSource::ExecuteOnBrowser()Not fixed here (reported):
obs_module_load()(obs-browser-plugin.cpp) ignores bothos_event_init()results. The CEF thread then signalscef_started_eventandcef_initialized_event,obs_module_unload()waits on the second, andBrowserSourcecallsos_event_try(cef_started_event)with no NULL check.BrowserSource::ExecuteOnBrowser()(obs-browser-source.cpp) declaresfinishedEventwithout initializing it, ignores the init, and then the CEF task signals it and the caller waits on it.init_service_data()in rtmp-services ignorespthread_mutex_init(), and so doesdisplay_capture_create()in mac-capture.init_screen_stream()(ScreenCaptureKit video), a stream output that cannot be added returns!did_add_output, which is true. The create then succeeds with a stream that was never started. This is upstream code.Platforms: the libobs and rtmp-services changes apply everywhere, and ffmpeg-mux's writer is used on every platform.
os_inhibit_sleep_create()is Linux and FreeBSD. mac-capture and the frontend change are macOS only.Hybrid libobs: its Rust
os_event_init()uses no pthread init, so it cannot fail this way. For those cases the test checks that the call still works.obs_queue_task(),obs_wait_for_destroy_queue()and ffmpeg-mux are C on all three builds. The serializer and the inhibitor are Rust ports in the hybrid. The Rust serializer's thread start does go throughpthread_create(), and that case failed on the hybrid in RED. The Rust serializer does not log the failure. It logs none of its errors, which is how the port already worked.Tests:
test_pthread_init_failures(libobs: Check pthread init and thread start results with != 0 #2163) gets 5 new cases, through its existing interposers:obs_queue_task(..., true)with the wait event's mutex init failing, forOBS_TASK_DESTROY, then forOBS_TASK_GRAPHICSandOBS_TASK_AUDIOonce video and audio are reset. The task sleeps 100 ms first. It must have run exactly once, on its own thread, when the call returns, and must not run again later. For the destroy queue,bnum_allocs()must be unchanged.obs_wait_for_destroy_queue()with its event's mutex init failing. A graphics task and an audio task queued just before it (each sleeping 100 ms) must have run when it returns.buffered_file_serializer_init(): one working open counts the inits it makes (C: 3 mutex, 2 cond, 1 thread; hybrid: 0, 0, 1). Then each one is failed in turn: the open must return false and leave the serializer untouched, withbnum_allocs()and the open file descriptor count unchanged.os_inhibit_sleep_create()with no session bus and its stop event's cond init failing: it must return NULL and allocate nothing.The first three run in a forked child per case, so a crash fails that case alone. Each child starts OBS itself, and the parent starts no thread before it forks. Children end with
exit(), so a leak checker loaded into the test checks them too. These cases run first. The video and audio cases load the OpenGL module, so the test's graph entry now requiresHAVE_OBS_OPENGLand getsGRAPHICS_MODULE_PATH,working_directory: ${runtime}and a 120 s timeout, astest_source_profilerhas.test_ffmpeg_mux_helper: the helper is included withos_event_initwrapped by a macro that can fail the Nth call. The new case fails each of the writer's two events in turn. The open must fail without starting the writer and stop at that init.test_ingest_refresh_wait: a wait whose event is NULL. Reset, complete and wait must not crash, and the wait must return false at once.RED → GREEN
pthread_create()case aborts (SIGABRT, the Rust.expect()). The queue, destroy queue, inhibitor and serializer lock cases report that the Rust event makes no pthread init and pass.test_ffmpeg_mux_helpercrashes (SIGSEGV) on C and c-ref. On the hybrid it fails its check: the Rust event does not crash on NULL, and both inits run although the first failed.test_ingest_refresh_wait's new case segfaults on C and c-ref and passes on the hybrid, whose event functions accept NULL.test_pthread_init_failures11/11 on libobs, the hybrid libobs and libobs-c-reference, on both heads. On C and c-ref the log shows each fallback taken: 3×obs_queue_task: Failed to create the wait event, polling for the task instead, 1× the destroy-queue warning, 6×Failed to start the buffered writer, 1× the inhibitor error.LD_PRELOAD=liblsan.so,fast_unwind_on_malloc=0, on ec2c41ade):test_pthread_init_failures: no leaks on any of the three. The forked children are checked too.test_ffmpeg_mux_helperandtest_ingest_refresh_waiton C and hybrid: no leaks.leak:gs_create: 128 bytes allocated underpthread_onceinside the dlopened Mesa driver, once per child that resets video. The driver is unloaded before LSan reports.pthread_create()child with "Joining already joined thread", because the free joins the thread that never started.obs_queue_task,obs_wait_for_destroy_queue, the buffered serializer, the inhibitor, ffmpeg-mux or the ingest wait, plustest_threadingandtest_task.test_platform_cocoais not built on Linux.-noreseton a display checked free, withdbus-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 (test_deinterlace_2x_field_order_*) and Flaky: real-time audio cmocka tests lose audio under machine load (test_vlc_playlist_gap, test_group_hide_transition_audio) #838 (test_vlc_playlist_gap,test_group_hide_transition_audio) passed.libobs-c-referencebuilt explicitly each time, and its mtime checked).platform-osx.mm.hppare macOS only, so they were checked by reading and by clang-format; Windows builds were not run.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 ec2c41ade:
--lane lintpassed,--lane crosspassed (Rust cross-target checks: no mac-capture or frontend code), and--test-diff origin/masterpassed (3412 passed, 4 skipped by the nextest profile). On 976ee6b, after the rebase:--test-diff origin/masterpassed again (3490 passed, 4 skipped), and clang-format passes on every changed C, C++ and Objective-C file.Rebased onto master (head
3eaea1689) to pick up #2186, which fixed the master macOS Tier 2 break from #2170 that failed this PR's macOS job. The rebase was clean and master's new commits touch only the frontend, so the earlier runs above (on the pre-rebase SHAs) still hold.