Skip to content

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

Closed

Description

@rdoupe

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.c ffmpeg_output_full_stop(): calls obs_output_end_data_capture() and then ffmpeg_deactivate(), which calls ffmpeg_data_free() and frees the video and audio codec contexts (close_video(), close_audio()).
  • libobs/obs-output.c obs_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() and receive_audio() in obs-ffmpeg-output.c encode 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() under vpx_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 in vpx_codec_encode() <- avcodec_send_frame() <- receive_video(), and libobs' end_data_capture thread waiting in stop_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

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.

Activity

  1. rdoupe commented on Oct 12, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: this issue.

    1. RED: 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.
    2. GREEN: an encode lock taken by the raw video/audio callbacks and by the stop path before it frees the encoders.
      PR today.
  2. added a commit that references this issue on Oct 12, 2026
    ea3b927
  3. rdoupe commented on Oct 12, 2026

    @rdoupe
    CollaboratorAuthor

    @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 from vp8_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-git in the AUR.

    If you try it, a short reply here on whether stopping your MKV + libvpx recordings is clean would really help.

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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions