HLE: wait for in-flight lifecycle callbacks on unregister #3270

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3272 into main
fuzeofficlal commented 2026-11-26 15:21:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T15:21:08Z.


Summary

  • Synchronize UnregisterAppStateChangeNotification and UnregisterAppConstrainedChangeNotification with end_title / constrain_then_suspend so unregistering waits until any in-flight invocation of that registration on another thread has returned.
  • Stop cloning CONSTRAINED and ROUTINES upfront in end_title; dispatch each phase from live registrations and re-check membership before every callback so routines unregistered mid-dispatch (including suspend routines unregistered during the constrained phase) are skipped.
  • Track active (id, ThreadId) invocations with an unwind-safe RAII guard so same-thread self-unregistration never deadlocks while another thread concurrently unregistering the same registration still waits until the callback returns, and unregistering unrelated or zero registrations does not block behind other active callbacks.
  • Write *registration before publishing the entry in Notifications::register.

Root Cause

Under Windows Process State Manager (api-ms-win-core-psm-appnotify-*), returning from UnregisterAppStateChangeNotification or UnregisterAppConstrainedChangeNotification guarantees that the callback is neither executing on another thread nor will be invoked again, allowing the title to destroy the callback's context immediately afterward.

Previously, end_title cloned both callback vectors before spawning the suspend thread, constrain_then_suspend iterated those stale snapshots without re-checking registration state, and both Unregister* exports only removed the entry from the Vec and returned immediately. This left two use-after-free paths during shutdown/teardown:

  1. Unregistering an in-flight callback returned before the background thread finished accessing context.
  2. Unregistering a not-yet-started sibling constrained callback or suspend callback (either from another thread or from an earlier constrained/suspend callback) did not prevent the stale snapshot from invoking it with freed context.

Verification

  • Reproduced both UAF paths deterministically as RED failures on origin/main (63bcca43) before the fix (returned_early=true, saw_destroyed=true and unregistered sibling constrained callback must not run).
  • Added regression tests covering in-flight wait for both state and constrained notifications, skipping unregistered intra-phase siblings and cross-phase suspend routines, same-thread self-unregistration combined with a second thread's concurrent wait, and non-blocking unregistration of zero/unrelated ids.
  • Verified after the final edit:
    • cargo test -p victory-hle --locked (155 unit + 2 integration tests passed)
    • cargo test -p victory-hle --all-features --locked (passed)
    • 20 consecutive rounds of the new concurrency tests (20/20 passed)
    • cargo clippy --workspace --all-targets --locked -- -D warnings (passed)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (passed)
    • cargo test -p xtask --locked policy::tests::repository_passes_policy (passed)
<!-- greyfinch-sync:Marvisak/VIctory:issue:5512686919 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3272). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T15:21:08Z. --- ## Summary - Synchronize `UnregisterAppStateChangeNotification` and `UnregisterAppConstrainedChangeNotification` with `end_title` / `constrain_then_suspend` so unregistering waits until any in-flight invocation of that registration on another thread has returned. - Stop cloning `CONSTRAINED` and `ROUTINES` upfront in `end_title`; dispatch each phase from live registrations and re-check membership before every callback so routines unregistered mid-dispatch (including suspend routines unregistered during the constrained phase) are skipped. - Track active `(id, ThreadId)` invocations with an unwind-safe RAII guard so same-thread self-unregistration never deadlocks while another thread concurrently unregistering the same registration still waits until the callback returns, and unregistering unrelated or zero registrations does not block behind other active callbacks. - Write `*registration` before publishing the entry in `Notifications::register`. ## Root Cause Under Windows Process State Manager (`api-ms-win-core-psm-appnotify-*`), returning from `UnregisterAppStateChangeNotification` or `UnregisterAppConstrainedChangeNotification` guarantees that the callback is neither executing on another thread nor will be invoked again, allowing the title to destroy the callback's `context` immediately afterward. Previously, `end_title` cloned both callback vectors before spawning the suspend thread, `constrain_then_suspend` iterated those stale snapshots without re-checking registration state, and both `Unregister*` exports only removed the entry from the `Vec` and returned immediately. This left two use-after-free paths during shutdown/teardown: 1. Unregistering an in-flight callback returned before the background thread finished accessing `context`. 2. Unregistering a not-yet-started sibling constrained callback or suspend callback (either from another thread or from an earlier constrained/suspend callback) did not prevent the stale snapshot from invoking it with freed `context`. ## Verification - Reproduced both UAF paths deterministically as RED failures on `origin/main` (`63bcca43`) before the fix (`returned_early=true, saw_destroyed=true` and `unregistered sibling constrained callback must not run`). - Added regression tests covering in-flight wait for both state and constrained notifications, skipping unregistered intra-phase siblings and cross-phase suspend routines, same-thread self-unregistration combined with a second thread's concurrent wait, and non-blocking unregistration of zero/unrelated ids. - Verified after the final edit: - `cargo test -p victory-hle --locked` (155 unit + 2 integration tests passed) - `cargo test -p victory-hle --all-features --locked` (passed) - 20 consecutive rounds of the new concurrency tests (20/20 passed) - `cargo clippy --workspace --all-targets --locked -- -D warnings` (passed) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (passed) - `cargo test -p xtask --locked policy::tests::repository_passes_policy` (passed)
Dukestar69 commented 2026-11-26 15:27:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T15:27:52Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64:

  • Encapsulating lifecycle callbacks in Notifications (Mutex + Condvar + active (id, ThreadId) tracking), writing the registration handle before publishing, waiting for in-flight external callbacks on unregister, and avoiding self-deadlock on reentrant self-unregistration cleanly matches Windows PSM semantics.
  • cargo test -p victory-hle --lib modules::lifecycle:: -- --test-threads=1: 15 passed, 0 failed (including unregister_waits_for_in_flight_lifecycle_callbacks, unregister_skips_unstarted_sibling_and_suspend_callbacks, and self_unregister_does_not_deadlock_while_other_thread_waits_and_unrelated_returns).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6076753927 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3272#pullrequestreview-6076753927). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T15:27:52Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Encapsulating lifecycle callbacks in `Notifications` (`Mutex` + `Condvar` + active `(id, ThreadId)` tracking), writing the registration handle before publishing, waiting for in-flight external callbacks on `unregister`, and avoiding self-deadlock on reentrant self-unregistration cleanly matches Windows PSM semantics. - `cargo test -p victory-hle --lib modules::lifecycle:: -- --test-threads=1`: 15 passed, 0 failed (including `unregister_waits_for_in_flight_lifecycle_callbacks`, `unregister_skips_unstarted_sibling_and_suspend_callbacks`, and `self_unregister_does_not_deadlock_while_other_thread_waits_and_unrelated_returns`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Jackster commented 2026-11-26 22:52:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T22:52:33Z.


Windows 11, 63bcca4 + this PR:

  • cargo test -p victory-hle: lib 155 passed / 0 failed (unpatched 63bcca4 here: 152 passed; +3), plus the integration suite (1 passed).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.

The three new tests are unregister_waits_for_in_flight_lifecycle_callbacks, unregister_skips_unstarted_sibling_and_suspend_callbacks and self_unregister_does_not_deadlock_while_other_thread_waits_and_unrelated_returns; none of them hangs here. Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7205059751 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3272#issuecomment-7205059751). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T22:52:33Z. --- Windows 11, `63bcca4` + this PR: - `cargo test -p victory-hle`: lib **155 passed / 0 failed** (unpatched `63bcca4` here: 152 passed; +3), plus the integration suite (1 passed). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. The three new tests are `unregister_waits_for_in_flight_lifecycle_callbacks`, `unregister_skips_unstarted_sibling_and_suspend_callbacks` and `self_unregister_does_not_deadlock_while_other_thread_waits_and_unrelated_returns`; none of them hangs here. Synthetic only. AI-assisted.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3272:upstream-pr/3272'
git switch 'upstream-pr/3272'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3272'
git switch 'upstream-pr/3272'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3272'
git switch 'upstream-pr/3272'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3272'
git switch 'main'
git merge --squash 'upstream-pr/3272'
git switch 'main'
git merge --ff-only 'upstream-pr/3272'
git switch 'main'
git merge 'upstream-pr/3272'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!3270
No description provided.