Skip to content

obs-callback: Port signal.c to Rust - #1642

Merged
zackees merged 3 commits into
obs-rust:masterfrom
vpacher:rust/1441-signal-port
Oct 21, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
vpacher:rust/1441-signal-port

Conversation

@vpacher

@vpacher vpacher commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Fixes #1441

What and why

Port libobs/callback/signal.c.h to rust/obs-callback and swap it into the hybrid libobs. The ten signal_handler_* exports move. signal_handler_add_array stays inline in signal.h.

The port follows the C as fixed by #1556 and #1639:

  • The running callback is a thread-local index: (signal, index) for signal callbacks, (global list, index) for global callbacks. A connect only pushes.
  • Passes nest by count, per signal and for the global list. Removed callbacks are erased only when the outermost pass ends. A disconnect while a pass runs only marks the callback. So the index stays valid while the callback runs.
  • Each callback call saves and restores both running callbacks.
  • Global callbacks are found by callback and data.
  • A keep_ref reference given back after a pass destroys the handler when it was the last one.

The C handle is one bmalloc block that holds an Arc<SignalHandler>, as in the audio-controls port. bnum_allocs therefore sees the handler until its last reference goes; keep_ref_remove_current_last_ref_test reads it. signal_handler_signal holds its own Arc clone while callbacks run, so a callback that destroys the handler does not free it under the pass.

Intentional differences, all where C dereferences null: a null handler in signal_handler_add returns false, a null signal name is not found, and a null callback is stored but not called. They are listed in the shim header and in rust/README.md.

The audio-controls oracle defined signal_handler_connect and signal_handler_disconnect as stubs. A test binary that also links obs-callback, such as obs-tests-core, would define them twice, and GNU ld stops. The stubs are renamed to oracle_audio_controls_*, as the oracle does for its own exports.

While I prepared this port, the Rust core showed the same behavior as the old C for global callbacks in a nested pass and for a global disconnect during the pass. Tier 3 was green because both sides had the bug. #1639 fixes the C, and this port follows it.

Tiers

  • Tier 1 (tests/callback/signal.rs): mirrors test/cmocka/test_signal.c with the callback: Fix signal callback removal across nesting, recursion and registration (split from #1464) #1556 and callback: Track the running global signal callback by index #1639 cases, plus edge cases: global connect and disconnect matched by callback and data, a global signal in a nested pass, and a disconnect from another thread that waits for the running pass.
  • Tier 2: test/cmocka/test_signal.c against the hybrid libobs and against libobs-c-reference.
  • Tier 3 (tests/callback/signal_parity.rs): random scripts against the original signal.c.h on the decl oracle. Signal and global callbacks connect, disconnect, remove themselves, connect then remove, and start nested signals. The test compares every callback call in order and every logged line. Only a destroy from a callback is excluded (C frees the handler under the running pass).

RED → GREEN

Base: master 01561bf (includes #1639).

Checklist

vpacher and others added 2 commits October 21, 2026 20:55
Mirror test/cmocka/test_signal.c, as fixed by obs-rust#1556 and obs-rust#1639, against
todo stubs in obs-callback. Add: global connect and disconnect matched by
callback and data, a connect of many callbacks then remove_current
from the running callback, a global signal in a nested pass, and a
disconnect from another thread that waits for the running pass.

From obs-rust#1639: a global callback that connects more global callbacks
then removes itself, a nested pass that must not erase an earlier
removed global callback, and a global disconnect during the pass.

Refs obs-rust#1441

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The safe core follows signal.c.h as fixed by obs-rust#1556 and obs-rust#1639: per-signal and
global re-entrant locks held while callbacks run, deferred removal
while signalling, keep_ref references, and the thread-local running
callback. The running callback is a (signal, index) pair instead of a
pointer into the callback array, so a connect from the callback cannot
leave it dangling.

As in the fixed C:
- Passes of one signal nest by count, and so do the global passes.
  Removed callbacks are erased only when the outermost pass ends. A
  disconnect while a pass runs only marks the callback. The running
  global callback is an index, as the running signal callback is.
- Each callback call saves and restores both running callbacks. A
  signal callback clears the global one, a global callback clears the
  signal one. remove_current marks the signal one first.
- Global callbacks are found by callback and data only.
- A keep_ref reference given back after a pass destroys the handler
  when it was the last one. signal() reports it to the owner.

Refs obs-rust#1441

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The ten signal_handler_* exports move into obs-callback.
signal_handler_add_array stays inline in signal.h. The handle is one
bmalloc block that holds an Arc, as in the audio-controls port, so
bnum_allocs sees the handler until its last reference goes
(keep_ref_remove_current_last_ref_test reads it). Both are freed when
the C reference count reaches zero. signal_handler_signal keeps its
own clone while callbacks run and frees the handler after the pass
when the pass gave back the last reference. The log texts are the C
ones.

callback/signal.c moves into the ported group libobs.ported-callback,
on its header.

The audio-controls oracle defined signal_handler_connect and
signal_handler_disconnect as stubs. A test binary that also links
obs-callback, such as obs-tests-core, would define them twice (GNU ld
stops). Rename the stubs to oracle_audio_controls_* with #define, as
the oracle already does for its own exports.

Tier 3 runs the original signal.c.h, as fixed by obs-rust#1639, on the decl
oracle (oracle/signal.c). Random scripts of add, connect, connect_ref,
disconnect, connect_global, disconnect_global and signal run with
re-entrant actions. Signal callbacks remove the current one, disconnect
or connect another, connect then remove, or start a nested signal.
Global callbacks remove the current one, disconnect themselves,
connect or disconnect another global callback, connect then remove, or
start a nested signal. The test compares every callback call in order
and every logged line. Excluded: a destroy from a callback (C frees the
handler under the running pass). Against the core without the obs-rust#1639
rule, Tier 3 finds a failing script.

Checks on Linux x86_64 (Debian 13), CMake with OBS_BUILD_C_REFERENCE:
test_signal.c passes 19/19 against the hybrid libobs and against
libobs-c-reference (loader checked). ctest against the hybrid passes
344/344 under xvfb-run, without the xcomposite/xshm tests that do not
build on Debian's cmocka. obs-tests-core passes 422/422 with GNU ld.

Refs obs-rust#1441

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 callback/signal.c to obs-callback

2 participants