Skip to content

obs-util: Port Linux sleep inhibition to Rust - #1504

Merged
zackees merged 3 commits into
obs-rust:masterfrom
rdoupe:rust/port-inhibit-sleep
Oct 20, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
rdoupe:rust/port-inhibit-sleep

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Stacked: on #1503 (test: Characterize Linux sleep inhibition before its port, commit 506f04bc2). Review and merge that first. This branch holds its commit too, and the two commits after it are this PR.

Fixes #1500

What and why

os_inhibit_sleep_create/_set_active/_destroy on Linux and FreeBSD (libobs/util/platform-nix-inhibit.c.h) move to obs-util::platform::unix::nix_inhibit. The C ABI shim is obs-util/src/ffi/platform_nix_inhibit.rs. libobs-with-rust links the shim. The C file moves to its own ported group, libobs.ported-nix-inhibit (with its own PCH), listed in LIBOBS_LINUX_PORTED_SOURCES. The public header is unchanged.

The first platform-nix port (#403) dropped the portal and session D-Bus inhibitors (#453), and #454 split this file out to keep them. They stay C here too (platform-nix-portal.c, platform-nix-dbus.c, group libobs.nix-dbus), and the Rust core calls them in the C's order:

  • the portal first;
  • the session D-Bus inhibitors only when there is no portal;
  • nothing inside a Flatpak with no portal;
  • otherwise the xdg-screensaver reset thread, every 30 s.

GIO_FOUND reaches Rust as the libobs-rust feature gio (→ obs-util/gio). libobs/cmake/os-linux.cmake and os-freebsd.cmake turn off the default features and set gio from find_package(Gio). Corrosion then runs --no-default-features --features=gio, or --no-default-features without GIO. The feature is on by default for a plain Cargo build. A forgotten flag then keeps the portal path, and a libobs without GIO fails to link instead of losing inhibition silently. Nothing is decided at run time.

Intentional differences (also in the parity test header, the core's module docs and rust/README.md):

  • The inhibitor is a Box and the reason a CString. C uses bzalloc and bstrdup.
  • A thread that cannot start makes set_active return false, and the inhibitor stays inactive. C checks pthread_create(...) < 0, which never holds, so it returns true and later joins a thread that does not exist.
  • The reset retries its wait for xdg-screensaver only when interrupted. C retries any waitpid failure, so with SIGCHLD ignored (ECHILD) it never stops trying.

The child is started with std's Command: an empty signal mask and default SIGPIPE, like the C's posix_spawnattr_t. The safe core has no unsafe.

Other changes:

  • obs-nix.c shared the old PCH and got <unistd.h> from this file's section, so its section now includes it.
  • The stale "inhibit stays C" comments in platform-nix and its parity test are updated.

RED → GREEN

  • Characterization: test: Characterize Linux sleep inhibition before its port #1503 (506f04bc2). test_inhibit_sleep and test_inhibit_sleep_dbus are unchanged here (git diff 506f04bc2 HEAD -- test/ ci/ is empty).
  • RED commit: 91c1ffac0 obs-util: Fail Linux sleep inhibition on stubs. soldr cargo test -p obs-util --lib tests::platform::nix_inhibit:: fails 24 of 25 with not yet implemented: create. null_info_is_rejected passes.
  • GREEN: c8f63a372. Tier 1: 25/25. They port both C tests case for case, under the same names. They also cover the reset thread without the 30 s wait, a failed spawn, a failed thread start, the Flatpak branch, a build without GIO, and the real spawn and its ENOENT.
  • Tier 3 (nix_inhibit_parity):
    • How it works: the oracle compiles the original file twice, as oracle_gio_* and as oracle_*. What the file calls outside itself goes to counted C stand-ins (obs-c-oracle/oracle/nix_inhibit_stubs_posix.c): the inhibitors, access("/.flatpak-info"), posix_spawnp, blog, and the reset thread's wait, which times out on request. The Rust core's test host calls the same stand-ins, and the driver waits for each reset thread to park. Returns and calls are compared line for line, in order.
    • cmocka_sequences_match_oracle: the C tests' step lists, for every mix of GIO, portal, D-Bus and Flatpak.
    • random_sequences_match_oracle: 96 proptest cases of create, set_active (also on NULL), destroy and resets, with random reasons including bytes that are not UTF-8.
    • shim_sequences_match_oracle: 32 cases of the real os_inhibit_sleep_* against the oracle with this build's GIO setting. It passes with and without --features gio.
  • Deliberate breaks, each reverted:
    • resetting before the first wait fails both core parity tests: spawn comes before wait 30000 timeout;
    • dropping the Flatpak check fails both parity tests and flatpak_without_portal_gives_no_inhibitor;
    • passing NULL for the reason in the shim fails shim_sequences_match_oracle with --features gio.
  • The workspace: uv run ci/local_gate.py --lane rust-test: ok. 1478 passed, 0 failed; the obs-util lib harness 616/616. --lane lint: ok (clippy -D warnings, rustfmt, dylints, clang-format 22.1.3, gersemi). --lane cross (aarch64-apple-darwin, x86_64-pc-windows-msvc): ok. Clippy -D warnings also on obs-util with and without gio.
  • Tier 2, headless (private Xvfb, private D-Bus, fresh HOME). Native, not through rust/tools/linux-validate/run.sh, which needs Docker and could not run here:
Build ctest test_inhibit_sleep test_inhibit_sleep_dbus Exported symbols
C lane (ENABLE_RUST_LIBOBS=OFF) 326/326, 0 skipped 8/8 10/10
Rust lane, libobs (OFF) 326/326, 0 skipped 8/8 10/10 1766
Rust lane, libobs-with-rust (ON) 326/326, 0 skipped 8/8 10/10 1766, identical
Rust lane without GIO (CMAKE_DISABLE_FIND_PACKAGE_Gio), OFF / ON not run 8/8 / 8/8 not built (needs GIO) 1766 / 1766, identical
  • ctest -R '^test_inhibit_sleep' --repeat until-fail:20: passed in all three GIO builds. check-skips.sh passes on all three ctest logs.
  • In ON, os_inhibit_sleep_* are the Rust shims and call the hidden C portal_inhibit_info_create and dbus_sleep_info_create. The build without GIO links no portal or D-Bus code.
  • Real app: not run. test_inhibit_sleep_dbus drives the real libobs entry points against a fake portal and screensaver on a private bus, in both binaries. No UI, protocol or log change.

Not checked here: FreeBSD (built by CMake the same way as Linux; no cross lane for it), and the Docker Tier 2 harness, which PR CI runs.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: not applicable, no user-visible change
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes: none
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

Pins os_inhibit_sleep_* in libobs/util/platform-nix-inhibit.c on Linux
and FreeBSD before the Rust port, so the port cannot drop a path the way
the first platform-nix port dropped the portal and D-Bus inhibitors.
No Rust and no production code change.

test_inhibit_sleep (new) covers the path with no session D-Bus: the bus
address points at a missing socket and PATH holds only a fake
xdg-screensaver, so the user's session is never touched. It pins a NULL
inhibitor, set_active returning true only on a change, 50
activate/deactivate cycles, destroy while active joining the thread at
once, NULL, empty, long and non-ASCII reasons, and two independent
inhibitors. The first reset waits the full 30 s, so the fake never runs.

test_inhibit_sleep_dbus gains seven cases on its private bus: the
portal is used and the session screensaver is not when both exist,
repeated set_active on the portal and on org.freedesktop.ScreenSaver
reaches the service once per change, destroy while active releases the
inhibit, the reason is copied at create byte for byte, and an empty bus
falls back without calling either service.

Without dbus-daemon it used to skip every case, which cmocka reports as
a pass, so a Tier 2 image without dbus would have passed it silently.
It now exits 77 (SKIP_RETURN_CODE), and check-skips.sh refuses a skipped
test_inhibit_sleep* in Tier 2.

Not pinned: the Flatpak branch (needs /.flatpak-info at the root) and a
NULL reason with a portal or D-Bus inhibitor (reaches
g_variant_new_string(NULL)).

Refs obs-rust#1500
RED for the port of libobs/util/platform-nix-inhibit.c: the safe core's
API in obs-util::platform::unix::nix_inhibit with todo!() bodies, and its
Tier 1 tests against a host that records every call.

The tests port test_inhibit_sleep.c and test_inhibit_sleep_dbus.c case
for case (same names), then add what a C test cannot reach: the reset
thread without the 30 s wait, a failed spawn, a failed thread start, the
Flatpak branch and a build without GIO.

soldr cargo test -p obs-util --lib tests::platform::nix_inhibit:: fails
24 of 25 with "not yet implemented"; null_info_is_rejected passes.

Refs obs-rust#1500
os_inhibit_sleep_create/_set_active/_destroy on Linux and FreeBSD
(libobs/util/platform-nix-inhibit.c) move to
obs-util::platform::unix::nix_inhibit, with the C ABI shim in
ffi/platform_nix_inhibit.rs. libobs-with-rust links the shim; the C file
moves to its own ported group, libobs.ported-nix-inhibit, listed in
LIBOBS_LINUX_PORTED_SOURCES.

The portal and session D-Bus inhibitors (platform-nix-portal.c,
platform-nix-dbus.c) stay C and are called as before, so the port does
not lose them the way the first platform-nix port did (obs-rust#453): the portal
first, the session inhibitors only without it, nothing in a Flatpak
without the portal, otherwise the xdg-screensaver reset thread.

GIO_FOUND reaches Rust as the libobs-rust feature gio. os-linux.cmake
and os-freebsd.cmake turn off the default features and set gio from
find_package(Gio); a plain Cargo build has it on by default.

The oracle compiles the original file twice, with and without GIO_FOUND,
and routes what it calls outside itself (the inhibitors, the Flatpak
check, posix_spawnp, blog and the reset thread's wait) to counted C
stand-ins. The Rust core's test host calls the same stand-ins, so the
parity test compares returns and calls line for line over random
sequences, and the shim against the oracle of the build's GIO setting.

Intentional differences: the inhibitor is a Box and the reason a CString;
a thread that cannot start returns false (C checks pthread_create < 0,
never true); the reset retries its wait only on EINTR (C retries any
waitpid error, forever on ECHILD).

obs-nix.c, which shared the PCH, now includes <unistd.h> itself.

Fixes obs-rust#1500
@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 06:29
@zackees
zackees enabled auto-merge October 20, 2026 06:42
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into obs-rust:master with commit fba9e78 Oct 20, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

rust: Port util/platform-nix-inhibit.c to obs-util

2 participants