From 5b0540dfe152f0c691f09c4c60854e88e27a55ea Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 18 Oct 2026 11:05:42 -0400 Subject: [PATCH 1/2] test: Add failing Tier 4 phase for a Lua timer that switches scenes A Lua script that calls obs_frontend_set_current_scene() from a timer and also has a frontend event callback hangs OBS (obsproject/obs-studio#3385, reported by woganmay). Script timers run on libobs's graphics thread holding the scripting callback lock. The call waits for the UI thread, and the UI thread, sending the scene change to the script's event callback, waits for that lock. The new phase (run_tier4.py --only scripts, also part of the full run) starts OBS with scenes A and B and such a script, which switches to B from a 2 s timer. It checks that the call returns, that B is on program and that the UI thread still answers SetCurrentProgramScene. On v0.1.0-beta.20261018 and master 065fa7d6c 2 of 4 checks fail: the call never returns and SetCurrentProgramScene times out. Refs #1416 --- test/websocket-tier4/run_tier4.py | 125 +++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 2 deletions(-) diff --git a/test/websocket-tier4/run_tier4.py b/test/websocket-tier4/run_tier4.py index ad5d5f7498..ce5dae3aa9 100644 --- a/test/websocket-tier4/run_tier4.py +++ b/test/websocket-tier4/run_tier4.py @@ -1482,6 +1482,110 @@ async def run_shutdown_phase(obs_bin): server.stop() +# A Lua script that switches scenes from a timer while it also has a frontend +# event callback (obsproject/obs-studio#3385, obs-rust/obs-studio#1416). +# Script timers run on libobs's graphics thread, holding the scripting +# callback lock. obs_frontend_set_current_scene() waited there for the UI +# thread, and the UI thread, sending the scene change to the script's event +# callback, waited for that lock: OBS hung. +LUA_SCENE_SWITCH = """obs = obslua +local fired = false + +function on_event(event) + if event == obs.OBS_FRONTEND_EVENT_SCENE_CHANGED then + print("tier4-lua: scene changed") + end +end + +function tick() + if fired then + return + end + fired = true + obs.timer_remove(tick) + local scenes = obs.obs_frontend_get_scenes() + for _, scene in ipairs(scenes) do + if obs.obs_source_get_name(scene) == "B" then + print("tier4-lua: switching to B") + obs.obs_frontend_set_current_scene(scene) + print("tier4-lua: switch returned") + end + end + obs.source_list_release(scenes) +end + +function script_load(settings) + obs.obs_frontend_add_event_callback(on_event) + obs.timer_add(tick, 2000) +end +""" + + +def write_lua_scene_switch_collection(home): + """Scene collection Tier4: scenes A (current) and B, with the script + above loaded by the Scripts tool.""" + script = os.path.join(home, "scene-switch.lua") + with open(script, "w") as f: + f.write(LUA_SCENE_SWITCH) + scenes = os.path.join(home, ".config", "obs-studio", "basic", "scenes") + os.makedirs(scenes, exist_ok=True) + collection = { + "name": "Tier4", "current_scene": "A", "current_program_scene": "A", + "scene_order": [{"name": "A"}, {"name": "B"}], + "sources": [{"id": "scene", "versioned_id": "scene", "name": name, + "settings": {"items": []}} for name in ("A", "B")], + "modules": {"scripts-tool": [{"path": script, "settings": {}}]}, + } + with open(os.path.join(scenes, "Tier4.json"), "w") as f: + json.dump(collection, f) + + +async def test_lua_timer_scene_switch(server): + log = "" + deadline = time.time() + 30 + while time.time() < deadline and "tier4-lua: switching to B" not in log: + await asyncio.sleep(0.25) + with open(server.log, errors="replace") as f: + log = f.read() + check("scripts:Lua timer reached obs_frontend_set_current_scene", + "tier4-lua: switching to B" in log, + "the script never ran: is Lua scripting in this build?") + await asyncio.sleep(3) + with open(server.log, errors="replace") as f: + log = f.read() + check("scripts:obs_frontend_set_current_scene from a Lua timer returns", + "tier4-lua: switch returned" in log, + "the script's timer is still inside obs_frontend_set_current_scene") + c, _ = await new_identified_client(subscriptions=SUB_NONE) + try: + r = await c.request("GetCurrentProgramScene", timeout=10.0) + name = r.get("d", {}).get("responseData", {}).get("sceneName") + check("scripts:the scene the Lua timer picked is on program", + name == "B", f"program scene {name!r}") + r = await c.request("SetCurrentProgramScene", {"sceneName": "A"}, + timeout=10.0) + check("scripts:UI thread still answers after the switch", + resp_status(r).get("code") == STATUS_SUCCESS, json.dumps(r)[:300]) + except TimeoutError as e: + check("scripts:UI thread still answers after the switch", False, + f"{e}: OBS hangs") + finally: + await c.close_conn() + + +async def run_scripts_phase(obs_bin): + home = tempfile.mkdtemp(prefix="obs-tier4-") + write_lua_scene_switch_collection(home) + server = ObsServer(obs_bin, PORT, password=None, keep_home=home, + profile={}) + server.start() + try: + print("== Lua timer scene switch ==", flush=True) + await test_lua_timer_scene_switch(server) + finally: + server.stop() + + async def run_shutdown_only(obs_bin): await run_shutdown_phase(obs_bin) failed = [n for n, ok, _ in RESULTS if not ok] @@ -1492,6 +1596,16 @@ async def run_shutdown_only(obs_bin): return 1 if failed else 0 +async def run_scripts_only(obs_bin): + await run_scripts_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 @@ -1587,6 +1701,10 @@ async def run_all(obs_bin, skip_obs, skip_auth_phase): finally: server_c.stop() + if not skip_obs: + wait_for_port_closed(PORT) + await run_scripts_phase(obs_bin) + if not skip_obs: wait_for_port_closed(PORT) await run_gpu_phase(obs_bin) @@ -1625,16 +1743,19 @@ 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"], + ap.add_argument("--only", choices=["gpu", "shutdown", "scripts"], 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") + "startup dialog; scripts = a Lua script switching " + "scenes from a timer") args = ap.parse_args() if args.only == "gpu": sys.exit(asyncio.run(run_gpu_only(args.obs))) if args.only == "shutdown": sys.exit(asyncio.run(run_shutdown_only(args.obs))) + if args.only == "scripts": + sys.exit(asyncio.run(run_scripts_only(args.obs))) sys.exit(asyncio.run( run_all(args.obs, args.no_spawn, args.skip_auth_phase))) From af0db1a2ad9157c2a11d135b7157752da94740b9 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 18 Oct 2026 11:06:15 -0400 Subject: [PATCH 2/2] frontend: Queue scene changes that scripts make from a timer obs_frontend_set_current_scene() ran the scene change on the UI thread and waited for it. Script timers run on libobs's graphics thread with the scripting callback lock held (the GIL for Python), and the scene change sends frontend events to the scripts' event callbacks on the UI thread, which take that lock. A script that switched scenes from a timer and also had an event callback deadlocked OBS (obsproject/obs-studio#3385). Called on libobs's graphics, audio or destroy thread, the scene change is now queued and the call returns, as the profile calls already do through RunFrontendApiCall() (#805). Called from any other thread (obs-websocket, plugins' own threads) it still waits until the scene has changed. The Studio Mode path (TransitionToScene) does the same. The Tier 4 scripts phase passes 4 of 4. Fixes #1416 --- docs/features.md | 6 ++++++ frontend/OBSStudioAPI.cpp.hpp | 29 +++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/docs/features.md b/docs/features.md index 46754d2ebc..76d159d884 100644 --- a/docs/features.md +++ b/docs/features.md @@ -1247,6 +1247,12 @@ area. [#383](https://github.com/obs-rust/obs-studio/issues/383), PR [#384](https://github.com/obs-rust/obs-studio/pull/384), upstream [obsproject/obs-studio#8936](https://github.com/obsproject/obs-studio/issues/8936). +- A Lua or Python script that switches scenes from a timer no longer hangs + OBS when it also has a frontend event callback; the scene changes right + after the timer returns. *All*, 2026-10-18. Issue + [#1416](https://github.com/obs-rust/obs-studio/issues/1416), PR + [#1417](https://github.com/obs-rust/obs-studio/pull/1417), upstream + [obsproject/obs-studio#3385](https://github.com/obsproject/obs-studio/issues/3385). ### Stability diff --git a/frontend/OBSStudioAPI.cpp.hpp b/frontend/OBSStudioAPI.cpp.hpp index 5ee09e43c0..0494254710 100644 --- a/frontend/OBSStudioAPI.cpp.hpp +++ b/frontend/OBSStudioAPI.cpp.hpp @@ -58,14 +58,27 @@ obs_source_t *OBSStudioAPI::obs_frontend_get_current_scene() } } +/* libobs's graphics, audio and destroy threads must not wait for the UI + * thread: the UI thread can wait for them. Activating a profile resets video + * and audio, which waits for the graphics and audio threads. Script timers + * run on the graphics thread holding the scripting callback lock, and a scene + * change sends events to the scripts' callbacks, which take that lock + * (obsproject/obs-studio#3385). */ +static bool InLibobsThread() +{ + return obs_in_task_thread(OBS_TASK_GRAPHICS) || obs_in_task_thread(OBS_TASK_AUDIO) || + obs_in_task_thread(OBS_TASK_DESTROY); +} + void OBSStudioAPI::obs_frontend_set_current_scene(obs_source_t *scene) { if (main->IsPreviewProgramMode()) { - QMetaObject::invokeMethod( - main, [this, scene = OBSSource(scene)]() { main->TransitionToScene(scene); }, WaitConnection()); + RunFrontendApiCall( + main, [this, scene = OBSSource(scene)]() { main->TransitionToScene(scene); }, InLibobsThread()); } else { - QMetaObject::invokeMethod(main, qOverload(&OBSBasic::SetCurrentScene), - WaitConnection(), OBSSource(scene), false); + RunFrontendApiCall( + main, [this, scene = OBSSource(scene)]() { main->SetCurrentScene(scene, false); }, + InLibobsThread()); } } @@ -202,14 +215,6 @@ void OBSStudioAPI::obs_frontend_set_current_profile(const char *profile) } } -/* Activating a profile can reset video and audio, which wait for libobs's - * graphics and audio threads. */ -static bool InLibobsThread() -{ - return obs_in_task_thread(OBS_TASK_GRAPHICS) || obs_in_task_thread(OBS_TASK_AUDIO) || - obs_in_task_thread(OBS_TASK_DESTROY); -} - void OBSStudioAPI::obs_frontend_create_profile(const char *name) { RunFrontendApiCall(