Skip to content

obs-core: Port the task queue's route - #3405

Merged
rdoupe merged 6 commits into
masterfrom
fix/rust-port-the-task-queue-s-route-440
Nov 1, 2026
Merged

rdoupe merged 6 commits into
masterfrom
fix/rust-port-the-task-queue-s-route-440

Conversation

@rdoupe

@rdoupe rdoupe commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3397

What and why

obs_queue_task() decides where a task goes: to the UI task handler, run in place on its own thread, a wait for itself, or the graphics, audio or destruction queue. obs_in_task_thread() picks the thread it asks about, and obs_set_video_levels() checks the SDR white and HDR peak levels (#2209). These are pure decisions next to the setup rules already ported to rust/obs-core (core_setup_rules), so they are moved out and ported there. Two bugs are fixed in the Rust:

  • An unknown task type. obs_queue_task() with a type outside the four asks obs_in_task_thread(), which hits assert(false) in a debug build and answers false otherwise. With wait, the inner call then queues nothing and the caller waits forever; without it, the task is dropped without a word.
  • obs_set_video_levels() before obs_startup(). It takes &obs->video and reads video->graphics with no core check, so a call with no core dereferences NULL.
  1. Move (libobs: Move the task queue's route out of obs.c): four hidden functions in the new libobs/obs-core-setup-rules-task.c.h, declared in obs-core-setup-rules.h beside the other setup rules: obs_core_setup_rules_task_step() (the route, one read at a time, as obs_core_setup_rules_mix_step() does it), obs_core_setup_rules_task_thread(), obs_core_setup_rules_levels_core() and obs_core_setup_rules_level_ok(). The header copies the four OBS_TASK_* values, checked by _Static_asserts in obs.c.h. The file has its own live group (libobs.core-setup-rules-task, in the core lib-module, with a PCH of math.h and obs-core-setup-rules.h). No Rust.
    obs.c.h keeps every read (the handler pointer, the three thread flags, os_task_queue_inside()), every call (the handler, the task, the wait event and its polling fallback, the recursive obs_queue_task(type, task_wait_callback, &info, false), the locks and pushes, os_task_queue_queue_task()), the assert and every log, at the same points and under the same conditions. A UI task still reads only the handler and never asks obs_in_task_thread().
    Move harness: master's three functions and the moved ones over a logging stand-in, for 11 type values from INT_MIN to INT_MAX, both waits and 64 worlds (each thread flag, the handler, the wait event), and 450 level pairs with and without graphics, built with gcc and clang at -O0 and -O2, plain and with ASan/UBSan: identical logs in all eight. Eight planted mutants (unknown types refused, the graphics and audio pushes swapped, the thread answer inverted, level >= 0, no isfinite(), the UI thread lost, a UI call without a handler, a wait dropped) are all caught. The 22 existing tests that reach these functions pass unchanged on all three libraries.
  2. Characterization (test: Characterize the task queue's route): the new test_core_setup_rules_task_steps compiles the moved file with ASan and UBSan: every step for the four types and for -1, 4 and INT_MAX with each value, the whole walk with the steps each type asks, the thread choice, the core check and the levels (0, -0, the smallest denormal, FLT_MAX, both infinities, NaN). It pins the two bugs as characterized, not endorsed, and never runs a task or a wait. No existing C test changes.
  3. RED (obs-core: Test the task queue's route port): todo!() cores and their Tier 1 tests.
  4. GREEN (obs-core: Port the task queue's route): the cores and their shims. The file moves into the existing ported group libobs.ported-core-setup-rules, whose PCH gains math.h for isfinite(); its live group, PCH and core lib-module entry go. The hidden shims are in rust-hidden.d/obs-core-core_setup_rules.txt.
  5. Tier 3 (obs-core: Compare the task queue's route with C).
  6. Docs (docs: Record the task queue's route port): rust/README.md, the crate description, lib.rs, naming.md and a core_setup_rules_task/task_step bench in its own benches/port_comparison/core_setup_rules_task.rs.

Intentional differences from C (in the parity test header and rust/README.md):

  • the type step routes a type outside the four to UNKNOWN, whatever the answers, and obs_queue_task() logs obs_queue_task: unknown task type N, the task was not queued and returns: no obs_in_task_thread() call, no wait, no task run. Debug builds used to abort here on assert(false) in obs_in_task_thread(); they now log instead, from obs_queue_task() only. That is not a lost check: the error is logged in every build. A direct obs_in_task_thread() call with an unknown type still asserts and returns false, as before;
  • obs_set_video_levels() with no core logs obs_set_video_levels: no core, ignoring the levels and returns before it touches obs->video. With a core but no graphics, the levels are still stored as before.

libobs-c-reference keeps the C behaviour.

Exported symbols: the C, hybrid and libobs-c-reference builds export the same 1767 symbols at the Move and at GREEN, the four new functions local (t) in each.

The moved code is Lain's (the task queue, 2019) and jpark37's (the video levels, 2022); both are credited on the Move commit. Upstream OBS has the same code; there is no upstream issue, and the bugs were found while planning this port.

RED → GREEN

  • RED commit: 60dff5d (obs-core: Test the task queue's route port). OBS_PARITY_CASES=reduced soldr cargo nextest run -p obs-data --test obs-tests-core -E 'test(/obs_core::core_setup_rules::/)': 40 pass, 8 fail on "not yet implemented" (the five mirrors of the C cases that call the new functions, unknown_type_never_asks_the_thread, task_step_any, level_ok_any). --test-diff on RED fails only those 8; the lint lane passed.

  • GREEN: the 48 core_setup_rules Tier 1 tests and 26 parity tests pass (OBS_PARITY_CASES=reduced and full). Tier 2: test_core_setup_rules_task_steps (6) and the 22 tests that reach these functions (test_task, test_core_setup_rules_steps, test_pthread_init_failures, test_video_levels_range, test_video_output_formats, test_source_profiler, test_audio_controls and the rest) pass on the C build (23), the hybrid libobs (24, with the _rust variant) and libobs-c-reference (24), none skipped. Tier 3 mutation check: dropping a type, swapping two pushes, inverting the thread answer, level >= 0, dropping is_finite(), losing the UI thread, a UI call without a handler, a wait dropped, levels_core always true and a route that moves each fail 1 to 3 of the 26 parity tests. Local gates: --test-diff 6060 passed, 0 failed, 6 skipped; --lane lint ok; --lane cross ok; soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets --locked -- -D warnings clean; soldr cargo test -p obs-c-oracle --locked --bench port_comparison passes. The Docker Tier 2 lane and the stamp were not run on this host, so PR CI runs the full suite. Runs were on 921e9d4 (Tier 2, the export comparison, the gates, Windows clippy and the bench check; the headless real run on the earlier GREEN, which the later rebases did not change). Both libobs unity groups the PR touches also compile with Clang and -Werror under the Linux Clang flags.

  • Real app (UI, protocol output or logging changes): a small program through the real libobs, headless (private Xvfb and D-Bus, fresh HOME):

    Call libobs-c-reference hybrid libobs
    obs_set_video_levels(300, 1000) before obs_startup() dies with SIGSEGV logs obs_set_video_levels: no core, ignoring the levels, returns
    obs_queue_task(7, task, NULL, true) after obs_startup() never returns (killed after 10 s) logs obs_queue_task: unknown task type 7, the task was not queued, returns; the task does not run
    obs_queue_task(7, task, NULL, false) returns without a word; the task does not run logs the same error, returns

Test cost: new tests: longest 0.032 s at the full level, peak RSS 16.7 MB over 25 runs of the four new proptests, no own threads, no allocation in the cores.

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 (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (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 (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

rdoupe and others added 6 commits November 1, 2026 05:42
Behaviour-neutral extraction before the Rust port. The route of
obs_queue_task(), the thread choice of obs_in_task_thread() and the
level checks of obs_set_video_levels() move, as written, into four
functions in the new obs-core-setup-rules-task.c.h, declared without
EXPORT in obs-core-setup-rules.h beside the other setup rules:
- obs_core_setup_rules_task_step(): the route, one read at a time, as
  obs_core_setup_rules_mix_step() does it. A UI task asks only whether
  there is a handler; any other asks obs_in_task_thread(), then wait,
  then is pushed to its queue or dropped;
- obs_core_setup_rules_task_thread(): the thread obs_in_task_thread()
  asks about, or -1, which still reaches its assert;
- obs_core_setup_rules_levels_core(): true with or without a core, as
  today;
- obs_core_setup_rules_level_ok(): isfinite(level) && level > 0.f.

obs.c keeps every read (the handler, the three thread flags,
os_task_queue_inside()), every call (the handler, the task, the wait
event and its fallback, the recursive call, the locks and pushes,
os_task_queue_queue_task()), the assert and every log, at the same
points. The two log calls behind levels_core() and the UNKNOWN route
are not reached yet. The header copies the four OBS_TASK_* values,
checked by _Static_asserts in obs.c.

The new file has its own live group, libobs.core-setup-rules-task, in
the core lib-module, with a precompiled header of math.h and
obs-core-setup-rules.h only. No Rust: the hybrid libobs compiles the C
too. The C, hybrid and C reference builds export the same 1767
symbols, the four new functions local (t) in each.

Move harness (scratch, not committed): origin/master's three functions
and the moved ones over a logging stand-in, for 11 type values from
INT_MIN to INT_MAX, both waits and 64 worlds (each thread flag, the
handler, the wait event), and 450 level pairs with and without
graphics, in 8 builds (GCC and Clang, -O0 and -O2, with and without
ASan and UBSan without recovery): identical logs, 11797 lines. Eight
planted mutants (unknown types refused, graphics and audio pushes
swapped, the thread answer inverted, level >= 0, no isfinite(), the UI
thread lost, a UI call without a handler, a wait dropped) are all
caught.

The 22 existing tests that reach these functions (test_task,
test_core_setup_rules_steps, test_pthread_init_failures,
test_video_levels_range, test_video_output_formats and the rest) pass
on the C, hybrid and C reference builds, none skipped.

Refs #3397.

Co-authored-by: Lain <[email protected]>
Co-authored-by: jpark37 <[email protected]>
test_core_setup_rules_task_steps compiles the moved
obs-core-setup-rules-task.c.h into itself, with ASan and UBSan, and
pins its four functions:
- obs_core_setup_rules_task_step(): every step on its own for the four
  task types and for -1, 4 and INT_MAX with each value, a route coming
  back unchanged, and the whole walk for each type over every handler,
  thread and wait answer, with the steps it asks: a UI task reads only
  the handler, any other the thread and then the wait;
- obs_core_setup_rules_task_thread(): each type, and -1 for the rest;
- obs_core_setup_rules_levels_core(): true with or without a core;
- obs_core_setup_rules_level_ok(): 0, -0, the smallest denormal,
  FLT_MIN, 1, 300, 1000, FLT_MAX, negative values, both infinities and
  both NaNs.

Two cases are characterized, not endorsed: an unknown type goes on to
obs_in_task_thread(), which asserts, and then, asked to wait, waits for
a task it never queues; and obs_set_video_levels() goes on with no core.
The test pins the route only and never runs a task or a wait.

It is a new test; the existing ones are unchanged. 6 of 6 pass on the C
and Rust build trees.

Refs #3397.
RED for #3397. core_setup_rules gains TASK_UI, TASK_GRAPHICS,
TASK_AUDIO and TASK_DESTROY, the Task steps and routes, and task_step(),
task_thread(), levels_core() and level_ok(), todo!() stubs, with their
Tier 1 tests:
- six tests mirror test_core_setup_rules_task_steps one to one: the
  constants, each step, the walk of every type with the steps it asks,
  the thread, the core check and the levels;
- but for the C test's two cases marked characterized, not endorsed,
  which the port fixes: an unknown task type's first step is
  Task::Unknown, whatever the answers, so obs_queue_task() never asks
  obs_in_task_thread() or waits for it; and levels_core(false) is
  false, so obs_set_video_levels() stops before it reads obs->video
  (unknown_type_never_asks_the_thread, and the each_step, routes and
  levels_core mirrors);
- proptests: task_step_any over every step, any type and both values,
  and level_ok_any over every f32 bit pattern against
  is_finite() && > 0.

OBS_PARITY_CASES=reduced soldr cargo nextest run -p obs-data --test
obs-tests-core -E 'test(/obs_core::core_setup_rules::/)': 40 pass, 8
fail on "not yet implemented".

Refs #3397.
GREEN for #3397. Ports libobs/obs-core-setup-rules-task.c.h to
core_setup_rules::task_step, task_thread, levels_core and level_ok in
rust/obs-core. obs.c stays C around them: the handler read, the thread
flags, os_task_queue_inside(), the handler call, the task, the wait
event and its fallback, the recursive call, the locks and pushes,
os_task_queue_queue_task(), the assert and every log.

Two intentional differences:
- the type step routes a type outside the four to Task::Unknown,
  whatever the answers, and obs_queue_task() logs "obs_queue_task:
  unknown task type N, the task was not queued" and returns. It no
  longer asks obs_in_task_thread(), which asserts, and no longer waits
  forever for a task it never queued, or drops it silently. A direct
  obs_in_task_thread() call with an unknown type still asserts and
  returns false;
- levels_core(false) is false, so obs_set_video_levels() with no core
  warns "obs_set_video_levels: no core, ignoring the levels" and
  returns before it reads obs->video. With a core but no graphics the
  levels are stored as before.
Debug builds no longer abort on an unknown type in obs_queue_task():
the log replaces the assert there, not the check.

C side: the file leaves its live group for the existing ported group,
libobs.ported-core-setup-rules, whose precompiled header gains math.h
for isfinite(); libobs.core-setup-rules-task, its PCH and its core
lib-module entry go. The functions are not EXPORT, so the shims are
hidden: rust-hidden.d's obs-core-core_setup_rules.txt fragment.

Through the real libobs, headless, with no core and after
obs_startup():
- obs_set_video_levels(300, 1000) with no core: libobs-c-reference
  dies with SIGSEGV; the hybrid libobs logs the warning and returns;
- obs_queue_task(7, task, NULL, true): libobs-c-reference never
  returns (killed after 10 s); the hybrid logs the error and returns,
  and the task does not run;
- obs_queue_task(7, task, NULL, false): libobs-c-reference returns
  without a word; the hybrid logs the error.

The 48 core_setup_rules Tier 1 tests and the 19 parity tests pass.
test_core_setup_rules_task_steps and the 22 tests that reach these
functions pass on the C, hybrid and C reference builds, none skipped.
The three libraries export the same 1767 symbols, the four new ones
local in each. docs/features.md extends the no-core line and adds the
unknown task type.

Refs #3397.
Tier 3 for the task queue's route and the video level checks. The
oracle compiles obs-core-setup-rules-task.c.h beside the other setup
rules, with its four entry points renamed, and exports the task type
copies, OBS_TASK_*, the enum's values and its size. The parity tests
compare:
- task_step for every step the enum names and past it, with the type
  values -2 to 5 and the edges of i32, and both values;
- every walk of obs_queue_task() over the handler, thread and wait
  answers, with the steps it asks;
- task_thread for the same types, and levels_core(true);
- level_ok over both zeros, the denormal and normal edges, 1, 300,
  1000, FLT_MAX, both infinities and quiet and signalling NaNs of both
  signs, exactly;
- proptests over any step, type and value, and every f32 bit pattern.

The intentional differences (#3397) are listed in the file header: an
unknown type at the type step, and levels_core(false). Those two skip
the oracle and are checked against the fixed contract; every later
step and every other input are compared.

Mutation check: dropping a type, swapping two pushes, inverting the
thread answer, level >= 0, dropping is_finite(), losing the UI thread,
a UI call without a handler, a wait dropped, levels_core always true
and a route that moves each fail 1 to 3 of the 26 parity tests.

Refs #3397.
rust/README.md's core_setup_rules row names
libobs/obs-core-setup-rules-task.c.h, the four new functions, the
OBS_TASK_* copies, what obs.c keeps around them, the new
characterization test and the two intentional differences (#3397). The
obs-core crate row, its Cargo.toml description, lib.rs and the
naming.md row list the new file. The port_comparison bench gains
core_setup_rules_task/task_step, in its own core_setup_rules_task.rs
(#1997): a walk of four tasks on different routes.

Fixes #3397.
@rdoupe
rdoupe force-pushed the fix/rust-port-the-task-queue-s-route-440 branch from 11994da to 921e9d4 Compare November 1, 2026 11:01
@rdoupe
rdoupe marked this pull request as ready for review November 1, 2026 11:28
@rdoupe
rdoupe requested a review from zackees as a code owner November 1, 2026 11:28
@rdoupe
rdoupe added this pull request to the merge queue Nov 1, 2026
Merged via the queue into master with commit b106053 Nov 1, 2026
41 checks passed
@rdoupe
rdoupe deleted the fix/rust-port-the-task-queue-s-route-440 branch November 1, 2026 11:45
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 the task queue's route

1 participant