Repository navigation
obs-ffmpeg: Release the SRT output's libsrt reference once - #1102
Conversation
|
CI triage (maintainer loop): both Tier 2 Linux jobs (ON and OFF) fail on the GREEN head c58c98b with All four cases print |
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]>
c58c98b to
e4de8be
Compare
|
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 It isn't about the number of startup/cleanup cycles. A five-line program that links libsrt-openssl and libavformat and calls 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 Fix (test only, RED commit amended).
Evidence (Ubuntu 26.04 rootfs, Tier 2 packages; plus an Arch host with libsrt 1.5.7):
|
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) callssrt_startup()and, on any failure,srt_cleanup().connect_mpegts_url()(obs-ffmpeg-mpegts.c) then callslibsrt_close()on the same context, which callssrt_cleanup()again. It also logs a Session Summary and callssrt_epoll_release()/srt_close()on id 0.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'ssrt://input (Media Sources) uses the same libsrt.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-bystanderin our release-check harness; 5 trials per row; killed = the victim's peer lost its connection, or the victim stream left STARTED):obs-ffmpeg, 1.5.2obs-ffmpeg, 1.5.7srt://port → running SRT stream (upstream #12361)obs-ffmpeg.soreplaced. That file was built from this branch in the same manylinux_2_28 image, with the AppImage'slibobs-vanames and RPATH. Both runs quit cleanly.Fix (
plugins/obs-ffmpeg/obs-ffmpeg-srt.honly)SRTContext.srt_startedrecords thesrt_startup()reference.libsrt_open()sets it, together withfd = SRT_INVALID_SOCKandeid = -1, and clears it in itserr:path beforesrt_cleanup().libsrt_close()returns early when the context holds no reference (a failed or already closed context). It callssrt_cleanup()without touching a socket when there is none. Otherwise it closes the socket, resetsfd/eid, and drops the reference once, also whensrt_close()fails.Not changed
connect_mpegts_url()andclose_mpegts_url()still calllibsrt_close(). That is now safe in every state.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 wayconnect_mpegts_url()does, against its own SRT listener on 127.0.0.1. It countssrt_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_alonenobody_listening_leaves_other_srt_users_aloneinvalid_url_leaves_other_srt_users_aloneThe guard
accepted_stream_leaves_other_srt_users_alonepasses. 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_closepasses 4/4 withENABLE_RUST_LIBOBSOFF 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, systemlibobs-openglmasked, TMPDIR unset), with the native SRT/RIST output on: 268/268 in each lane. Skips:open_crashes_without_obs_coreintest_audio_ioandtest_video_io, as on master;test_startup_shutdown_cycles, whereobs_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.pyare clean.CI: Tier 2 Linux installs
librist-devandlibsrt-openssl-devand buildscmocka-testswith 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 fortest_srt_open_close. It is not registered on macOS/Windows, where the test's listener setup is untested.Checklist
master-W..., ratchets): noneAI 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