Repository navigation
obs-ffmpeg: Wait for a frame being encoded before freeing the encoders - #355
Merged
zackees merged 2 commits intoOct 12, 2026
Merged
Conversation
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]>
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
force-pushed
the
fix/7536-ffmpeg-stop-encode-race
branch
from
October 12, 2026 09:55
b899e8e to
ea3b927
Compare
Member
|
Verified locally under xvfb-run (Tier 2 image + mesa), 3 runs per commit:
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. |
This was referenced Oct 12, 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.
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 madetest_ffmpeg_output_alpha(#311) flaky.ffmpeg_output_full_stop()callsobs_output_end_data_capture()and thenffmpeg_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, soreceive_video()orreceive_audio()can still be inavcodec_send_frame()when the contexts are freed.vp8_alpha_conflict_still_failssometimes aborted or timed out when its VP8 recording stopped. Under gdb, the hang shows the stopping thread invpx_codec_destroy()and the video thread invpx_codec_encode()<-receive_video()at the same time.test/cmocka: Add failing test for stopping an FFmpeg output mid-encode:test_ffmpeg_output_stopbuilds the realobs-ffmpeg-output.cinto the test and wrapsavcodec_send_frame()andavcodec_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:Today both fail, in every run (20 of 20 without the Rust libobs, 10 of 10 with it).
obs-ffmpeg: Wait for a frame being encoded before freeing the encoders: addsencode_mutextostruct ffmpeg_output.receive_video()andreceive_audio()encode under it, andffmpeg_deactivate()holds it while it frees the queued packets and callsffmpeg_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()infree()fromvp8_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_LIBOBSOFF 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.vpx_codec_destroy()while the video thread was invpx_codec_encode()).ci/test_registration.pyare 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:
Types of changes
Checklist:
🤖 Generated with Claude Code