Skip to content

obs-scripting: Lua graphics lock retry yield-spins on the graphics thread with no deadline #1275

Closed

Description

@zackees

Part of #1273. A.

shared/obs-scripting/obs-scripting-lua-source.c.h:193-197, on the graphics thread:

while (pthread_mutex_trylock(mutex) != 0) {
	if (os_atomic_load_long(&lua_graphics_waiters) > 0)
		return false;
	os_sleep_ms(1);
}

It has no time limit; it only exits once the lock is free or another graphics waiter appears. On Windows os_sleep_ms(1) is Sleep(0) (#1274), so while a Lua script holds its mutex the graphics thread yield-spins, on the same thread that renders every frame.

Fix: a real wait: a bounded pthread_mutex_timedlock-style wait, or an event or condvar signalled on unlock, with a deadline. Keep the existing "another waiter" escape.

Test: hold the mutex from another thread and count loop iterations, or the CPU time used by the waiting thread, over 100 ms; it must stay bounded.

Activity

  1. nathan-v commented on Oct 20, 2026

    @nathan-v
    Contributor

    Claiming: #1275 Lua graphics-lock wait characterization.

    1. Characterize the current lock_in_graphics behavior, including uncontended acquisition, the non-graphics path, retries under contention, and the graphics-waiter escape.
    2. Add deterministic coverage against the production C function and run it on macOS; record the scope and validation limits.

    This session is scoped to characterization only, before a separate RED regression and fix. ETA: characterization commit and evidence this session; no fix PR promised in this phase. Windows-specific Sleep(0) CPU behavior will remain unverified here.

  2. added
    claimedAn agent or person has claimed this; check the claim comment before starting
    on Oct 20, 2026
  3. nathan-v commented on Oct 20, 2026

    @nathan-v
    Contributor

    Characterization completed locally at 265886747 on branch test/1275-lua-graphics-lock-characterization, based on master 7a56d867e. No production changes; no fix or RED regression yet.

    Eight cmocka cases compile the actual private C helper via a configure-time slice, with controlled context, mutex, atomic-read and sleep dependencies. They cover the blocking path, immediate acquisition (even with a waiter), contention retries, existing/new waiter escape, acquisition before the waiter check, 10,000 failed attempts with fixed 1 ms sleep requests, and retries on non-EBUSY errors. The last two are characterized, not endorsed.

    macOS validation: AppleClang 21, cmocka 2.0.2, -Wall -Wextra -Werror; all eight pass, and CTest passes 100 consecutive runs. Changing the generated waiter threshold from > 0 to > 1 fails both escape cases; restoring production passes. Pinned clang-format 22.1.3 and gersemi 0.25.0 checks pass.

    The new tests.d fragment was built through a minimal standalone CMake driver. Full OBS build and OFF/ON Tier 2 were not run. These tests characterize control flow and requested sleeps, not real Lua/rendering, native thread scheduling, elapsed wait time, or Windows CPU usage. The branch is local and not pushed. Stopping at the requested characterization checkpoint.

  4. nathan-v commented on Oct 20, 2026

    @nathan-v
    Contributor

    Continuing the claim with RED → GREEN after characterization PR #1496 (queued).

    Decisions: use a single nonblocking try-lock while holding the graphics context. The existing callers already return cached dimensions or skip rendering on a failed lock. This avoids polling and scheduler-dependent waits altogether; the non-graphics path keeps blocking acquisition. A contended Lua source may skip a frame until its lock is available, rather than stalling every source. Preserve the old characterization against a frozen original helper, and add separate production regression tests. ETA: RED draft PR and GREEN implementation this session.

  5. removed
    claimedAn agent or person has claimed this; check the claim comment before starting
    on Oct 22, 2026
  6. zackees commented on Oct 25, 2026

    @zackees
    MemberAuthor

    /ready

    Triage: concrete fix and test plan, claimed by @nathan-v with the characterization PR (#1496) first and a decision recorded (a single nonblocking try-lock while holding the graphics context, relying on the callers' existing cached-dimensions/skip-render path). Acceptance: no polling loop on the graphics thread, and the existing "another waiter" escape and cached-dimension behaviour covered by the RED → GREEN test. P2 (it can stall the render thread), size:S.

  7. zackees commented on Oct 25, 2026

    @zackees
    MemberAuthor

    Correction to my triage above: this is already fixed. #1498 (merged 2026-10-20, after the characterization in #1496) replaced the retry loop with a single nonblocking try-lock (shared/obs-scripting/obs-scripting-lua-source.c.h:189 on master: return pthread_mutex_trylock(mutex) == 0;), so the graphics thread no longer spins. Thanks @nathan-v.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal priorityreadyMeets the definition of Ready; claimablesize:SHours

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions