Repository navigation
Description
Upstream OBS bug, reported by glaubersm in obsproject/obs-studio#7536 (closed upstream as not planned; RytoEX reproduced intermittent crashes on Arch Linux there and wrote "PRs to address this Issue are welcome"). Code unchanged in this fork and on upstream master. Found again here as a flaky crash in test_ffmpeg_output_alpha (#311).
Location
plugins/obs-ffmpeg/obs-ffmpeg-output.cffmpeg_output_full_stop(): callsobs_output_end_data_capture()and thenffmpeg_deactivate(), which callsffmpeg_data_free()and frees the video and audio codec contexts (close_video(),close_audio()).libobs/obs-output.cobs_output_end_data_capture_internal(): disconnects a raw output's video and audio callbacks on a new thread (end_data_capture_thread()->stop_raw_video(),stop_raw_audio()) and returns right away.receive_video()andreceive_audio()inobs-ffmpeg-output.cencode with those contexts and take no lock against the stop.
Class
Crash / hang: use of freed encoders. Stopping a Custom Output (FFmpeg) recording can corrupt the heap or deadlock.
How it is reached
Settings > Output > Recording > Type: Custom Output (FFmpeg), output to file. Start a recording and stop it. When the stop lands while the video thread is in receive_video() (or the audio thread in receive_audio()), the stopping thread frees the encoder the other thread is encoding with. The race window is the length of one encode call, so fast encoders rarely hit it; libvpx (VP8 in WebM or Matroska) does, because its encode and its teardown both wait for its worker threads.
Seen here (FFmpeg 9.0.1, libvpx 1.17), in test_ffmpeg_output_alpha's vp8_alpha_conflict_still_fails, when its VP8 recording stops:
- glibc abort "corrupted double-linked list", or SIGSEGV in
free()undervpx_codec_destroy()<-avcodec_free_context()<-close_video()<-ffmpeg_data_free()<-obs_output_actual_stop(); - a hang (test timeout): the stopping thread in
vpx_codec_destroy(), the video thread invpx_codec_encode()<-avcodec_send_frame()<-receive_video(), and libobs' end_data_capture thread waiting instop_raw_video()for that frame to finish.
The upstream report has the same crash: unlink_chunk() in free() from vp8_remove_compressor() <- vp8e_destroy(), on stopping an MKV + libvpx recording.
The "ffmpeg_data_init failed" line right before the crash in the test log is the expected failure of the first VP8 recording (auto-alt-ref with alpha); the crash is in the second recording's stop.
Not the same as
- bug: obs-ffmpeg: WebM/VP9 recording with BGRA fails to start (upstream #13785) #310 / PR obs-ffmpeg: Fall back to an alpha format the encoder opens #311 (upstream WebM-VP9 codec no longer loads with BGRA format obsproject/obs-studio#13785): the alpha-format retry in
create_video_stream(). It only runs when the encoder fails to open, and the crashing recording opens on the first try. The race is older than obs-ffmpeg: Fall back to an alpha format the encoder opens #311 and is the same on upstream master. - An FFmpeg or libvpx bug: both are used from two threads at once, which neither supports.
Proposed fix and failing test
Fix: a new encode_mutex in 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 (ffmpeg_data_free() clears them) and return.
RED test (cmocka, test_ffmpeg_output_stop, needs a display like the other output tests and skips without one): 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. Cases:
- FFV1 in Matroska, video only: stop while the video thread encodes;
- FLAC, audio only: stop while the audio thread encodes.
Both fail today, in every run.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
rdoupe commentedon Oct 12, 2026
Claiming: this issue.
test_ffmpeg_output_stop(2 of 2 failing, every run): the real FFmpeg output with a video or audio encode call held in flight; stopping the output must not free that encoder's context underneath it.PR today.
obs-ffmpeg: Wait for a frame being encoded before freeing the encoders
rdoupe commentedon Oct 12, 2026
@glaubersm you reported this in obsproject/obs-studio#7536, so a heads-up: it's fixed here in obs-rust by #355, merged today with a regression test.
Cause: when a Custom Output (FFmpeg) recording stops, libobs disconnects the video and audio callbacks on a separate thread and returns right away, and the output then frees its encoders. A frame could still be inside the encoder on the video or audio thread at that moment, so libvpx's teardown freed memory its encode call was still using. That's the
free()crash fromvp8_remove_compressor()in your backtrace (RytoEX reproduced it intermittently with MKV + libvpx), and it could also hang on stop. Now the encode callbacks and the stop path share a lock, so stopping waits for the frame being encoded before freeing anything. We found it again because one of our own VP8 tests crashed now and then on stop. The new test holds an encode call in flight and stops the output: before, the encoder was freed underneath it every time; now the stop waits.Not covered: tested with libvpx, FFV1 and FLAC through FFmpeg 9.0 on a virtual display; the UI and Windows/macOS weren't tested.
Workaround on stock OBS: none reliable; stopping is a race, so it only crashes some of the time.
obs-rust is an AI-assisted fork of OBS; on Arch it's
obs-studio-rust-gitin the AUR.If you try it, a short reply here on whether stopping your MKV + libvpx recordings is clean would really help.
obs-ffmpeg: Wait for a frame being encoded before freeing the encoders (