Skip to content

obs-ffmpeg: Release the SRT output's libsrt reference once - #1102

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/srt-double-cleanup
Oct 16, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/srt-double-cleanup

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1101

Related: upstream obsproject/obs-studio#12361 has the same symptom (one failing SRT connection tears down the others) but a different cause, libsrt 1.5.4's own bug. In this repo that half is #1082 (AppImage libsrt bump).

What and why

An SRT stream that fails to connect no longer breaks the other SRT connections in the same OBS. Failures include nobody listening, a wrong passphrase, a refused streamid, and an invalid srt:// URL. Before, every such Start Streaming disconnected every SRT Media Source, and the libsrt reference count went below zero. After that, one Media Source that failed to connect stopped a running SRT stream. This happens with every libsrt version, 1.5.7 included. We found it ourselves; no user report is known.

Cause

  • libsrt_open() (plugins/obs-ffmpeg/obs-ffmpeg-srt.h) calls srt_startup() and, on any failure, srt_cleanup().
  • connect_mpegts_url() (obs-ffmpeg-mpegts.c) then calls libsrt_close() on the same context, which calls srt_cleanup() again. It also logs a Session Summary and calls srt_epoll_release()/srt_close() on id 0.
  • libsrt counts srt_startup() calls for the whole process. In 1.5.2 and 1.5.7, CUDTUnited::cleanup() stops the GC thread when the count reaches zero, and 1.5.7 also force-breaks every socket (closeAllSockets()). FFmpeg's srt:// input (Media Sources) uses the same libsrt.
  • Upstream OBS 32.2.2 has identical code.

In the release and on master (headless, fresh profile, Simple output, loopback only, the host's ffmpeg / srt-live-transmit as peers; check 70-srt-failed-connect-bystander in our release-check harness; 5 trials per row; killed = the victim's peer lost its connection, or the victim stream left STARTED):

Attack → victim 0.1.0-beta.20261014 (libsrt 1.5.4) master AppImage (1.5.2) master AppImage (1.5.7) this branch's obs-ffmpeg, 1.5.2 this branch's obs-ffmpeg, 1.5.7
(a) fresh OBS: Media Source calls a dead srt:// port → running SRT stream (upstream #12361) 5/5 killed 0/5 0/5 0/5 0/5
(b) SRT stream, nobody listening → SRT Media Source (listener) 5/5 killed 5/5 killed 5/5 killed 0/5 0/5
(b) SRT stream, wrong passphrase → the same Media Source 5/5 killed 5/5 killed 5/5 killed 0/5 0/5
(c) = (a) after (b) 5/5 killed 5/5 killed 4/4 killed, then SIGFPE 0/5 0/5
  • The fixed columns are master's manylinux AppImage trees with only obs-ffmpeg.so replaced. That file was built from this branch in the same manylinux_2_28 image, with the AppImage's libobs-va names and RPATH. Both runs quit cleanly.
  • The 1.5.7 master run died of SIGFPE while starting a stream after the failures. One run of the released AppImage also stopped answering at the same point. Neither happened with the fix.
  • Row (a) in the release is libsrt 1.5.4's bug and stays until bug: AppImage: libsrt is outdated (public advisories), and the next build drops it from 1.5.4 to 1.5.2 #1082 ships 1.5.7. This change does not touch it.
  • Stock OBS 32.2.2 (libsrt 1.5.6): row (a) 0/5. Rows (b)/(c) were not reachable, because its frontend started those streams through the out-of-process FFmpeg muxer.

Fix (plugins/obs-ffmpeg/obs-ffmpeg-srt.h only)

  • SRTContext.srt_started records the srt_startup() reference. libsrt_open() sets it, together with fd = SRT_INVALID_SOCK and eid = -1, and clears it in its err: path before srt_cleanup().
  • libsrt_close() returns early when the context holds no reference (a failed or already closed context). It calls srt_cleanup() without touching a socket when there is none. Otherwise it closes the socket, resets fd/eid, and drops the reference once, also when srt_close() fails.

Not changed

  • connect_mpegts_url() and close_mpegts_url() still call libsrt_close(). That is now safe in every state.
  • RIST (obs-ffmpeg-rist.h) has no process-wide counter, so it is not affected.

RED → GREEN

  • RED commit: fee2cbd test/cmocka: SRT output releases libsrt twice after a failed connect. test_srt_open_close (Linux, built where the native SRT/RIST output and libsrt are found) opens and closes an SRT output context the way connect_mpegts_url() does, against its own SRT listener on 127.0.0.1. It counts srt_startup()/srt_cleanup() with --wrap. Where libsrt counts references, it also checks that the listener is still listening; a group setup probes that. Three cases fail in both lanes, each with "the output called srt_startup() 1 time(s) and srt_cleanup() 2 time(s)" and, on the host's libsrt 1.5.6, "another SRT user's socket was broken (state 9, expected SRTS_LISTENING 3)":

    • rejected_passphrase_leaves_other_srt_users_alone
    • nobody_listening_leaves_other_srt_users_alone
    • invalid_url_leaves_other_srt_users_alone

    The guard accepted_stream_leaves_other_srt_users_alone passes. 3 of 4 fail. With libsrt 1.5.4 (Ubuntu 26.04's, as in CI), 1.5.2 and 1.5.7 preloaded instead (C lane), the same 3 fail. On 1.5.4 the test says that libsrt breaks every socket on any cleanup, and it checks the count only.

  • GREEN: c58c98b. test_srt_open_close passes 4/4 with ENABLE_RUST_LIBOBS OFF and ON, also run directly without a display, and (C lane) against libsrt 1.5.4, 1.5.2 and 1.5.7.

  • Full ctest at -j8 (private Xvfb, private D-Bus, system libobs-opengl masked, TMPDIR unset), with the native SRT/RIST output on: 268/268 in each lane. Skips:

    • open_crashes_without_obs_core in test_audio_io and test_video_io, as on master;
    • test_startup_shutdown_cycles, where obs_startup() fails here, as on master;
    • test_alloc_count_stable_after_startup, under load in the OFF lane only.

    All PipeWire/Pulse tests ran. clang-format 22.1.3, gersemi 0.25.0 and ci/test_registration.py are clean.

  • CI: Tier 2 Linux installs librist-dev and libsrt-openssl-dev and builds cmocka-tests with the native output on (the default), so the test should be built and run there, on libsrt 1.5.4 (count check only); check the PR's Tier 2 Linux log for test_srt_open_close. It is not registered on macOS/Windows, where the test's listener setup is untested.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): none
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not a port

AI note: Claude Code (Claude Opus 5.5) wrote the code, the tests, the commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

@rdoupe
rdoupe requested a review from zackees as a code owner October 16, 2026 17:20
@zackees

zackees commented Oct 16, 2026

Copy link
Copy Markdown
Member

CI triage (maintainer loop): both Tier 2 Linux jobs (ON and OFF) fail on the GREEN head c58c98b with 216 - test_srt_open_close (SEGFAULT), so the merge queue can't take this yet.

All four cases print [ PASSED ] 4 test(s). before the crash, so it happens after cmocka_run_group_tests returns, at process exit. The runner has libsrt 1.5.4 (libsrt version 1.5.4 loaded), the version the test itself says tears down global state on any srt_cleanup() (Haivision/srt#3098). Most likely cause: the test makes several full startup/cleanup cycles (probe + 4 × setup/teardown + the output's own pair), and libsrt 1.5.4's static destructors run at exit against state that was already cleaned up. To check, run it under gdb/ASan in the Tier 2 image (./unittest), or remove the probe's extra __real_srt_startup()/__real_srt_cleanup() pair and see whether the segfault goes away. If this turns out to be a 1.5.4 bug that can't be avoided, ending main with _exit(rc) after flushing output, with a comment pointing to the upstream issue, would be acceptable. Push a fix to this branch and CI will re-run.

rdoupe and others added 2 commits October 16, 2026 16:50
When an SRT stream fails to connect (nobody listening, wrong passphrase,
refused streamid) or its srt:// URL is invalid, libsrt_open() calls
srt_cleanup() itself, then connect_mpegts_url() calls libsrt_close() on
the same context, which calls srt_cleanup() again. libsrt counts
srt_startup() calls process-wide and breaks every SRT socket in the
process when the count reaches zero, so the extra call drops a
reference that belongs to another SRT user, such as an SRT Media Source.

test_srt_open_close mirrors the output's open + close against its own
listener on 127.0.0.1 and counts the srt_startup()/srt_cleanup() calls
with --wrap. Where libsrt counts references (not 1.5.4), it also checks
that an idle listener, which stands in for the other SRT user, is still
listening. Fails now: rejected passphrase, nobody listening and invalid
URL (1 startup, 2 cleanups). Guard: a stream that connects and stops.

The stream's server runs in a child process, forked before the test
touches libsrt, and the in-process stand-in for the other SRT user is an
idle listener: when libsrt tears its global state down (any
srt_cleanup() on 1.5.4, the extra one without the fix), its garbage
collector can deadlock with a receive thread handling a handshake for a
listener in the same process. The test leaves with _exit() once cmocka
has printed its results: on Ubuntu, libavformat links libsrt-gnutls and
the test (like obs-ffmpeg) libsrt-openssl, and with both copies loaded
libsrt 1.5.4 destroys its packet-filter registry twice at exit and
crashes after any srt_startup(), independent of the output's code.

Refs obs-rust#1101.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
libsrt_open() calls srt_cleanup() itself when it fails, and
connect_mpegts_url() then calls libsrt_close() on the same context,
which called srt_cleanup() again (and srt_close() on socket 0). libsrt
counts srt_startup() calls process-wide and breaks every SRT socket in
the process when the count reaches zero, so every SRT stream that
failed to connect (nobody listening, wrong passphrase, refused
streamid) or had an invalid srt:// URL disconnected SRT Media Sources
in the same OBS, and drove the count below zero, so a later failed
Media Source connect also broke a running SRT stream.

Track the srt_startup() reference in the SRTContext and drop it exactly
once: libsrt_open() clears it when it cleans up after a failure, and
libsrt_close() only closes a socket and calls srt_cleanup() while the
context still holds the reference, so closing a failed or already
closed context does nothing. A failing srt_close() no longer keeps the
reference either.

Fixes obs-rust#1101.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rdoupe
rdoupe force-pushed the fix/srt-double-cleanup branch from c58c98b to e4de8be Compare October 16, 2026 21:05
@rdoupe

rdoupe commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the triage. Reproduced in an Ubuntu 26.04 rootfs with the Tier 2 package list (libsrt 1.5.4-3), and fixed in the test. The product fix doesn't change.

Cause. Two copies of libsrt end up in the test process: the distro libavformat links libsrt-gnutls.so.1.5, and the test (like obs-ffmpeg) links libsrt.so.1.5 (openssl). Both export the same C++ globals, so after any srt_startup() both copies' static destructors run on the one srt::PacketFilter::filters map at exit, and the second one crashes:

#1 std::map<std::string, srt::PacketFilter::ManagedPtr>::~map ()  srtcore/packetfilter.h:91
#2 __cxa_finalize ()
#3 __do_global_dtors_aux ()  libsrt-gnutls.so.1.5

It isn't about the number of startup/cleanup cycles. A five-line program that links libsrt-openssl and libavformat and calls srt_startup() once (no cleanup) segfaults at exit the same way. Without libavformat it doesn't, and with only one copy loaded it doesn't either.

While testing under load I also caught a second 1.5.4 problem: an occasional hang (3 of 200 runs under CPU load). A global teardown (any srt_cleanup() on 1.5.4) deadlocks libsrt's GC thread against a receive thread that is handling a handshake for the test's own listener in the same process.

Fix (test only, RED commit amended).

  • The SRT server the output connects to now runs in a child process, forked before the test touches libsrt. The in-process stand-in for "another SRT user" is an idle listener, so no handshake is in flight in the test process during a teardown.
  • main ends with fflush + _exit(rc) after cmocka has printed its results, with a comment explaining the two-copy destructor crash.

Evidence (Ubuntu 26.04 rootfs, Tier 2 packages; plus an Arch host with libsrt 1.5.7):

  • RED fails on 1.5.4, 1.5.2 and 1.5.7 in both lanes (3 of 4 cases: 1 startup, 2 cleanups; on 1.5.2/1.5.7 the bystander listener is also broken). GREEN passes on all three, both lanes.
  • 1.5.4, GREEN: 20 of 20 sequential runs pass per lane, and 200 of 200 under 8-way parallel load per lane, with no crash and no hang. The old test layout hung 3 of 200 under the same load; the new one hung 0 of 200.
  • Full ctest -j8 in the Ubuntu rootfs: 263 of 263 in both lanes. On the host, the full suite passes except for the usual intermittent xvfb-run teardown flake in test_video_io_hidden (all of its cases pass).
  • A real OBS with the GREEN obs-ffmpeg on a single libsrt 1.5.4 quits cleanly after a failed connect, a wrong passphrase, an invalid URL and a good stream. The two-copy exit crash isn't caused by this change: any process that loads both libsrt builds crashes at exit after a single srt_startup(), whatever the plugin does (shown by the minimal repro above).

@zackees
zackees added this pull request to the merge queue Oct 16, 2026
Merged via the queue into obs-rust:master with commit 86fb718 Oct 16, 2026
25 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: obs-ffmpeg: an SRT stream that fails to connect calls srt_cleanup() twice and breaks every other SRT connection in OBS

2 participants