Skip to content

libobs, plugins: Check os_event_init in the callers that waited on a NULL event - #2188

Merged
zackees merged 7 commits into
obs-rust:masterfrom
rdoupe:fix/event-init-callers
Oct 24, 2026
Merged

zackees merged 7 commits into
obs-rust:masterfrom
rdoupe:fix/event-init-callers

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

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() with wait, and obs_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.
  • ffmpeg-mux's file writer (open_output_file()), which also ignored its mutex and thread results.
  • rtmp-services' ingest refresh wait (ingest-refresh-wait.h).
  • mac-capture: display capture's stream stop event; window capture's two events and its capture thread (pthread_create() was ignored too); ScreenCaptureKit video and audio capture's stream start event.
  • Not in the issue, found by the git grep of every os_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() with wait and obs_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 its param, 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 failed pthread_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.
  • ffmpeg-mux: the four writer inits move into start_io_thread(), which undoes what it set up. On failure the open closes the file and fails with FFM_ERROR, as it does when the file cannot be opened.
  • rtmp-services: without the event, the refresh still runs, but the wait ends at once and the caller goes on with the ingest list it has (the default or cached one).
  • mac-capture: 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). Window capture's create undoes what it set up and returns NULL. Each logs an error.
  • frontend: shows the access prompt but does not wait for the answer, and logs a warning. The permission is then reported as not yet determined.

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):

Caller Result Fixed here
libobs/media-io/audio-io.c.h stop event checked
libobs/obs-output.c.h stopping and reconnect stop events checked
libobs/obs-video-gpu-encode.c.h checked
libobs/obs.c.h hotkey stop and ready events checked
libobs/obs.c.h obs_queue_task() with wait ignored, then used yes
libobs/obs.c.h obs_wait_for_destroy_queue() ignored, then used yes
libobs/util/buffered-file-serializer.c.h (two events, plus mutex and thread) ignored, then used yes
libobs/util/platform-nix-inhibit.c.h create ignored; the NULL event was handled later, but the inhibitor reported success without inhibiting yes
libobs/util/task.c.h checked
frontend/utility/platform-osx.mm.hpp (two access prompts) ignored, then used (uninitialized pointer) yes
plugins/linux-alsa, linux-v4l2 (input, udev) checked
plugins/mac-capture/coreaudio-retry.h, mac-audio.c.h checked
plugins/mac-capture/mac-display-capture.m.h ignored, then used yes
plugins/mac-capture/mac-window-capture.m.h (two events, plus thread) ignored, then used yes
plugins/mac-capture/mac-sck-*-capture.m.h stop events checked (fallback without the event)
plugins/mac-capture/mac-sck-video-capture.m.h start event before an early return without a stream (4 places) ignored but harmless (only destroyed) stale pointer cleared
plugins/mac-capture/mac-sck-{video,audio}-capture.m.h start event before starting the stream ignored, then used yes
plugins/obs-ffmpeg/ffmpeg-mux writer (two events, plus mutex and thread) ignored, then used yes
plugins/obs-ffmpeg hls-mux, mpegts, output, source checked
plugins/obs-outputs/rtmp-stream.c.h (five events) checked
plugins/rtmp-services/service-specific/ingest-refresh-wait.h ignored, then used yes
plugins/vlc-video drain gate, source checked
shared/happy-eyeballs, shared/obs-scripting checked
plugins/obs-browser (submodule) obs_module_load() and BrowserSource::ExecuteOnBrowser() ignored, then used no: separate PR to obs-browser

Not fixed here (reported):

  • obs-browser is a submodule, so its callers need a PR there. obs_module_load() (obs-browser-plugin.cpp) ignores both os_event_init() results. The CEF thread then signals cef_started_event and cef_initialized_event, obs_module_unload() waits on the second, and BrowserSource calls os_event_try(cef_started_event) with no NULL check. BrowserSource::ExecuteOnBrowser() (obs-browser-source.cpp) declares finishedEvent without initializing it, ignores the init, and then the CEF task signals it and the caller waits on it.
  • Other ignored lock inits seen on the way: init_service_data() in rtmp-services ignores pthread_mutex_init(), and so does display_capture_create() in mac-capture.
  • In 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 through pthread_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, for OBS_TASK_DESTROY, then for OBS_TASK_GRAPHICS and OBS_TASK_AUDIO once 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, with bnum_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 requires HAVE_OBS_OPENGL and gets GRAPHICS_MODULE_PATH, working_directory: ${runtime} and a 120 s timeout, as test_source_profiler has.

  • test_ffmpeg_mux_helper: the helper is included with os_event_init wrapped 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

  • RED commit: d6659eb (test only; the runs below were on the same commit before the rebase, cd655de8f on 6b1e25b).
    • libobs (C): 5/11 cases fail. The three wait cases crash (SIGSEGV in the child). The serializer open reports success for all 6 failed inits: the data mutex case returns true, and the other five crash. The inhibitor returns an inhibitor without its stop event.
    • libobs-c-reference: the same 5/11.
    • Hybrid libobs: 1/11 fails. The serializer's 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.
    • Plugin tests, run with the test changes only, on the libobs fix (54b09d6a4): test_ffmpeg_mux_helper crashes (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.
  • GREEN: d6659eb..976ee6b (libobs 4013823, obs-ffmpeg e9887e7, rtmp-services 68be89f, mac-capture ab4ff6d, frontend 17b47a6, docs 976ee6b), rebased onto 2f28dc2. Before the rebase: ec2c41ade on 6b1e25b.
    • test_pthread_init_failures 11/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.
    • LSan (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_helper and test_ingest_refresh_wait on C and hybrid: no leaks.
      • One suppression, leak:gs_create: 128 bytes allocated under pthread_once inside the dlopened Mesa driver, once per child that resets video. The driver is unloaded before LSan reports.
    • RED under LSan (cd655de8f): the same failures on all three. On C and c-ref, LSan also aborts the serializer pthread_create() child with "Joining already joined thread", because the free joins the thread that never started.
    • Focused, on ec2c41ade and again on 976ee6b: 47 tests on each of C, hybrid and c-ref, all pass. These are every cmocka test that uses obs_queue_task, obs_wait_for_destroy_queue, the buffered serializer, the inhibitor, ffmpeg-mux or the ingest wait, plus test_threading and test_task. test_platform_cocoa is not built on Linux.
    • Full ctest on ec2c41ade: libobs (C) 453/453, hybrid 467/467, 0 skipped. Each ran under a private Xvfb with -noreset on a display checked free, with dbus-run-session, a fresh HOME, ulimit -c 0 and 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.
    • Exported symbols: 1766 on libobs, the hybrid libobs and libobs-c-reference, with identical sets (on both heads; libobs-c-reference built explicitly each time, and its mtime checked).
    • Builds: only Linux was built. mac-capture and the frontend's platform-osx.mm.hpp are macOS only, so they were checked by reading and by clang-format; Windows builds were not run.
  • Real app (UI, protocol output or logging changes): N/A. The new log lines appear only on an init failure; the paths where the inits succeed are unchanged.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • 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 (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

Local gate on ec2c41ade: --lane lint passed, --lane cross passed (Rust cross-target checks: no mac-capture or frontend code), and --test-diff origin/master passed (3412 passed, 4 skipped by the nextest profile). On 976ee6b, after the rebase: --test-diff origin/master passed 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.

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
rdoupe force-pushed the fix/event-init-callers branch from 976ee6b to 3eaea16 Compare October 24, 2026 17:05
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into obs-rust:master with commit 8150538 Oct 24, 2026
35 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.

libobs, plugins: Callers ignore os_event_init failures and then use the event

2 participants