Skip to content

bug: OBS hangs after shutdown when a plugin's static destructor blocks (exit watchdog) #4065

Closed

Description

@zackees

Promoted from discussion #4051 (@vpacher).

Problem

OBS can finish its own shutdown and still not quit. In #3739 the log ends with Number of memory leaks, and the process stays alive with no window. The hang was in a static destructor of a third-party plugin: at exit(), a library in the plugin joined a thread that never stopped. OBS had no code left on the stack.

OBS cannot stop one plugin: a plugin is a shared library inside the OBS process, not a process of its own. libobs also never unloads modules (os_dlclose is commented out in free_module), so every plugin's destructors run at exit(), after main() returns.

For the user this is a process that never ends — it can block the next start, a restart, or a system shutdown. For us it is a long hunt: #3739 needed a test machine, the reporter's plugin set and a debugger to identify the plugin.

Proposed fix (from the discussion)

An exit watchdog for the last step of the process:

  1. In frontend/obs-main.cpp.hpp, after obs_main_host_shutdown() and the Number of memory leaks line, start a detached thread with a timeout of some seconds.
  2. If the process still runs when the timeout expires, the thread writes one line naming the situation ("exit did not finish in N s: a static destructor or atexit handler is blocking") and terminates the process rather than hanging forever.

Why high priority

  • It produces a user-visible hang that survives shutdown, blocking restarts and system power-off, and it is indistinguishable from a crash for support purposes.
  • Diagnosis is expensive: the failing plugin is invisible in the OBS log, and finding it required a debugger and a reproduction from the reporter.
  • The fix is small, local (one file), and converts an open-ended hang into a bounded, diagnosable exit.

Acceptance criteria

  • After obs_main_host_shutdown() and the leak report, a watchdog with a bounded timeout is armed.
  • If shutdown completes normally, the watchdog never fires and nothing is logged.
  • If a static destructor/atexit handler blocks past the timeout, OBS writes exactly one diagnostic line naming the timeout, and the process exits non-zero instead of hanging.
  • The timeout is configurable or at least a single named constant; a machine-readable log line is preferred over prose.
  • A test (or a documented manual reproduction with a plugin that joins a never-terminating thread) proves the watchdog fires and the process ends.
  • No behaviour change when shutdown is healthy: no extra threads left running, no measurable delay on a normal exit.

Notes

  • The watchdog must not mask a genuine slow shutdown: the timeout needs to be comfortably longer than normal teardown on a loaded machine, and the log line must say it was the watchdog, not OBS, that ended the process.
  • Consider whether to name the loaded modules in the diagnostic, so the next occurrence identifies the plugin without a debugger.

Activity

  1. added
    bug
    P1
    on Nov 8, 2026
  2. vpacher commented on Nov 8, 2026

    @vpacher
    Member

    A proposal for the "name the loaded modules" note, as follow-ups after the watchdog itself. All of it is in libobs and the frontend: no change to the module API or ABI, and existing plugin binaries need no rebuild.

    Why the module list is not enough

    libobs already logs Loaded Modules: at startup, but in #3739 that list would not have named the cause:

    • The hang was in a sub-plugin of Advanced Scene Switcher (its MIDI support, with libremidi). advss loads its sub-plugins with its own dlopen, so libobs never sees them. The list names only advss.
    • The blocking object was a function-local static, made at run time, not when the module loaded.
    • obs_shutdown() frees obs->first_module before the watchdog arms, so the watchdog cannot read the list at that point.

    Proposal

    1. Record which module brought in which libraries. Around os_dlopen(), obs_init_module() and post_load of each module, compare the process's loaded images before and after. The new images belong to that module (for example advanced-scene-switcher: midi.so, libremidi.so). Keep the table until exit; it does not depend on obs->first_module.

    • Linux: dl_iterate_phdr. macOS: _dyld_register_func_for_add_image or _dyld_image_count. Windows: EnumProcessModules or LdrRegisterDllNotification.
    • The diff is a pure function (before set, after set, owner), so a Tier 1 test covers it.
    • Limits: OBS loads modules one at a time, but a plugin thread that loads a library during another module's load gives it the wrong owner. Libraries loaded later at run time show as "loaded after startup", with no owner. To get those right, OBS would have to intercept dlopen; we do not propose that, because it changes what plugins call.

    2. Name the library where the main thread waits. When the watchdog fires, take the main thread's stack, map each return address to an image of the table, and write the first image that is not a system library, with its owner from 1, in the same line:

    exit-watchdog: exit did not finish in 10 s; main thread waits in midi.so (loaded by advanced-scene-switcher)
    
    • Linux: send a real-time signal to the main thread. The handler only calls backtrace() into a static buffer; the watchdog thread does the lookup and the write. The handler is installed only when the watchdog fires, so it cannot conflict with a plugin's signal use during normal operation.
    • macOS: no signal. thread_suspend() and thread_get_state() on the main thread, then a frame-pointer walk.
    • Windows: to test first. As far as I know, ExitProcess ends the other threads before DLL_PROCESS_DETACH, so the watchdog thread may not be alive when DLL destructors run, and the Quitting the GUI does not end the process on Linux #3739 pattern may not hang there in the same way.

    Considered and not proposed

    An atexit breadcrumb after each module load (the last one that ran brackets the blocking module). It misses statics made at run time, which was the #3739 case, and destructor functions that do not go through the atexit list.

    Suggested order

    1. The watchdog as this issue describes it: no naming.
    2. The image table (1) on Linux and macOS.
    3. The stack lookup (2) on Linux and macOS. The manual reproduction from the acceptance criteria (a plugin with a static that joins a thread that never ends) also checks that the line names that plugin.
    4. Windows, after a test of how the hang shows there.

    Questions

    1. Should the startup log list the extra libraries of each module? If yes, at info level with system libraries left out, or only at debug level?
    2. File names only, as Loaded Modules: does? Full paths can contain the user name.
  3. vpacher commented on Nov 9, 2026

    @vpacher
    Member

    /ready

    Claiming: the exit watchdog (step 1 of the order in my comment above; no module naming in this PR).

    Plan, one PR:

    1. RED: a new crate rust/obs-exit-watchdog (laid out like obs-camera-busy: src/ffi/, src/platform/), re-exported from rust/obs-frontend, with todo!() stubs. Tier 1 tests as a module in obs-tests-gpu (the frontend's Rust category), no new test binary. Each case re-runs the test binary with an exact selector:
      • a child arms the watchdog with 200 ms and a temporary log file, then blocks: the child must end within 2 s with the watchdog's exit code, and the file must have exactly one watchdog line;
      • a child arms the watchdog and returns normally: exit code 0, no line, no delay.
    2. GREEN: the crate, a header in frontend/utility/, and the call in frontend/obs-main.cpp.hpp after the Number of memory leaks line.
    3. A manual run of the real app with a test plugin whose static destructor joins a thread that never ends; the line and the exit code go in the PR. A docs/features.md line.

    First PR (RED) expected today.

    Decisions:

    • All work at arm time. When the watchdog fires, main() has returned, the log file object is closed and static destructors are running. So arming opens the current log file in append mode and formats the line; firing only writes it (to the log file and stderr) and ends the process with _exit() (Linux, macOS) or TerminateProcess() (Windows). Not exit(), which runs the same destructors again, and not abort(), which looks like a crash.
    • Line and exit code. One line, exit-watchdog: exit did not finish in <N> s: a static destructor or atexit handler is blocking; ending the process, and a named exit code that is not 0.
    • Timeout. A named constant. I measure a normal exit (leak line to end of process) on Linux and macOS first, and set the value with a large margin; the PR gives both numbers. The tests pass a short timeout through the same function.
    • Normal exit. The only cost is one detached thread that sleeps and ends with the process. Nothing waits for it.
    • Restart. The restart starts the new OBS before the old one exits. This PR does not change that; the watchdog only makes sure the old process ends.
    • Windows. Built and armed the same way. ExitProcess ends the other threads before the DLLs' destructors run, so the watchdog thread is probably gone when a plugin DLL blocks. Covering that needs a test on Windows first, as a follow-up.
    • Without the frontend's Rust (the build without cargo): no watchdog, as for the other features in rust/obs-frontend.

    Master is red on push runs from the windows-gnu oracle build (#4168, fix in #4191); that does not touch this path.

  4. added
    ready
    on Nov 9, 2026
  5. vpacher commented on Nov 9, 2026

    @vpacher
    Member

    Claiming: the exit watchdog, PR #4309 (plan and decisions in my comment above).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1bugready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions