Skip to content

bug: libobs: video stats (fps, frame time, total/lagged frames) raced between graphics and UI threads #175

Closed

Description

@zackees

Found by the first CI run of the sanitizer workflow (run, PR #171; meta issue linked as parent). Upstream OBS code, unchanged in this fork.

Report (TSan, app launch)

WARNING: ThreadSanitizer: data race
  Read of size 4 by main thread:
    #0 obs_get_total_frames          libobs/obs.c:3085
    #1 OBSBasicStats::Update()       frontend/widgets/OBSBasicStats.cpp:394
  Previous write of size 4 by thread T19 'libobs: graphic':
    #0 video_sleep                   libobs/obs-video.c:824   (video->total_frames += count)

WARNING: ThreadSanitizer: data race
  Read of size 8 by main thread:
    #0 obs_get_active_fps                    libobs/obs.c:2897
    #1 OBSBasicStatusBar::UpdateCurrentFPS() frontend/widgets/OBSBasicStatusBar.cpp:232
  Previous write of size 8 by thread T19 'libobs: graphic':
    #0 obs_graphics_thread_loop              libobs/obs-video.c:1146  (obs->video.video_fps = ...)

Class

Data race on plain (non-atomic) shared fields.

Cause

The graphics thread writes obs_core_video's total_frames, lagged_frames (obs-video.c:824-825), video_fps and video_avg_frame_time_ns (obs-video.c:1145-1148) with no lock and no atomics. The public getters obs_get_active_fps, obs_get_average_frame_time_ns, obs_get_total_frames, obs_get_lagged_frames read them from any thread. The frontend's status bar and stats dock poll them from the UI thread, and plugins and obs-websocket (GetStats) call them too.

On x86-64 the aligned loads are effectively atomic, so today the visible effect is at most a stale value. It is still UB, and video_fps (a double) and the 64-bit fields aren't guaranteed tear-free on 32-bit ARM builds.

Fix

Make them volatile-free atomics: os_atomic_* from util/threading.h, or _Atomic/atomic_* for the double via bit-cast. Alternatively, publish a stats snapshot under a small mutex. Keep the getters' ABI unchanged.

Failing test sketch

A Tier 2 cmocka test that starts video with the null graphics module and polls the four getters from a second thread for a few hundred frames. Under TSan it goes RED today.

Activity

  1. added
    bug
    on Oct 11, 2026
  2. zackees commented on Oct 13, 2026

    @zackees
    MemberAuthor

    Claiming: video-stat publication race.

    1. Add a focused RED test that concurrently publishes and reads the four stats under ThreadSanitizer.
    2. Synchronize the graphics-thread writes and public getter reads without changing the C API or ABI; keep the test deterministic enough for the local gate.
    3. Run lint, unit and sanitizer checks, stamp the local gate, and open one PR against master. First PR expected today.
  3. added
    claimed
    on Oct 13, 2026
  4. zackees commented on Oct 13, 2026

    @zackees
    MemberAuthor

    Fixed by PR #439, merged as 7cf6f39. The four graphics-thread stats and the output start/stop frame-count snapshots now use one private mutex; the public getter signatures and exported symbols are unchanged. A concurrent test reported three races under TSan at the RED commit and none after the fix. The local lint and OFF/ON unit gate passed. No remaining work is known for this race.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclaimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions