Skip to content

TSan: audio thread reads source audio_ts / audio_input_buf without audio_buf_mutex #130

Closed

Description

@zackees

Found by the TSan launch pass (meta issue linked as parent). Upstream OBS code, unchanged in this fork.

Race

WARNING: ThreadSanitizer: data race
  Write of size 8 by thread T49 'threaded-ml' (PulseAudio capture, holding the source's audio_buf_mutex):
    #0 reset_audio_data               libobs/obs-source.c:1505   (source->audio_ts = os_time)
    #1 source_output_audio_place      libobs/obs-source.c:1553
    #3 obs_source_output_audio        libobs/obs-source.c:4100
  Previous read of size 8 by thread T8 'audio-io: audio':
    #0 obs_source_audio_render        libobs/obs-source.c:5512   (if (!source->audio_ts))
    #1 audio_callback                 libobs/obs-audio.c:624

WARNING: ThreadSanitizer: data race
  Write of size 8 by thread T49 (holding audio_buf_mutex):
    #0 deque_push_back                libobs/util/deque.h:147
    #1 source_output_audio_push_back  libobs/obs-source.c:1587
  Previous read of size 8 by thread T8 (holding only obs->data.audio_sources_mutex):
    #0 audio_buffer_insufficient      libobs/obs-audio.c:443     (source->audio_input_buf[0].size)
    #1 mark_invalid_sources           libobs/obs-audio.c:472

Cause

Capture threads update audio_ts and audio_input_buf[] while holding source->audio_buf_mutex. The audio thread reads both in obs_source_audio_render and audio_buffer_insufficient without taking that mutex. It holds only audio_sources_mutex, which the capture thread never takes. These are real, unsynchronized 64-bit accesses. On x86-64 they are usually torn-free, but they're still UB and can give inconsistent size/audio_ts pairs when a timestamp jump resets the buffer mid-mix.

Repro

TSan build (-fsanitize=thread), headless launch with the default Desktop Audio/Mic sources under PulseAudio/PipeWire. Both races fire within seconds.

Fix direction

Read these fields under audio_buf_mutex, or make audio_ts atomic and snapshot the buffer size under the lock. Add a Tier 1/2 test that drives obs_source_output_audio from one thread against the mix loop from another, so the regression shows up under TSan.

Activity

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

    @zackees
    MemberAuthor

    Claiming: audio capture/mix synchronization. 1. Reproduce the unlocked timestamp/buffer reads with a regression against the real source and mixer path. 2. Protect the shared state while preserving render callback behavior. 3. Validate focused concurrency tests, sanitizer evidence, and the OFF/ON compatibility gates; open one PR after RED/GREEN evidence.

  3. added
    claimed
    on Oct 13, 2026
  4. zackees commented on Oct 14, 2026

    @zackees
    MemberAuthor

    PR #662 is open. RED94909f355 reproduces the capture/mixer deque race under TSan. The fix synchronizes timestamp and buffer access, including transition child timestamps. Both real capture and active-transition cases pass TSan, 100 repetitions each OFF/ON pass under Xvfb, 192 C tests each pass, symbols match, and all local gate lanes passed on the stamped pushed head. CI is running before merge.

  5. zackees commented on Oct 14, 2026

    @zackees
    MemberAuthor

    Fixed by #662. Capture, source rendering, buffering decisions, and transition timestamp reads now synchronize on audio_buf_mutex. The real capture/mixer regression was RED under TSan and is GREEN for direct capture and active transitions. 100 consecutive runs passed in each OFF/ON configuration; all local gate lanes and native macOS/Windows CI checks passed. The issue’s timestamp and input-buffer races are addressed.

  6. removed
    claimed
    on Oct 14, 2026
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

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions