Skip to content

callback: Fix signal callback removal across nesting, recursion and registration (split from #1464) - #1556

Merged
zackees merged 2 commits into
masterfrom
fix/signal-callbacks
Oct 20, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/signal-callbacks

Conversation

@zackees

@zackees zackees commented Oct 20, 2026

Copy link
Copy Markdown
Member

Split from #1464 (commits 00368fb, 183d3fd, 8bc9c00, 4554fee, 179e9a4, squashed into one GREEN commit). Part of breaking that 20-fix PR into small reviewable PRs. All changes are in libobs/callback/signal.c.h; signal.c has no Rust port yet, so there is no Rust side to change (no parity test).

What and why

  1. Deferred keep_ref removal leaked the handler (00368fb). When a signal_handler_connect_ref callback removed itself during delivery, the ref was dropped with os_atomic_set_long(refs - n), never checking for zero; if the creator had already called signal_handler_destroy, the handler was leaked. Now each ref is dropped with os_atomic_dec_long and the handler is destroyed on zero.
  2. Global callbacks matched by da_find (memcmp of the whole entry) (183d3fd). The compare included padding and the signaling counter, so a global callback could not disconnect itself during delivery (signaling == 1), and connect/disconnect dedup depended on uninitialized padding. Now matched by callback and data.
  3. Nested delivery reset the current callback (8bc9c00). signal_handler_signal set the thread-local current callback to NULL after each call, so a callback that emitted another signal and then called signal_handler_remove_current() removed nothing. Now the previous signal/global current callback is saved and restored.
  4. Recursive delivery erased entries mid-iteration (4554fee). signalling was a bool; an inner emission of the same signal erased removed callbacks while the outer loop was still walking the array, so the outer pass skipped later subscribers. Now a depth counter; removal happens only when the outermost pass finishes.
  5. Current callback was a pointer into the array (179e9a4). A callback that connected more callbacks (reallocating the DARRAY) and then called signal_handler_remove_current() wrote into freed memory (ASan: heap-use-after-free in signal_handler_remove_current) and did not remove itself. Now tracked as (signal, index), and the callback entry is copied before the call.

This deliberately changes characterization tests in test/cmocka/test_signal.c, following the precedent of #1508 (the C-test expectation change was approved by the maintainer as in #1508). Assertions are stricter, comments say the old characterization is superseded and why. No assertion was removed or weakened.

Test Old expectation New expectation
nested_signal_test (fixture nested_then_remove) "characterized, not endorsed": remove_current after a nested signal removes nothing; ids 1,2,1,2 callback removes itself: ids 1,2 then nothing; plus inner still delivers
global_remove_test "characterized, not endorsed": self-disconnecting global stays connected; ids 7,8,8 removed: ids 7,8; plus disconnect outside a signal is found
global_test — added: connecting the same global callback+data twice delivers once
file header "Not pinned, on purpose" (da_find padding) not pinned now pinned; header says it is superseded
new keep_ref_remove_current_last_ref_test (#1) — handler freed (bnum_allocs back to baseline) when the last ref is a self-removing keep_ref callback
new recursive_remove_test (#4) — outer r, outer a, inner a, inner c, outer c (5 calls); r gone afterwards
new connect_then_remove_current_test (#5) — callback connects 64 more, removes itself; not called on the next signal

RED → GREEN

Built test_signal.c locally against signal.c.h/calldata.c.h/decl.c.h and the util sources with a minimal cmocka shim (with and without ASan); the full Docker Tier 2 is left to PR CI per shared-machine rules.

  • RED b7205f8 on master's signal.c.h: 5 failures — nested_signal_test (count 4 != 2), keep_ref_remove_current_last_ref_test (allocs 11 != 7), recursive_remove_test (4 != 5), connect_then_remove_current_test (2 != 1; ASan: heap-use-after-free), global_remove_test (3 != 2).
  • GREEN e6fb361: 16/16 pass, clean under ASan + LeakSanitizer. ./lint ok.

Checklist

@zackees
zackees enabled auto-merge October 20, 2026 20:19
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 20, 2026
…tion and self-disconnect

Supersedes the characterizations in nested_signal_test and
global_remove_test and adds cases for deferred removal releasing the last
handler reference, removal during recursive delivery, and removal after
connects that move the callback array. Split from #1464.
…registration

Squash of #1464's 00368fb, 183d3fd, 8bc9c00, 4554fee, 179e9a4:
- release the handler when a deferred keep_ref removal drops the last ref
- find global callbacks by callback and data, not a whole-struct memcmp
- save and restore the thread-local current callback around delivery
- count nested deliveries and erase removed callbacks only at depth 0
- track the current callback by signal and index, not a pointer into the
  array that a connect during delivery can reallocate
@zackees
zackees force-pushed the fix/signal-callbacks branch from e6fb361 to 52ac191 Compare October 20, 2026 21:12
@zackees
zackees enabled auto-merge October 20, 2026 21:12
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into master with commit 5310fed Oct 20, 2026
23 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 21, 2026
…#1639)

* test: expect global callbacks tracked by index in signal (Refs obs-rust#1441)

signal_handler_signal keeps a pointer into the global callback array
while a global callback runs, and the running callback uses it for
signal_handler_remove_current and the signaling count. Three cases
move that entry while the pointer is held:

- The callback connects enough global callbacks to reallocate the
  array, then removes itself (heap use-after-free, GHSA-rjwf-j53r-47qr).
- A nested signal erases an earlier removed entry, then the outer
  callback removes itself.
- The callback disconnects an earlier global callback, then removes
  itself.

In the last two, the removal marks the next callback instead, and the
pass skips that callback. The signal callbacks handle the same cases
since obs-rust#1556.

Build test_signal with ASan. Its allocator moves every reallocated
block, so the first case does not depend on the platform allocator.
libobs is not instrumented, so ASan does not report the write itself;
the test sees the wrong result. Without ASan, glibc moves the array
here too (5 of 5 runs fail).

Refs obs-rust#1441

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* libobs: Track the running global callback by index (Refs obs-rust#1441)

signal_handler_signal kept a pointer into the global callback array
while a global callback ran. A connect from the callback could
reallocate the array, and a nested signal or a disconnect could erase
an earlier entry and move the running one. signal_handler_remove_current
then wrote to freed memory or marked the next callback, and the
signaling count went to the wrong entry.

Follow the signal callbacks (obs-rust#1556):
- The running global callback is a (handler, index) context.
- The handler counts the running global passes. Removed entries are
  erased only when the outermost pass ends.
- signal_handler_disconnect_global only marks the entry while a global
  pass runs.

The per-entry signaling count is no longer needed and is removed.

Refs obs-rust#1441

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 21, 2026
* test: add failing obs-callback signal tier 1 (Refs obs-rust#1441)

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]>

* obs-callback: signal handler core in Rust (Refs obs-rust#1441)

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]>

* obs-callback: swap signal_handler to Rust (Refs obs-rust#1441)

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]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@zackees
zackees deleted the fix/signal-callbacks branch October 26, 2026 15:44
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.

1 participant