Skip to content

startup: hold other threads while a hook steps over its byte - #1408

Merged
pigalot merged 14 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-1281-hooks
Nov 27, 2026
Merged

pigalot merged 14 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-1281-hooks

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1281

What this unblocks: a startup run that traces every call of a hooked function even when two threads enter it at once (files.txt, the offline socket refusal and rewrites built on the hook no longer miss concurrent calls).

Source: Microsoft Win32 debugging API documentation: WaitForDebugEvent / ContinueDebugEvent (https://learn.microsoft.com/windows/win32/api/debugapi/nf-debugapi-waitfordebugevent), SuspendThread and GetThreadContext (https://learn.microsoft.com/windows/win32/api/processthreadsapi/nf-processthreadsapi-suspendthread); the single-step trap flag is the x86 EFLAGS TF bit.

Change

The startup debugger's function hooks are one patched byte per function. To run the original instruction it restores the byte, single-steps the thread that hit it and patches the byte again. A second thread entering the same function in that window ran untraced, so files.txt, the offline socket refusal and any rewrite built on the hook missed calls.

Chosen fix: hold every other thread for the step (SuspendThread plus a GetThreadContext to wait until it has stopped), step the owner alone, patch the byte again, then resume them. Why this and not the alternatives:

  • Hardware breakpoints are per thread but only four, and every new thread has to be armed; the hooks here are more than four.
  • Emulating the original instruction needs a decoder for every hooked prologue.
  • Holding the others for one instruction is bounded: the step is one instruction of an ntdll stub, so a held thread's lock is not needed.

Details:

  • A thread that had already trapped before it was held goes back to the hook address and traps again after the window, so its call is traced once.
  • A thread created during the window is held too; a thread that exited is skipped.
  • Every window has a 2 s deadline. A thread that cannot be held, or a deadline that passes, is counted.
  • Per-thread state of a call in flight (return address and stack pointer) was already keyed by thread id; the docs now say so.
  • Every run ends with hook windows: entries=N windows=W unprotected=U deferred=D timeouts=T protection=on|off in the console, finished.txt and files.txt, also when all are 0.
  • --suspend-others 0 turns the holding off, to measure it or to see the loss.

Bookkeeping is in tools/startup/hook_window.py (no Win32 calls) so it can be checked without Windows.

Verification

Linux, run here:

  • python3 -B tests/startup_hook_check.py: PASS. Five single mutations of hook_window.py (defer never, any thread closes the window, deadline not counted, failed hold not counted, owner held) each make it fail.
  • py_compile of the three Python files, tests/docs_links_check.py, tests/bidi_controls_check.py, git diff --check: clean.
  • The probe builds with a cross compiler and, run alone under Wine, opens 64 of 64.

Windows (Win2025, D3D12 WARP node, MSVC 14.44, Windows Server 2025 build 26100), PR head 88e0aa8 (origin/main merged in, merge commit, no force):

python x2w_pipe.py win2025 1408 --host proxmox --regex '^(startup_launch|startup_hook_window|startup_hook_overlap|startup_debug)' --keep-release
  • Debug and release: 7/7 passed each: startup_launch, startup_launch_cli_check, startup_hook_window, startup_hook_overlap, startup_debug, startup_debug_log, startup_debug_hooks.
  • startup_hook_overlap: 64 of 64 opens traced with the window, three times, unprotected=0; the control with --suspend-others 0 lost opens and reported unprotected above 0.
  • Found and fixed while merging with main: the hit-limit release asked the return book returns.get(addr) (the class has no get; ReturnBook.pending() added with a test), and a duplicate file_log assignment overrode main's trace or log_at (startup_debug_log failed before the fix, passes after).
  • Not covered here: the two-thread --map-drive case needs startup: map a console package drive letter to a real folder #1251, which is not in main.

Evidence and follow-up

  • Outside the pr preset: startup_hook_window, startup_hook_overlap.
  • Interaction with startup: map a console package drive letter to a real folder #1251 (--map-drive): that PR registers startup_map_drive_overlap as WILL_FAIL for this issue. After both are merged, that test passes and WILL_FAIL must be removed. The name pool there already hands each call its own buffer, and its in-flight data is kept per call in returns, so it needs no change for this fix. This PR does not touch those lines; it applies on main without startup: map a console package drive letter to a real folder #1251.
  • cpu: handle immediate INSERTQ in the startup debugger #1229 also edits debug_launch.py (near set_ctx, the event loop and the stop report); the hunks are separate from mine, but whichever merges second may need a small rebase.
  • Not measured: how often a real startup run enters one hooked function from two threads, and the time cost of holding threads on a run with many threads.

Checklist

  • docs/experiments.md and docs/startup-runtime.md updated
  • Title-neutral, original code, no game files or logs
  • Test fails without the change

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

AI-assisted. Cause of "88 of 64" in the unprotected control: with --suspend-others 0 another thread can re-patch the hook byte before the stepping thread has run the original instruction, so that thread traps again at the same address. The entry hook registered the same call twice (same thread, same stack slot), and the return wrote one line per registration. Replaying that bookkeeping with 24 re-trapped calls out of 64 gives 88 lines with the old logic and 64 with the fix.

Changes in the new commit: pending returns now live in ReturnBook (tools/startup/hook_window.py), where a call is identified by thread and stack slot and a repeat registration replaces the first. The control counts lost opens as max(0, 64 - traced) and fails if traced exceeds 64. The overlap probe now uses 4 threads x 16 rounds (still 64 opens) with a spin barrier to make the race more likely.

Linux: startup_hook_window logic, py_compile, docs_links_check, bidi_controls_check pass. Windows not run by me. Node command: ctest --preset dev -R 'startup_hook|startup_launch' (Debug and /O2). PR stays Draft.

@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

AI-assisted. Pushed 3cfa172 for the Windows Server 2025 failure of startup_hook_overlap (windows=132 unprotected=1 timeouts=0).

Cause: a thread that has called ExitThread stays in the debugger's thread list until its exit event is read. OpenThread still succeeds on it, but SuspendThread/GetThreadContext fail with access denied, and hold_thread returned FAILED, so the window counted it as unprotected although it runs no code. The hold is now classified after the failure by a pure function (hook_window.classify_hold): GONE when GetExitCodeThread != STILL_ACTIVE (or the handle is already signaled, for a thread that is terminating and has no exit code yet), FAILED when the thread is alive and could not be suspended. A live unsuspendable thread still counts as unprotected, and the test still requires unprotected == 0.

Ruled out by reading: a thread created after the list is read (CREATE_THREAD held at its event), an already suspended thread (nested count is not an error, only 0xFFFFFFFF is), a reused or closed handle (a fresh handle per hold, threads dropped from the list on exit), the owner itself (excluded from the others).

Linux checks: py_compile, startup_hook_window (new GONE/FAILED cases; the old module has no classifier), docs_links_check, bidi_controls_check, preflight. The Win32 debugger race cannot be run on Linux, so it is not verified here. Windows: ctest --preset dev -R 'startup_hook|startup_launch' (Debug and /O2).

@tedin7
tedin7 marked this pull request as ready for review November 24, 2026 11:02
@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit 3cfa1726:

  • Debug (--preset dev): ctest --preset dev -R '^(startup_launch|startup_hook_window|startup_hook_overlap)$' --no-tests=error --output-on-failure — 3/3 passed (startup_hook_window 0.07s, startup_hook_overlap 0.78s, startup_launch 1.77s)
  • Release /O2 (--preset release): ctest --preset release -R '^(startup_launch|startup_hook_window|startup_hook_overlap)$' --no-tests=error --output-on-failure — 3/3 passed (startup_hook_window 0.07s, startup_hook_overlap 0.75s, startup_launch 1.76s)

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-26T10:46:36.092276Z
We couldn’t complete this review. Comment @codex review to try again.
88e0aa8 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3cfa1726b4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/startup/hook_window.py Resolved
Comment thread docs/startup-runtime.md Resolved
@SimSalabimse

Copy link
Copy Markdown
Contributor

Tested on Windows 11 26200, MSVC 19.42, RTX 4070 Ti (driver 621.35) at 3cfa172: startup_launch, startup_hook_window and startup_hook_overlap all passed. Overlap traced 64/64 opens, while the control lost 12.

AI-Assisted

@tedin7
tedin7 force-pushed the x2w-frontier-1281-hooks branch from 86d81f9 to 6df8cf0 Compare November 24, 2026 11:21
@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @SimSalabimse for verifying on Windows 11 26200 / MSVC 19.42! Rebased onto current main at 6df8cf0a after addressing the Codex P2 notes (3/3 pass on Win2025 Debug & Release).

@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @SimSalabimse: that is the hardware pass (RTX 4070 Ti, 64/64 opens traced against 12 lost by the control) on top of our Windows Server 2025 run. No change to the head is needed.

@RenzoWit35

Copy link
Copy Markdown
Contributor

Ran this branch on a Windows 11 client (build 26300, MSVC 14.51, Python 3.14), commit 86d81f9 (same hook code as the rebased head 6df8cf0).

startup_hook_window, startup_launch, startup_debug and startup_host_cpu_debug pass. The
tracing itself looks right: in every protected run 64 of 64 opens were traced. A second probe of
mine, two threads opening their own file 64 times each without a barrier, gave exact per-thread
counts in 20 of 20 runs, where current main is wrong in 16 of 20.

startup_hook_overlap is intermittent here: 17 of 40 runs failed with (262, 1, 0) or
(262, 2, 0), so unprotected was 1 or 2 with all 64 opens traced. Single runs do pass, which fits the passes reported above. I logged the failed holds
in 15 more runs (5 failed). All 8 failed holds looked the same: SuspendThread failed,
GetExitCodeThread returned 259, the handle was still not signaled after the 20 ms wait, and
NtSuspendThread on the same handle returned 0xC000004B (STATUS_THREAD_IS_TERMINATING). My
guess is that the thread is inside its exit and waits for the debugger to take its EXIT_THREAD
event, so it cannot become signaled while the window is open. Treating 0xC000004B from
NtSuspendThread as GONE (the thread runs no more user code) covered it in a similar change
here. The failures appeared when the test was started from PowerShell, directly or through ctest;
15 runs started from Git Bash all passed, and I don't know why the shell matters.

Cost: 2 x 2000 serial opens took 7.1 to 8.0 s on main and 12.5 to 13.3 s at 2fabc90 (three runs
each; similar at 86d81f9 on a busier machine). In a similar change most of that was OpenThread
per held thread per step, about 80 µs each here. The hThread from the CREATE_THREAD and
CREATE_PROCESS events already has suspend/resume and get-context access, and the system closes it
after the exit event.

AI-assisted.

@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @RenzoWit35, great catch on the Win11 26300 STATUS_THREAD_IS_TERMINATING case. hold_thread now suspends with NtSuspendThread and treats 0xC000004B and 0xC0000008 as GONE (a unit test covers it, and it does not count as unprotected). It also reuses the hThread from the CREATE_PROCESS and CREATE_THREAD debug events, and only falls back to OpenThread when that handle is missing. It never closes the OS-provided handle.

Verified on Windows Server 2025 at 4b05247a: startup_launch, startup_hook_window and startup_hook_overlap pass 3/3 in both Debug and Release /O2. I haven't re-run it on Win11 26300.

@tedin7
tedin7 force-pushed the x2w-frontier-1281-hooks branch 2 times, most recently from c0816fa to ecbbf3e Compare November 24, 2026 13:15
@SimSalabimse

Copy link
Copy Markdown
Contributor

Retested at ecbbf3e on Windows 11 26200, MSVC 19.42, RTX 4070 Ti (driver 621.35): startup_launch, startup_hook_window and startup_hook_overlap pass in both presets (ctest exit 0 for dev and release).

startup_hook_overlap traced 64/64 opens with the window on both presets; the unprotected control lost 21 opens on dev and 30 on release. So the NtSuspendThread path holds on Win11 as well.

AI-Assisted

@SuperCraft3M

Copy link
Copy Markdown
Contributor

Independent integration check on Windows 11 build 26300.9711, Python 3.13 / MSVC 19.44 Debug: combined main 8f43802, #1251 at ad94c95, and #1408 at ecbbf3e (plus the independent UTF-8 storage change #1551).

The merge needs one semantic resolution in debug_launch.py: store (description, restore) as the ReturnBook payload, then invoke the restore callback when taking the matching return. Keeping only the description loses the mapped caller-name restoration. No change to ReturnBook's bookkeeping API is needed.

The combined source-built map overlap control reports 64 opens / 64 traces / 64 rewrites, two pooled name allocations, and both caller names intact. Its former WILL_FAIL registration can therefore become a normal pass when both PRs are present.

ctest --test-dir C:/Xbox2Windows-build --no-tests=error -R '^startup_(map_drive|map_drive_overlap|hook_window|hook_overlap)$' --output-on-failure: 4/4 passed, no skips. Integration and gate changes are on SuperCraft3M/Xbox2Windows branch runtime/startup-null-diagnosis, commit 3098828. This is an integration check of the existing PRs, not a replacement implementation. No binary inputs or raw runtime traces attached.

AI-assisted (Codex).

@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @SuperCraft3M for the independent integration check with #1251 (and noting the (description, restore) ReturnBook payload resolution for debug_launch.py when both PRs are combined).

Rebased #1408 onto origin/main at 19d57310 (resolving the docs/experiments.md conflict) and re-verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207):

  • Debug (--preset dev): ctest --preset dev -R '^(startup_launch|startup_hook_window|startup_hook_overlap)$' --parallel 4 --no-tests=error --output-on-failure — 3/3 passed (startup_hook_window 0.07s, startup_hook_overlap 0.80s, startup_launch 2.01s)
  • Release /O2 (--preset release): ctest --preset release -R '^(startup_launch|startup_hook_window|startup_hook_overlap)$' --parallel 4 --no-tests=error --output-on-failure — 3/3 passed (startup_hook_window 0.07s, startup_hook_overlap 0.83s, startup_launch 1.98s)

@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Updated the branch for the conflict with main.

  • New base: origin/main at e263314 (merged, no force push)
  • New head: c100ac3
  • Conflict resolved: docs/startup-runtime.md. Main's "Trap-by-default header check" and this PR's "Concurrent calls through a hooked function" sections are both kept.
  • Run locally: git diff --check origin/main...HEAD, tests/startup_hook_check.py (startup_hook_window PASS), tests/docs_links_check.py (378 links), tests/bidi_controls_check.py, py_compile of the touched tools: all passed.
  • Windows Server 2025 (x2w_pipe.py win2025 1408) at c100ac3: Debug 6/6 and Release 6/6 passed.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Updated the branch for the new conflict with main (merge, no force push).

  • New base: origin/main at 84ce294. New head: 36bb4ad.
  • Conflicts: CMakeLists.txt (kept both the hook tests and startup_launch_cli_check) and docs/experiments.md (kept both sections).
  • The two earlier review threads (tail-called hook returns, wording of the unprotected count) were already fixed in 2229407 and answered; nothing new to change.
  • Run locally: tests/startup_hook_check.py (startup_hook_window PASS), tests/docs_links_check.py (397 links), tests/bidi_controls_check.py, py_compile of tools/startup, git diff --check origin/main...HEAD: all passed.
  • Windows Server 2025 (x2w_pipe.py win2025 1408): Debug 7/7 and Release 7/7 passed on the previous head c100ac3; the merge only touches test registration and docs, so a re-run on 36bb4ad is left to CI.

AI-assisted; commands listed were executed locally.

@tedin7 tedin7 closed this Nov 25, 2026
@tedin7 tedin7 reopened this Nov 26, 2026
@tedin7
tedin7 marked this pull request as draft November 26, 2026 10:34
c1 added 2 commits November 26, 2026 11:34
The hook window replaced the returns dict with ReturnBook; the hit-limit release still called returns.get(addr), which the class does not have.
The merge left a second file_log assignment that opened files.txt only when tracing, overriding the main definition that also opens it for X2W_LOG_AT. startup_debug_log then failed with a None log.
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Win2025 receipt for head 88e0aa8 (origin/main merged in, no force push):

  • startup_launch|startup_hook_window|startup_hook_overlap|startup_debug on Windows Server 2025 build 26100: dev 7/7 and release 7/7 passed (startup_launch, startup_launch_cli_check, startup_hook_window, startup_hook_overlap, startup_debug, startup_debug_log, startup_debug_hooks).
  • startup_hook_overlap: 64 of 64 opens traced, three times, unprotected=0; the control with --suspend-others 0 lost opens.
  • Two defects from the merge with main were fixed in this head: ReturnBook had no get() for the hit-limit release (pending() added, with a test), and a duplicate file_log assignment broke startup_debug_log.
  • The two-thread --map-drive case depends on startup: map a console package drive letter to a real folder #1251, which is not in main, so it is not part of this run.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

Independent Stage-2 gate of head 08f0f4e: PASS (supersedes my receipt for 88e0aa8).

Checked:

  • tools/startup/hook_window.py / debug_launch.py: the owner steps alone while every other live thread is held (NtSuspendThread plus a GetThreadContext to wait until it has stopped); a thread that trapped before it was held goes back to the hook address and is traced after the window; a thread created in the window is held; a thread that is already exiting counts as gone; every window has a 2 s deadline and the counters are always reported.
  • Three further single mutations of hook_window.py (defer for any thread, _expose not counted, any thread closes the window) are killed by tests/startup_hook_check.py, on top of the author's five.
  • startup_hook_overlap asserts the real-thread result itself: 4 threads x 16 rounds through one hooked CreateFileW (the probe from this PR), every one of the 64 opens traced with its result and unprotected=0 / timeouts=0 / protection=on in three runs; the control --suspend-others 0 must lose opens and report unprotected above 0, else the test fails.

Verification (Win2025, proxmox only):

  • python3 -B tests/startup_hook_check.py, tests/docs_links_check.py, py_compile of the Python files: pass on Linux.
  • x2w_pipe.py win2025 1408 --host proxmox --force --keep-release --regex '^(startup_launch|startup_hook_window|startup_hook_overlap|startup_debug)': dev 7/7 and release 7/7 passed (startup_hook_overlap 0.8 s dev, 0.9 s release).

Not covered, as the PR says: the two-thread --map-drive case needs #1251.

Update, head 08f0f4e (ReturnBook restore callback): the callback runs once, when its call returns, and only for that call; a re-trap of the same call keeps the first restore (the caller's original state). Three single mutations (restore order swapped, callback never run, callbacks of other calls run too) are killed by tests/startup_hook_check.py. x2w_pipe.py win2025 1408 --host proxmox --force --keep-release --regex '^(startup_launch|startup_hook_window|startup_hook_overlap|startup_debug)': dev 7/7 and release 7/7 passed.

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 marked this pull request as ready for review November 26, 2026 10:46
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

A hooked call may change its arguments for the call only (a mapped drive name). The book now keeps an optional restore per call and runs it once on the return, so the caller's state is back before anything else runs; a re-trap of the same call keeps the first restore.
tedin7 pushed a commit to triggered02/Xbox2Windows that referenced this pull request Nov 26, 2026
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Head moved to 08f0f4e: ReturnBook gets an optional per-call restore callback (run once when the call returns; a re-trap of the same call keeps the first restore), so #1251 can restore a rewritten name through the same return book. Covered in startup_hook_check.

Win2025 (build 26100) on 08f0f4e: startup_launch|startup_hook_window|startup_hook_overlap|startup_debug dev 7/7 and release 7/7 passed (hook_overlap 64 of 64 with the window, control loses opens).

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

@MasterDomino these are tested and Ready. Could you merge them in this order when you have a moment?

  1. startup: hold other threads while a hook steps over its byte #1408 (hold other threads while a launcher hook steps over its byte)
  2. startup: map a console package drive letter to a real folder #1251 (map a package drive letter to a folder): builds on startup: hold other threads while a hook steps over its byte #1408. The change request on it is addressed: per-call buffers, and the overlap test now fails on a shared-buffer copy. Win2025 dev 9/9 + release 9/9.
  3. startup: decode the MSVC C++ exception record into a sanitized summary #1939 (decode the C++ exception record)
  4. perf: remember a refused buffer memory SRV for a short, validated time #2782
  5. test: allow one 8-bit filter weight step in the linear sampler probes #2794
  6. tests: serve GPU draw cases from one offscreen_check device #2766

Each PR has its Win2025 dev + release receipt in its comments. Thanks!

tedin7 pushed a commit to tedin7/Xbox2Windows that referenced this pull request Nov 26, 2026
tedin7 pushed a commit to tedin7/Xbox2Windows that referenced this pull request Nov 26, 2026
tedin7 pushed a commit to tedin7/Xbox2Windows that referenced this pull request Nov 26, 2026
Keeps R0Y00#2786 (slot 57 mesh buffer reads). R0Y00#1951 duplicates R0Y00#1408, which tester-loop already carries: debug_launch.py
keeps R0Y00#1408's hold-and-step (hook_window StepWindow); R0Y00#1951's step_gate.py, its check and its CI line are dropped.

@pigalot pigalot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independently reviewed 08f0f4e9 integrated over main 5ebdc581 (tree ad6137f4, clean merge).

I traced the step window through the breakpoint handler in debug_launch.py and through hook_window.py. StepWindow.open() holds every other thread. Only finish() releases them early, from the owner's single-step or its EXIT_THREAD; otherwise they stay held until the 2 s deadline. One path opens a window with no step behind it; see the finding below.

Built startup_launch_probe, startup_debug_probe and startup_debug_calls in dev and release, then ran
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^(startup_launch|startup_launch_cli_check|startup_hook_window|startup_hook_overlap|startup_debug|startup_debug_log|startup_debug_hooks)$':
7/7 passed in Debug and in Release /O2, no skips.

ctest ... --repeat until-fail:20 -R '^startup_hook_overlap$', started from PowerShell, passed 20 of 20 in each preset.

On main 5ebdc581, startup_hook_window and startup_hook_overlap are not registered. A mutant whose hold_thread reports HELD without calling NtSuspendThread fails startup_hook_overlap with 16 of 64 opens traced. git diff --check is clean.

Finding: a LOG_AT breakpoint retired at its hit limit leaves its step window open until the deadline.

In the breakpoint handler, window.open(tid, ...) runs before the hit-limit branch. That branch:

  • restores the original byte for good;
  • calls set_ctx;
  • returns without setting TF.

So no single-step event follows, and finish(tid) is never called for that window. The other threads stay held until the 2 s deadline. Then expire() counts the window as both a timeout and unprotected, although nothing could be missed: the byte is no longer patched.

To reproduce, use the source-built startup_debug_probe. It calls its log site 19 times, then waits 3 s.

python -B tools/startup/debug_launch.py <probe> <work> <logdir> --timeout 30 --console 0 --continue-asserts 10 --offline 0

Set X2W_LOG_AT=<site>:rdx with a hit limit, taking <site> from the probe's debug-probe log site line:

  • @2 (retire path): hook windows: entries=0 windows=2 unprotected=1 deferred=0 timeouts=1 protection=on. The run took 5.1 s.
  • @100 (control, no retire): windows=19 unprotected=0 timeouts=0. The run took 3.1 s.

Two runs each gave the same result.

Suggested direction (tried locally, not pushed): move window.open(...) below the retire branch, directly before TF is set. With that change, @2 reports windows=1 unprotected=0 timeouts=0 in 3.1 s, and the 7 tests above still pass in Debug.

The existing log-at case in tests/startup_debug_check.py already takes the retire path (@2 of 19 calls) but doesn't read the counters. An assert on timeouts=0 unprotected=0 there would catch this.

Environment: Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Python 3.12.10. No GPU is involved in these tests.
CI: Build and test on Windows passes on 08f0f4e9.

This reply was AI-assisted.

fix: open the hook step window only when a single-step follows
@tedin7

tedin7 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the trace. Pushed ac0f5e216, which merges current main and fixes the finding: window.open(...) now runs after the hit-limit retire branch, directly before TF is set, so a retired LOG_AT breakpoint no longer leaves a window open until the deadline. The log-at case in tests/startup_debug_check.py now also asserts unprotected=0 and timeouts=0.

Not yet run on Windows: startup_debug with the new assertion.

Commands run locally: python3 -m py_compile tools/startup/debug_launch.py tests/startup_debug_check.py, cmake -S . -B <tmp> -DDXC_EXECUTABLE=/bin/true, public_guard.py.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

Verification: head ac0f5e216 after the main merge and the latest fixes.

Environment: Windows Server 2025 (build 26100), MSVC 14.44, D3D12 WARP and the hardware adapter, dev and release legs in parallel.

Commands (the same selection in both legs):

x2w_pipe.py win2025 1408 --force --regex '^(startup_debug.*|startup_hook_window|startup_hook_overlap)$'
  dev:     cmake --build --preset dev --target auto
           ctest --preset dev -R '^(startup_debug.*|startup_hook_window|startup_hook_overlap)$' --no-tests=error --output-on-failure
  release: cmake --build --preset release --target auto
           ctest --preset release -R '^(startup_debug.*|startup_hook_window|startup_hook_overlap)$' --no-tests=error --output-on-failure

Result: 6/7 passed, 1 skipped by capability (startup_debug_window), no failures, in both legs.

AI-assisted; commands listed were executed locally.

@pigalot pigalot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed ac0f5e21 merged over current main 28e4a025. The merge is clean and git diff --cached --check is clean.

The finding from my review of 08f0f4e9 is fixed. window.open(...) now sits after the hit-limit retire branch, directly before TF is set. A LOG_AT breakpoint that retires at its limit therefore returns without opening a window. The log-at case in tests/startup_debug_check.py now asserts unprotected=0 and timeouts=0 on the hook windows: line.

What I ran. I built startup_launch_probe startup_debug_probe startup_debug_calls in dev and release, then ran

ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(startup_launch|startup_launch_cli_check|startup_hook_window|startup_hook_overlap|startup_debug.*)$'

9/9 pass in both presets, with no skips. The set is startup_debug, _log, _hooks, _window, startup_debug_launch_args, startup_launch, startup_launch_cli_check, startup_hook_window and startup_hook_overlap. startup_debug_window passes on this host; tedin7's Server 2025 run reports it as a capability skip.

Negative control. I moved window.open(...) back above the retire branch, which is the 08f0f4e9 order. startup_debug_log, the group that holds the log-at case, then fails on the new assertion with

process exited with 0x00000000 after 5.01 s
  hook windows: entries=0 windows=2 unprotected=1 deferred=0 timeouts=1 protection=on

Those are the same counters and the same 5 s run time I reported before. With the PR's order the line reads unprotected=0 ... timeouts=0, and the run takes about 3 s. I restored the file afterwards, and git diff --quiet is clean.

Note for anyone re-running this: the case is in startup_debug_log. The anchored -R '^startup_debug$' runs only the core group, which doesn't reach it.

Verdict: approve on ac0f5e21.

Environment: Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; Python 3.12.10. No GPU is involved in these tests.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on ac0f5e21.

This reply was AI-assisted.

@pigalot
pigalot merged commit 6cc46a3 into R0Y00:main Nov 27, 2026
5 checks passed
JohnConnorNPC pushed a commit that referenced this pull request Nov 28, 2026
#1408 holds every other thread while the debugger steps one over a hooked
function. In a long title run that is tens of thousands of windows with about
100 threads alive, and it slows the load after the first-run pages 3-4x.
debug_launch.py already has --suspend-others 0; the launchers now pass it
(run.ps1 -SuspendOthers 0, run.sh --suspend-others 0, default 1 = hold).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

startup-tools: the launcher drops concurrent hits on a hooked function

5 participants