HLE: preserve ERROR_TLW_WITH_WSCHILD and close relaunch duplicate handle #3317

Open
mgunes1311-lab wants to merge 2 commits from upstream-pr/3317 into main
mgunes1311-lab commented 2026-11-26 17:20:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mgunes1311-lab on 2026-11-26T17:20:01Z.


Coordination

Claimed in https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7197202703 (HLE: preserve Win32 ERROR_TLW_WITH_WSCHILD on null-parent WS_CHILD windows and close duplicated handle in lifecycle::relaunch). Checked STATUS.md, CLAIMS.md, and forge/main — zero overlapping PRs or diff in crates/victory-hle/src/modules/lifecycle.rs.

Problem

Two Win32 lifecycle edge cases in crates/victory-hle/src/modules/lifecycle.rs:

  1. CreateWindowExW and CreateWindowExA checked let top = parent.is_null(); without checking WS_CHILD. When a caller passed style = WS_CHILD with parent = NULL, substituting HWND_MESSAGE caused Win32 to create a message-only child window and attempt a stand-in window with WS_CHILD (parent = NULL, logging a spurious warning) instead of failing with NULL and ERROR_TLW_WITH_WSCHILD (1406).
  2. relaunch duplicated the current process handle with bInheritHandle = TRUE (DuplicateHandle) for VICTORY_RELAUNCHED_FROM, but never called CloseHandle(me) in the parent after command.spawn() returned (Ok or Err), leaking an inheritable self-process handle on failed or repeated relaunch attempts.

Fix

  • Gate message-only / stand-in substitution on let top = parent.is_null() && style & wm::WS_CHILD == 0; in both CreateWindowExW and CreateWindowExA.
  • Extract relaunch_exe and call CloseHandle(me) immediately after command.spawn() returns, once CreateProcessW has inherited the handle into the child (or failed).

Verification

  • Added null_parent_ws_child_windows_fail_with_win32_error_while_valid_children_succeed testing both CreateWindowExW and CreateWindowExA with WS_CHILD + null parent (NULL + ERROR_TLW_WITH_WSCHILD) and valid parent (GetParent == parent, hooked procedure, no stand-in).
  • Added relaunch_closes_its_duplicated_process_handle verifying process handle count stability across 257 failed spawns and a successful ComSpec /C exit 0 child launch.
  • Ran cargo test -p victory-hle --locked modules::lifecycle::tests:: (14/14 passed), cargo clippy --workspace --all-targets --all-features --locked -- -D warnings, and cargo xtask policy on Windows 11 (x86_64-pc-windows-msvc).

AI-assisted contribution (Google Antigravity); all code and Win32 unit tests were locally executed and verified.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5514413778 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3317). Originally posted by [mgunes1311-lab](https://github.com/mgunes1311-lab) on 2026-11-26T17:20:01Z. --- ## Coordination Claimed in https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7197202703 (`HLE: preserve Win32 ERROR_TLW_WITH_WSCHILD on null-parent WS_CHILD windows and close duplicated handle in lifecycle::relaunch`). Checked `STATUS.md`, `CLAIMS.md`, and `forge/main` — zero overlapping PRs or diff in `crates/victory-hle/src/modules/lifecycle.rs`. ## Problem Two Win32 lifecycle edge cases in `crates/victory-hle/src/modules/lifecycle.rs`: 1. `CreateWindowExW` and `CreateWindowExA` checked `let top = parent.is_null();` without checking `WS_CHILD`. When a caller passed `style = WS_CHILD` with `parent = NULL`, substituting `HWND_MESSAGE` caused Win32 to create a message-only child window and attempt a stand-in window with `WS_CHILD` (`parent = NULL`, logging a spurious warning) instead of failing with `NULL` and `ERROR_TLW_WITH_WSCHILD` (`1406`). 2. `relaunch` duplicated the current process handle with `bInheritHandle = TRUE` (`DuplicateHandle`) for `VICTORY_RELAUNCHED_FROM`, but never called `CloseHandle(me)` in the parent after `command.spawn()` returned (`Ok` or `Err`), leaking an inheritable self-process handle on failed or repeated relaunch attempts. ## Fix - Gate message-only / stand-in substitution on `let top = parent.is_null() && style & wm::WS_CHILD == 0;` in both `CreateWindowExW` and `CreateWindowExA`. - Extract `relaunch_exe` and call `CloseHandle(me)` immediately after `command.spawn()` returns, once `CreateProcessW` has inherited the handle into the child (or failed). ## Verification - Added `null_parent_ws_child_windows_fail_with_win32_error_while_valid_children_succeed` testing both `CreateWindowExW` and `CreateWindowExA` with `WS_CHILD` + null parent (`NULL` + `ERROR_TLW_WITH_WSCHILD`) and valid parent (`GetParent == parent`, hooked procedure, no stand-in). - Added `relaunch_closes_its_duplicated_process_handle` verifying process handle count stability across 257 failed spawns and a successful `ComSpec /C exit 0` child launch. - Ran `cargo test -p victory-hle --locked modules::lifecycle::tests::` (14/14 passed), `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`, and `cargo xtask policy` on Windows 11 (`x86_64-pc-windows-msvc`). AI-assisted contribution (Google Antigravity); all code and Win32 unit tests were locally executed and verified.
Dukestar69 commented 2026-11-26 17:27:22 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64:

  • Checking parent.is_null() && style & wm::WS_CHILD == 0 preserves Win32 ERROR_TLW_WITH_WSCHILD instead of turning invalid WS_CHILD windows into message-only windows, and closing the duplicated process handle in relaunch_exe after spawn() prevents handle leaks.
  • cargo test -p victory-hle --lib modules::lifecycle:: -- --test-threads=1: 14 passed, 0 failed (including null_parent_ws_child_windows_fail_with_win32_error_while_valid_children_succeed and relaunch_closes_its_duplicated_process_handle).
  • 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:6078357640 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3317#pullrequestreview-6078357640). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T17:27:22Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Checking `parent.is_null() && style & wm::WS_CHILD == 0` preserves Win32 `ERROR_TLW_WITH_WSCHILD` instead of turning invalid `WS_CHILD` windows into message-only windows, and closing the duplicated process handle in `relaunch_exe` after `spawn()` prevents handle leaks. - `cargo test -p victory-hle --lib modules::lifecycle:: -- --test-threads=1`: 14 passed, 0 failed (including `null_parent_ws_child_windows_fail_with_win32_error_while_valid_children_succeed` and `relaunch_closes_its_duplicated_process_handle`). - `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:44:12 +00:00 (Migrated from github.com)

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


Windows 11, 63bcca4 + this PR:

  • cargo test -p victory-hle lifecycle::: 14 passed / 0 failed.
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.

The two new tests are in that 14: null_parent_ws_child_windows_fail_with_win32_error_while_valid_children_succeed and relaunch_closes_its_duplicated_process_handle, so the Win32 error path and the duplicated-handle close are exercised on the real OS. Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204940654 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3317#issuecomment-7204940654). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T22:44:12Z. --- Windows 11, `63bcca4` + this PR: - `cargo test -p victory-hle lifecycle::`: 14 passed / 0 failed. - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. The two new tests are in that 14: `null_parent_ws_child_windows_fail_with_win32_error_while_valid_children_succeed` and `relaunch_closes_its_duplicated_process_handle`, so the Win32 error path and the duplicated-handle close are exercised on the real OS. 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/3317:upstream-pr/3317'
git switch 'upstream-pr/3317'

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/3317'
git switch 'upstream-pr/3317'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3317'
git switch 'upstream-pr/3317'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3317'
git switch 'main'
git merge --squash 'upstream-pr/3317'
git switch 'main'
git merge --ff-only 'upstream-pr/3317'
git switch 'main'
git merge 'upstream-pr/3317'
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!3317
No description provided.