Skip to content

test: Check that media playback returns the packet on EOF and read errors - #734

Merged
zackees merged 3 commits into
obs-rust:masterfrom
chriscoveries:test/media-packet-eof-leak
Oct 14, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
chriscoveries:test/media-packet-eof-leak

Conversation

@chriscoveries

Copy link
Copy Markdown
Contributor

Draft. Opened so upstream PR CI can run it. Our own CI is down, so this hasn't been through our usual CI runs: see our note on #699.

Status: draft. Opened so upstream PR CI can run it.

Fork branch: test/media-packet-eof-leak, head 72d744e325bdbe940904f7c706412c655811da0d (one commit on upstream/master 1a096bf43).


Problem

7e0f28957 ("media-playback: Return the packet to the pool when a read fails", closes #463) fixed a packet leak in mp_media_next_packet(): a failed av_read_frame() (EOF, network timeout, abort, read error) returned without putting the packet back in the pool. That commit has no regression test of its own. The leak was only visible through LeakSanitizer and valgrind on test_media_network_timeout, so nothing in the default test run fails if the fix is removed.

Root cause

Not a new bug. This PR only adds the missing guard for the one-line fix at shared/media-playback/media-playback/media.c:180-186 (mp_media_free_packet(media, pkt) before returning a negative av_read_frame() result).

Fix

Tests only. No production code changes. Adds test_media_packet_eof_leak with two cases:

  • eof_releases_packet: demuxes a small committed Matroska fixture through the production mp_media_next_packet() until EOF, drains the packet pool, and checks that every packet the code allocated was freed.
  • read_error_releases_packet: forces AVERROR(EIO) from av_read_frame() through a test-side wrapper and checks that the packet is back in the pool after the failed read.

No libobs test hook, exported symbol or sleep. The test includes media.c the same way test_svt_av1.c does. It is registered in test/cmocka/tests.d/test_media_packet_eof_leak.cmake, guarded by if(TARGET OBS::libobs AND TARGET FFmpeg::avformat).

Fixture: test/cmocka/data/media_packet_eof_leak.mkv is a 5 s, 16x16 ffv1 plus 48 kHz mono PCM clip (any small media file would do). Regenerate with ffmpeg -f lavfi -i "color=c=black:s=16x16:r=10:d=5,format=yuv420p,geq=lum='5*N':cb=128:cr=128" -f lavfi -i "aevalsrc='500*(floor(t*10)+1)/32768':s=48000:d=5:c=mono" -c:v ffv1 -c:a pcm_s16le -t 5 out.mkv.

  • Alternative: add the assertions to test_media_network_timeout. Rejected because that test needs a network stack and only shows the leak under a sanitizer; this one fails in a plain run.

Evidence

Local, macOS arm64, Debug, Xcode generator, ENABLE_UNIT_TESTS=ON, frontend/scripting/browser/websocket OFF, no sanitizer.

  • Passes on current master (fix present): test_media_packet_eof_leak passes 2/2 with ENABLE_RUST_LIBOBS=OFF and with ENABLE_RUST_LIBOBS=ON:

    [ RUN      ] eof_releases_packet
    [       OK ] eof_releases_packet
    [ RUN      ] read_error_releases_packet
    warning: MP: av_read_frame failed: Input/output error (-5)
    [       OK ] read_error_releases_packet
    [  PASSED  ] 2 test(s).
    
  • Fails when the fix is reverted: with only the three added lines in media.c from 7e0f28957 removed (git show 7e0f28957 -- shared/media-playback/media-playback/media.c | git apply -R), rebuilt and rerun in both OFF and ON, both cases fail:

    packet EOF: reads=285 ret=-541478725 allocated=1 freed=0
    [  ERROR   ] --- 1 != 0   (test_media_packet_eof_leak.c:107)
    [  FAILED  ] eof_releases_packet
    packet read error: ret=-5 pooled=0 allocated=1 freed=0
    [  ERROR   ] --- 0 != 1   (test_media_packet_eof_leak.c:130)
    [  FAILED  ] read_error_releases_packet
    [  FAILED  ] tests: 2 test(s)
    

    The revert was local only and has been restored; the pushed branch contains no production change.

  • Real-run evidence: not applicable. This adds a headless test; no full OBS session was run.

  • Fork CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI. Linux OFF/ON, GCC -Werror and LeakSanitizer have not been run.

Compatibility & risk

No C API or ABI change and no production code change. The only added build surface is one cmocka target, one tests.d fragment and a ~480 KB fixture. Local validation covers macOS arm64 only; Linux (GCC, -Werror, LeakSanitizer) was not run. The branch is based on 1a096bf43; upstream/master has since advanced (including media-playback seek changes in accf62aa1), so re-run git merge-tree --write-tree upstream/master <head> and re-check the head before posting.

Status

Status: draft. Opened so upstream PR CI can run it. The earlier RED+GREEN draft is obsolete because upstream merged the fix first.

How this was tested

  • Pass on master, fail on revert: built test_media_packet_eof_leak on macOS arm64 Debug with ENABLE_UNIT_TESTS=ON, for ENABLE_RUST_LIBOBS=OFF and ON. It passes on current master and fails (both cases) with the 7e0f28957 change reverted locally. No sanitizer was enabled locally.
  • Our own CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.
  • Real runs: not applicable; headless test only.

Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.

@zackees

zackees commented Oct 14, 2026

Copy link
Copy Markdown
Member

Both failures are the same clang-format violation (the "Rust Tests" job fails in lane lint):

test/cmocka/test_media_packet_eof_leak.c:139:75: error: code should be clang-formatted [-Wclang-format-violations]
        const struct CMUnitTest tests[] = {cmocka_unit_test(eof_releases_packet), cmocka_unit_test(read_error_releases_packet)};

Line 139 is over the column limit. Fix: uv run ci/local_gate.py --lane lint --fix (or uvx [email protected] -i test/cmocka/test_media_packet_eof_leak.c) and commit. No other failures in this run.

@zackees
zackees marked this pull request as ready for review October 14, 2026 23:11
@zackees
zackees self-requested a review as a code owner October 14, 2026 23:11
@zackees
zackees merged commit 467d82d into obs-rust:master Oct 14, 2026
22 checks passed
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: media-playback: mp_media_next_packet leaks a packet on the reset path

2 participants