From 3236f9d6ad8abcfbcf93d9ce373539fac021b023 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 18 Oct 2026 12:28:10 -0400 Subject: [PATCH 1/2] test: Add failing Tier 4 phase for stopping an SRT listener Refs #1421 --- test/websocket-tier4/run_tier4.py | 179 +++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 2 deletions(-) diff --git a/test/websocket-tier4/run_tier4.py b/test/websocket-tier4/run_tier4.py index ce5dae3aa9..d0a34eea56 100644 --- a/test/websocket-tier4/run_tier4.py +++ b/test/websocket-tier4/run_tier4.py @@ -1438,6 +1438,132 @@ def test_sigterm_during_startup_dialog(server): ("OBS crashed" if crashed else "no clean shutdown in the log")) +def srt_udp_port(): + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: + sock.bind(("127.0.0.1", 0)) + return sock.getsockname()[1] + + +def ffmpeg_with_srt(): + """The host ffmpeg, if it can call an SRT listener; else None.""" + ffmpeg = shutil.which("ffmpeg") + if not ffmpeg: + return None + out = subprocess.run([ffmpeg, "-hide_banner", "-protocols"], + capture_output=True, text=True).stdout + return ffmpeg if any(l.strip() == "srt" for l in out.splitlines()) else None + + +async def srt_stream_status(c): + r = await c.request("GetStreamStatus") + return r.get("d", {}).get("responseData", {}) + + +async def start_srt_listener(c, port): + """Settings > Stream: Custom, srt://127.0.0.1:port?mode=listener.""" + for _ in range(120): + r = await c.request("SetStreamServiceSettings", { + "streamServiceType": "rtmp_custom", + "streamServiceSettings": { + "server": f"srt://127.0.0.1:{port}?mode=listener", "key": ""}}) + if resp_status(r).get("code") != 207: + break + await asyncio.sleep(0.5) + r = await c.request("StartStream") + return resp_status(r).get("code") == STATUS_SUCCESS, r + + +async def test_srt_listener_stop_while_reconnecting(): + """An SRT listener whose caller went away waits for the next caller + (RECONNECTING). Stop Streaming there must stop the stream, and the UI + thread must keep running. The stop joined the start thread, which waited + for a caller with no way to be interrupted: the stream never stopped + (upstream obsproject/obs-studio#12250).""" + ffmpeg = ffmpeg_with_srt() + if not ffmpeg: + print(" (no ffmpeg with srt: SRT reconnect check skipped)", flush=True) + return + c, _ = await new_identified_client(subscriptions=SUB_NONE) + port = srt_udp_port() + ok, r = await start_srt_listener(c, port) + check("srt:StartStream on a listener", ok, json.dumps(r)[:300]) + caller = subprocess.Popen( + [ffmpeg, "-hide_banner", "-nostdin", "-loglevel", "error", + "-i", f"srt://127.0.0.1:{port}?mode=caller", "-map", "0", + "-c", "copy", "-f", "mpegts", "-y", os.devnull], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + start_new_session=True) + try: + active = False + deadline = time.time() + 20 + while time.time() < deadline and not active: + active = (await srt_stream_status(c)).get("outputActive") + await asyncio.sleep(0.25) + check("srt:the stream starts when a caller connects", active, + "outputActive never became true") + await asyncio.sleep(2) + finally: + caller.kill() + caller.wait() + reconnecting = False + deadline = time.time() + 30 + while time.time() < deadline and not reconnecting: + reconnecting = (await srt_stream_status(c)).get("outputReconnecting") + await asyncio.sleep(0.25) + check("srt:the listener reconnects after the caller left", reconnecting, + "outputReconnecting never became true") + # Let the reconnect start waiting for a caller again. + await asyncio.sleep(4) + r = await c.request("StopStream") + check("srt:StopStream while reconnecting", + resp_status(r).get("code") == STATUS_SUCCESS, json.dumps(r)[:300]) + stopped = False + deadline = time.time() + 10 + while time.time() < deadline and not stopped: + try: + st = await c.request("GetStreamStatus", timeout=2.0) + except TimeoutError: + continue + data = st.get("d", {}).get("responseData", {}) + stopped = data.get("outputActive") is False and not data.get("outputReconnecting") + await asyncio.sleep(0.25) + check("srt:the stream stops within 10 s", stopped, + "still active or reconnecting 10 s after StopStream") + try: + r = await c.request("GetSceneList", timeout=5.0) + answers = resp_status(r).get("code") == STATUS_SUCCESS + except TimeoutError: + answers = False + check("srt:UI thread still answers after the stop", answers, + "GetSceneList timed out: the UI thread is stuck") + await c.close_conn() + + +async def test_srt_listener_quit_while_waiting(server): + """OBS must quit on SIGTERM while an SRT listener waits for its first + caller. Destroying the output joined the start thread, which waited for + a caller forever: OBS never quit.""" + c, _ = await new_identified_client(subscriptions=SUB_NONE) + ok, r = await start_srt_listener(c, srt_udp_port()) + check("srt-quit:StartStream on a listener", ok, json.dumps(r)[:300]) + await asyncio.sleep(2) + await c.close_conn() + pids = obs_pids(server) + check("srt-quit:OBS process found", bool(pids), f"no {server.obs_bin} process") + for pid in pids: + os.kill(pid, signal.SIGTERM) + deadline = time.time() + 30 + while time.time() < deadline and obs_pids(server): + time.sleep(0.2) + left = obs_pids(server) + time.sleep(1) + with open(server.log, errors="replace") as f: + log = f.read() + check("srt-quit:OBS quits on SIGTERM while the listener waits", + not left and "Number of memory leaks" in log, + "still running 30 s after SIGTERM" if left else "no clean shutdown in the log") + + async def run_shutdown_phase(obs_bin): """Simple output recording into a fresh folder, exit confirmation on.""" home = tempfile.mkdtemp(prefix="obs-tier4-") @@ -1606,6 +1732,48 @@ async def run_scripts_only(obs_bin): return 1 if failed else 0 +async def run_srt_phase(obs_bin): + """Simple output streaming to an SRT listener (srt:// Custom server).""" + profile = { + "Video": {"BaseCX": "640", "BaseCY": "360", "OutputCX": "640", + "OutputCY": "360", "FPSType": "0", "FPSCommon": "30"}, + "Output": {"Mode": "Simple"}, + "SimpleOutput": {"StreamEncoder": "x264", "VBitrate": "800"}, + } + server = ObsServer(obs_bin, PORT, password=None, profile=profile) + server.start() + try: + print("== SRT listener: stop while reconnecting ==", flush=True) + await test_srt_listener_stop_while_reconnecting() + except BaseException: + server.report_failure() + raise + finally: + server.stop() + + wait_for_port_closed(PORT) + server = ObsServer(obs_bin, PORT, password=None, profile=profile) + server.start() + try: + print("== SRT listener: quit while waiting for a caller ==", flush=True) + await test_srt_listener_quit_while_waiting(server) + except BaseException: + server.report_failure() + raise + finally: + server.stop() + + +async def run_srt_only(obs_bin): + await run_srt_phase(obs_bin) + failed = [n for n, ok, _ in RESULTS if not ok] + print("=" * 60) + print(f"checks: {len(RESULTS) - len(failed)}/{len(RESULTS)} passed") + for n, _, detail in (r for r in RESULTS if not r[1]): + print(f" - {n}: {detail}") + return 1 if failed else 0 + + async def run_gpu_phase(obs_bin): """OBS forced onto llvmpipe: GetStats.gpuAcceleration and the startup warning (obs-rust/obs-studio#846, #940); and one libsrt in the @@ -1713,6 +1881,10 @@ async def run_all(obs_bin, skip_obs, skip_auth_phase): wait_for_port_closed(PORT) await run_shutdown_phase(obs_bin) + if not skip_obs: + wait_for_port_closed(PORT) + await run_srt_phase(obs_bin) + total = len(RESULTS) passed = sum(1 for _, ok, _ in RESULTS if ok) failed = [n for n, ok, _ in RESULTS if not ok] @@ -1743,12 +1915,13 @@ def main(): "mainly for debugging)") ap.add_argument("--skip-auth-phase", action="store_true", help="with --no-spawn only: skip the auth-required phase") - ap.add_argument("--only", choices=["gpu", "shutdown", "scripts"], + ap.add_argument("--only", choices=["gpu", "shutdown", "scripts", "srt"], help="run one group: gpu = GetStats.gpuAcceleration and the " "startup warning with OpenGL forced onto llvmpipe; " "shutdown = SIGTERM while recording and during a " "startup dialog; scripts = a Lua script switching " - "scenes from a timer") + "scenes from a timer; srt = stopping an SRT listener " + "and quitting while one waits") args = ap.parse_args() if args.only == "gpu": sys.exit(asyncio.run(run_gpu_only(args.obs))) @@ -1756,6 +1929,8 @@ def main(): sys.exit(asyncio.run(run_shutdown_only(args.obs))) if args.only == "scripts": sys.exit(asyncio.run(run_scripts_only(args.obs))) + if args.only == "srt": + sys.exit(asyncio.run(run_srt_only(args.obs))) sys.exit(asyncio.run( run_all(args.obs, args.no_spawn, args.skip_auth_phase))) From f13ef5dcc7a345f6b6d938895e61e4743d1f849c Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 18 Oct 2026 12:30:49 -0400 Subject: [PATCH 2/2] obs-ffmpeg: Let a stop end an SRT listener's wait for its caller The SRT and RIST URLContext had no interrupt callback, and an SRT listener has no listen timeout by default, so its start thread waited for a caller for as long as it took. Every stop joins that thread first: Stop Streaming while a listener waited for its caller to come back never stopped the stream, and destroying the output at quit never returned. A stop now sets abort_connect before it joins, and the interrupt callback returns it, so the wait ends within 100 ms. A connect that a stop ended does not signal a connect failure: while reconnecting, libobs would take that as a reason to reconnect again. Fixes #1421 --- docs/features.md | 6 ++++++ plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c.h | 20 +++++++++++++++++++- plugins/obs-ffmpeg/obs-ffmpeg-output.h | 3 +++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/features.md b/docs/features.md index aeaaf3a1cf..30ce92177a 100644 --- a/docs/features.md +++ b/docs/features.md @@ -858,6 +858,12 @@ area. fails. *All*, 2026-10-17. Issue [#1347](https://github.com/obs-rust/obs-studio/issues/1347), PR [#1353](https://github.com/obs-rust/obs-studio/pull/1353). +- Stop Streaming stops an SRT listener stream that is waiting for its caller + to come back, and OBS quits while a listener waits; before, the stream + never stopped and OBS never finished quitting. *All*, 2026-10-18. Issue + [#1421](https://github.com/obs-rust/obs-studio/issues/1421), PR + [#1422](https://github.com/obs-rust/obs-studio/pull/1422), upstream + [obsproject/obs-studio#12250](https://github.com/obsproject/obs-studio/issues/12250). ### Sources and filters diff --git a/plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c.h b/plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c.h index bc0666b39b..4552f38dd9 100644 --- a/plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c.h +++ b/plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c.h @@ -264,6 +264,12 @@ int ff_network_init(void) return 1; } +static int connect_interrupted(void *opaque) +{ + struct ffmpeg_output *stream = opaque; + return os_atomic_load_bool(&stream->abort_connect); +} + static inline int connect_mpegts_url(struct ffmpeg_output *stream, bool is_rist) { int err = 0; @@ -281,6 +287,11 @@ static inline int connect_mpegts_url(struct ffmpeg_output *stream, bool is_rist) } uc->url = (char *)(uintptr_t)url; + /* Without it, nothing can end the wait of an SRT listener for its + * caller (no listen_timeout by default): every stop joins the start + * thread and would wait as long. */ + uc->interrupt_callback.callback = connect_interrupted; + uc->interrupt_callback.opaque = stream; uc->max_packet_size = is_rist ? RIST_MAX_PAYLOAD_SIZE : SRT_LIVE_DEFAULT_PAYLOAD_SIZE; if (stream->ff_data.config.srt_pkt_size) @@ -1031,7 +1042,11 @@ static bool set_config(struct ffmpeg_output *stream) return true; fail: - obs_output_signal_stop(stream->output, code); + /* A connect that a stop ended is no failure; the stop signals it. While + * reconnecting, libobs would also take the failure as a reason to + * reconnect again. */ + if (!os_atomic_load_bool(&stream->abort_connect)) + obs_output_signal_stop(stream->output, code); stop(stream, false, 0); return false; } @@ -1113,6 +1128,8 @@ static void ffmpeg_mpegts_stop_internal(void *data, uint64_t ts, bool signal) bool have_to_join = false; pthread_t to_join; + os_atomic_set_bool(&stream->abort_connect, true); + /* macOS fix: handover of pthread; for some reason, maybe related to srt sockets, if the pthread joining is * done within the mutex, there can be a stall. So we just copy it and join out of the mutex. */ pthread_mutex_lock(&stream->start_stop_mutex); @@ -1171,6 +1188,7 @@ static bool ffmpeg_mpegts_start(void *data) stream->video_start_ts = 0; stream->total_bytes = 0; stream->got_headers = false; + os_atomic_set_bool(&stream->abort_connect, false); pthread_create(&stream->start_stop_thread, NULL, start_stop_thread_fn, cmd); os_atomic_set_bool(&stream->start_stop_thread_active, true); diff --git a/plugins/obs-ffmpeg/obs-ffmpeg-output.h b/plugins/obs-ffmpeg/obs-ffmpeg-output.h index 27107276ca..7f3f31a618 100644 --- a/plugins/obs-ffmpeg/obs-ffmpeg-output.h +++ b/plugins/obs-ffmpeg/obs-ffmpeg-output.h @@ -126,6 +126,9 @@ struct ffmpeg_output { pthread_mutex_t start_stop_mutex; volatile bool start_stop_thread_active; bool has_connected; + /* set by a stop: ends a connect that is still waiting (an SRT + * listener waiting for its caller) */ + volatile bool abort_connect; #endif };