Skip to content

obs-ffmpeg: Wait for a frame being encoded before freeing the encoders - #355

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/7536-ffmpeg-stop-encode-race
Oct 12, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/7536-ffmpeg-stop-encode-race

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Description

Stopping a Custom Output (FFmpeg) recording no longer frees the encoders while the video or audio thread is still encoding with them. With libvpx this crashed OBS on stop ("corrupted double-linked list", a crash in vp8_remove_compressor()) or hung it, and it made test_ffmpeg_output_alpha (#311) flaky.

  • ffmpeg_output_full_stop() calls obs_output_end_data_capture() and then ffmpeg_deactivate(), which frees the codec contexts. For a raw output, libobs disconnects the video and audio callbacks on its end_data_capture thread and returns right away, so receive_video() or receive_audio() can still be in avcodec_send_frame() when the contexts are freed.
  • The window is one encode call. libvpx's encode and teardown both wait for its worker threads, which widens it: in the test, vp8_alpha_conflict_still_fails sometimes aborted or timed out when its VP8 recording stopped. Under gdb, the hang shows the stopping thread in vpx_codec_destroy() and the video thread in vpx_codec_encode() <- receive_video() at the same time.
  1. test/cmocka: Add failing test for stopping an FFmpeg output mid-encode: test_ffmpeg_output_stop builds the real obs-ffmpeg-output.c into the test and wraps avcodec_send_frame() and avcodec_free_context() with the linker's --wrap. It parks the next video (or audio) encode call before the real call, stops the output, and checks whether the output freed the context the parked thread is about to use. If it did, the parked call returns EAGAIN without touching the freed context, so the case fails instead of crashing. The cases are:

    • FFV1 in Matroska, video only: stop while the video thread encodes;
    • FLAC, audio only: stop while the audio thread encodes.

    Today both fail, in every run (20 of 20 without the Rust libobs, 10 of 10 with it).

  2. obs-ffmpeg: Wait for a frame being encoded before freeing the encoders: adds encode_mutex to struct ffmpeg_output. receive_video() and receive_audio() encode under it, and ffmpeg_deactivate() holds it while it frees the queued packets and calls ffmpeg_data_free(). A stop waits for the frame in flight; callbacks that arrive before libobs disconnects them find no streams and return. 2 of 2 pass.

Motivation and Context

Fixes #354 (upstream obsproject/obs-studio#7536, reported by glaubersm). glaubersm's backtrace has the same crash: unlink_chunk() in free() from vp8_remove_compressor(), on stopping an MKV + libvpx recording. RytoEX reproduced intermittent crashes with matroska + libvpx + libvorbis on Arch Linux. The issue was closed upstream as not planned, with "PRs to address this Issue are welcome". There is no upstream PR for it, and no code from upstream is used here.

It is not caused by #311: the alpha-format retry only runs when the encoder fails to open, and the recording that crashes opens on the first try. The race is the same on upstream master.

Why this one: a crash or hang when the user presses Stop Recording loses the end of the recording (no trailer is written) and the session. It also kept a flaky test in our suite.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), on current master:

  • test_ffmpeg_output_stop: both cases fail at the RED commit in 30 of 30 runs (20 and 10 per lane), and pass at GREEN in 30 of 30 runs per lane. At GREEN the test takes about 2 s.
  • test_ffmpeg_output_alpha, run alternately with and without the fix (two CPUs, 100 runs each): without it 4 of 100 runs failed when the VP8 recording stopped (2 glibc aborts "free(): corrupted unsorted chunks", 1 SIGSEGV, 1 hang); with it 100 of 100 passed. Also 30 of 30 per lane at GREEN through ctest.
  • AddressSanitizer did not report it: the race corrupts memory inside libvpx and glibc, which are not instrumented. Under ASan the test hung once in 50 runs before the fix and passed 30 of 30 after it. The cause was found under gdb instead (the stopping thread in vpx_codec_destroy() while the video thread was in vpx_codec_encode()).
  • Full ctest at GREEN: 115 of 115 in each lane (rebased on current master). The two FFmpeg output tests also pass with the system's OpenGL module hidden, with every case run (no skips).
  • clang-format, gersemi and ci/test_registration.py are clean. The test is registered Linux-only (it needs GNU ld's --wrap) and, like the other output tests, needs a display and skips without one.

Not tested:

  • The OBS UI. The test drives the real output through libobs.
  • Windows and macOS. The test is Linux-only; the fix is plain pthreads, as in the rest of the file.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

Stopping a Custom FFmpeg Output frees its encoders in ffmpeg_deactivate()
right after obs_output_end_data_capture(). libobs disconnects the raw video
and audio callbacks on its end_data_capture thread, so receive_video() or
receive_audio() may still be encoding a frame with those encoders. With
libvpx this corrupts the heap or deadlocks the encoder's threads on stop.

The test builds the real ffmpeg_output into the test and wraps
avcodec_send_frame() and avcodec_free_context() with the linker's --wrap.
It parks the next video (or audio) encode call before the real call, stops
the output, and checks whether the output freed the context the parked
thread is about to use. If it did, the parked call returns EAGAIN without
touching the freed context, so the case fails instead of crashing.

Cases:
- FFV1 in Matroska, video only: stop while the video thread encodes;
- FLAC, audio only: stop while the audio thread encodes.

On master, under xvfb, both fail in 20 of 20 runs. Without a display
every case skips.

Reported upstream in obsproject/obs-studio#7536 by glaubersm (crash in
vp8_remove_compressor() on stopping an MKV + libvpx recording).

Refs obs-rust#354.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 09:44
Stopping a Custom FFmpeg Output called obs_output_end_data_capture() and
then ffmpeg_deactivate(), which frees the codec contexts. libobs
disconnects the raw video and audio callbacks on its end_data_capture
thread, so receive_video() or receive_audio() could still be encoding a
frame, or start one, while the encoders were freed. With libvpx (WebM or
MKV with VP8) stopping a recording crashed with heap corruption in
vp8_remove_compressor() ("corrupted double-linked list") or hung with the
video thread in vpx_codec_encode() and the stopping thread in
vpx_codec_destroy().

Encode under a new encode_mutex in both callbacks, and hold it in
ffmpeg_deactivate() while the queued packets and the encoders are freed.
A stop now waits for the frame in flight; later callbacks find no streams
(ffmpeg_data_free() clears them) and return.

test_ffmpeg_output_stop passes (2 of 2), and test_ffmpeg_output_alpha's
VP8 case no longer crashes or hangs when its recording stops.

Fixes obs-rust#354.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=1ef1809310fa4fd4e14bbba255dcbeb5e5d4c3b4 secs=147 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791798943,"gate":"rust/all/fmt","host":"linux-x86_64","key":"8664d78025d3eeffd14b28783c6f5b428eb2b4889abe7cc791290e15780cf3e9","lane":"lint","parents":["bfac39d5e80237c4070333d76206607c62c2a36e"],"secs":56,"stamp":"07a7b07894758b95e39e35db05a401a3","tree":"1ef1809310fa4fd4e14bbba255dcbeb5e5d4c3b4","v":1,"via":"run"}
Ci-Attestation: {"at":1791798943,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"8664d78025d3eeffd14b28783c6f5b428eb2b4889abe7cc791290e15780cf3e9","lane":"lint","parents":["bfac39d5e80237c4070333d76206607c62c2a36e"],"secs":56,"stamp":"5944a8a3011e561b52da6b52fb215022","tree":"1ef1809310fa4fd4e14bbba255dcbeb5e5d4c3b4","v":1,"via":"run"}
Ci-Attestation: {"at":1791798943,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"97a6dbb0c0573bcf30a46128c3448c9c99bdce9e41a8c1cccde9f0a690ed8d14","lane":"rust-test","parents":["bfac39d5e80237c4070333d76206607c62c2a36e"],"secs":47,"stamp":"52a1840097762a7cc6678561a5b4eee1","tree":"1ef1809310fa4fd4e14bbba255dcbeb5e5d4c3b4","v":1,"via":"run"}
Ci-Attestation: {"at":1791798943,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"e51ed0fb10218b46bbd1d6bbdf73c976ed837cbf675020525e0966005fd892ff","lane":"tier2-linux","parents":["bfac39d5e80237c4070333d76206607c62c2a36e"],"secs":44,"stamp":"d6211abe3fee0f13756bdf5204252592","tree":"1ef1809310fa4fd4e14bbba255dcbeb5e5d4c3b4","v":1,"via":"run"}
@zackees
zackees force-pushed the fix/7536-ffmpeg-stop-encode-race branch from b899e8e to ea3b927 Compare October 12, 2026 09:55
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally under xvfb-run (Tier 2 image + mesa), 3 runs per commit:

  • RED bfac39d: 3/3 runs fail stop_waits_for_video_encode and stop_waits_for_audio_encode
  • GREEN b899e8e: 3/3 runs pass 2/2

Note: without a display the test skips but reports Passed, so plain Tier 2 / CI does not exercise it. Rebased on master, local gate passed; pushed as ea3b927.

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.

bug: obs-ffmpeg: stopping a Custom FFmpeg output frees the encoder mid-frame (crash/hang with libvpx) (upstream #7536)

2 participants