Skip to content

tools: preserve concurrent breakpoint hits in debug_launch - #1951

Closed
tedin7 wants to merge 7 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-1281-launcher
Closed

tedin7 wants to merge 7 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-1281-launcher

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Part of #1281

What this unblocks: the launcher keeps concurrent hits on a hooked function, so file tracing, the offline socket deny and the -MapDrive rewrite no longer miss overlapping calls.

Verification:

  • python3 -I tests/startup_debug_launch_concurrent_check.py -> Ran 3 tests, OK (Linux worker).
  • With the suspend step disabled the same test fails (1 failure), so it guards the fix.
  • python3 -I -m py_compile tools/startup/debug_launch.py tools/startup/step_gate.py -> ok.
  • Not run: hooked launch / startup_map_drive_overlap on Win2025 (no Windows host on this worker). Please run before merge.

Change: new tools/startup/step_gate.py suspends the other live threads while one steps over a restored hook byte, re-patches only after the last stepper finishes, and the launcher logs how many overlapping hits it held.

AI-assisted; commands listed were executed locally.

@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:42:10.132560Z
We couldn’t complete this review. Comment @codex review to try again.
ff733ed 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: aab45db628

ℹ️ 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/step_gate.py Resolved
Comment thread tools/startup/step_gate.py Resolved
@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Ran it, and found the one place the gate's guarantee is unasserted

Windows 11 build 26300.9974:

out/pe-env/Scripts/python.exe -I tests/startup_debug_launch_concurrent_check.py
Ran 3 tests in 0.000s
OK      exit 0

Mutations to tools/startup/step_gate.py, each reverted:

mutation result
for other in sorted(self._live()) → for other in [] (suspend nothing) caught
return None if addr in self.stepping.values() else addr → return addr (repatch while another thread steps) caught
overlap = addr in self.stepping.values() → False caught
if not self.stepping: → if True: (resume held threads while another thread still steps) not caught

Why the survivor matters, with the trace

Driving the gate directly with two threads stepping the same address:

begin 1 at A -> overlap False
begin 2 at A -> overlap True
  suspends: [('suspend',2), ('suspend',3), ('suspend',4), ('suspend',3), ('suspend',4)]
finish 1 -> None                      # still open, thread 2 is stepping it
  resumes while thread 2 still steps A: []        # correct

same run with `if True:`:
finish 1 -> None
  resumes while thread 2 still steps A: [('resume',2), ('resume',3), ('resume',3), ('resume',4), ('resume',4)]

The mutated gate releases every suspended thread the moment the first stepper finishes, while the second is still stepping over
the restored original byte — exactly the window the module docstring says it closes. The fixture counts dropped and checks the
address returned by finish, so both overlap and the repatch rule are pinned, but nothing asserts that no resume happens
before the last stepper finishes
.

Suggested assertion, in the same style

def test_no_thread_is_resumed_while_another_still_steps(self):
    resumed = []
    gate = StepGate(lambda tid: True, resumed.append, lambda: [1, 2, 3])
    gate.begin(1, 0xA0)
    gate.begin(2, 0xA0)
    resumed.clear()
    self.assertIsNone(gate.finish(1))
    self.assertEqual(resumed, [])                     # the whole point of the gate
    self.assertEqual(gate.finish(2), 0xA0)
    self.assertTrue(resumed)                          # and they do come back afterwards

The second half matters too: a gate that never resumes would pass the first assertion alone.

What the rest of the design gets right

  • _held is a count per thread, not a flag, so a thread suspended by two overlapping begin calls is resumed twice — the
    trace above shows suspend 3 and suspend 4 twice each.
  • forget(tid) drops both maps, so a thread that exits mid-step cannot be resumed later or block the repatch forever.
  • begin skips threads that are already stepping (other not in self.stepping), which is what keeps two steppers from
    suspending each other into a deadlock.

Scope

The Windows half (hooked launch, startup_map_drive_overlap) I cannot run here; the PR already says so. Everything above is the
pure-Python gate, which is where the concurrency rule lives.

Verdict

Reviewed. Four rules, three pinned; the missing one is the gate's own guarantee.

Notes

  • AI-assisted review.

The startup launcher restored the patched byte for a single step, so a second thread entering the
same function in that window was never traced. Other threads are now suspended for the step, the
byte is patched again only after the last stepper finishes, and overlapping hits are counted in the log.
@tedin7
tedin7 force-pushed the x2w-frontier-1281-launcher branch from f8dd917 to fef6bef Compare November 25, 2026 06:17
@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

@ChazY11 thank you, the survivor mutation was a real gap in the gate's own guarantee.

Added in b559ad5: test_no_thread_is_resumed_while_another_still_steps in tests/startup_debug_launch_concurrent_check.py. It checks that held threads stay suspended after the first stepper finishes, and that all of them are released after the last one.

Commands executed locally:

  • python3 -B tests/startup_debug_launch_concurrent_check.py -> Ran 6 tests, OK
  • mutation if not self.stepping: -> if True: in tools/startup/step_gate.py -> the new test fails (1 failure of 6); mutation reverted
  • python3 -B tests/docs_links_check.py -> OK

AI-assisted; commands listed were executed locally.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The exact Python suite passes, but StepGate.begin() still enters the stepping state when suspension of a known live peer fails. I reproduced this with two live IDs and a suspend callback returning false. The original byte is already restored by the time the caller enters the gate, and the caller then continues the debug event. That retains the trace gap this PR is meant to eliminate. Please make gate acquisition all-or-nothing before opening the byte, roll back successfully held peers when another suspension fails, and cover the failure with a deterministic fake. Also verify the real overlapping launch on Windows with a source-built child, including both simultaneous calls observed and cleanup.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks, all points addressed in abadcfb.

  • All-or-nothing gate: StepGate.begin() now suspends every live peer first and only then marks the thread as stepping (tools/startup/step_gate.py). On a failed suspension it returns None. debug_launch.py calls gate.begin() before it opens the byte, so the byte stays patched and the thread re-executes the breakpoint. Commit abadcfb.
  • Roll back held peers: when a later peer cannot be suspended, the peers already taken are resumed and nothing is recorded as held or stepping. Commit abadcfb.
  • Deterministic fake: StepGateTest.test_failed_suspension_leaves_the_gate_closed_and_rolls_back in tests/startup_debug_launch_concurrent_check.py uses four live IDs with peer 4 refusing suspension. It checks no stepping, dropped == 0, and 2 and 3 released. It then retries successfully and checks cleanup. Run with python3 -m unittest tests.startup_debug_launch_concurrent_check: 7 tests OK.
  • Win2025 WARP at head abadcfb (x2w_pipe.py win2025 1951): startup_host_cpu_debug, startup_run_ps1 and startup_debug pass in dev and release (3/3 each). These targets do not start two real overlapping launches with a source-built child; that case is covered only by the deterministic fake above.

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 requested a review from R0Y00 November 25, 2026 13:47
@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

@R0Y00 the changes you requested are addressed on the current head abadcfb3f0848b78d6940dcf4dddd2dd91b3799d (see our reply above). Could you re-review, or dismiss the change request if it is resolved? Under the new scope, MasterDomino can then review and merge it.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Closing this PR for now so we can focus on the test-speed work. The branch stays as it is, and review comments here remain valid.

We will reopen it later, bring it up to date with main, re-run the Win2025 WARP checks at its head, and post the result here.

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
# Conflicts:
#	.github/workflows/windows.yml
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Merged origin/main into this branch (no force; one conflict in the windows.yml check list, both lines kept). New head ff733ed34.

Verification:

  • Linux: python3 -B tests/startup_debug_launch_concurrent_check.py -> 7 tests OK.
  • Win2025 (proxmox), dev + release, head ff733ed34: x2w_pipe.py win2025 1951 --force --regex '^startup_(debug|launch)' -> 5/5 passed in both builds (startup_debug, startup_debug_log, startup_debug_hooks, startup_launch, startup_launch_cli_check); python checks tests/startup_debug_launch_concurrent_check.py and tests/startup_launch_cli_check.py rc=0.
  • One earlier dev run had startup_launch fail its "fast-fail" timing assertion (process killed at 1.00 s, the limit is < 1.0 s). It does not use debug_launch.py or step_gate.py; the immediate rerun passed twice.

Overlap with #1408: both change the same step-over region of debug_launch.py (the stepping map, thread exit, thread suspension). Proposed order is #1408 first, then this PR keeps only what #1408 does not already do (the concurrent check, the all-or-nothing retry when a peer cannot be suspended), or is closed as superseded if nothing is left.

Source: Microsoft Learn, Win32 debugging API (WaitForDebugEvent, ContinueDebugEvent) and SuspendThread / ResumeThread documentation.

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 ff733ed: PASS.

Checked:

  • The gate is all-or-nothing: every live peer is suspended before the hooked byte is opened; if one cannot be suspended the byte stays patched and the hit is retried. A thread that exits while stepping re-arms the byte (forget).
  • Real threads, not fakes: the overlap mode of tests/startup_launch_probe.cpp from startup: hold other threads while a hook steps over its byte #1408 (4 threads x 16 rounds, all entering CreateFileW together, 64 opens) run under tools/startup/debug_launch.py --trace-files 1 on Win2025 (proxmox): 3 runs with this PR, each overlap opens 64 of 64 and 64 of 64 successful NtCreateFile lines in files.txt. Control: the same run with step_gate.py replaced by a copy that never holds a peer lost opens on its first run (60 of 64 traced).

Verification (Win2025, proxmox only, head ff733ed plus that overlap run registered as an extra CTest):

  • x2w_pipe.py win2025 1951 --force --regex '^startup_(debug|launch)' (c1's run): 5/5 passed in dev and release.
  • Same regex with the overlap test added: dev 6/6 and release 6/6 passed.
  • python3 -B tests/startup_debug_launch_concurrent_check.py: pass on Linux.

Not covered by this PR's own tests: the real-thread overlap above is not in the PR (its test uses fake threads); #1408 has the real-thread test, so whichever of the two merges first should keep that fixture (the two PRs rewrite the same step-over region of debug_launch.py).

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 marked this pull request as ready for review November 26, 2026 10:42
@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.

@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as superseded by #1408, which fixes the same problem (concurrent hook hits while a hook steps over its byte, #1281) and covers every case this PR's check covers:

  • other threads cannot run through the step: StepWindow.open holds every other thread (startup_hook_check.py bookkeeping case);
  • the window closes only when its owner finishes or exits (finish, abandon), and no held thread resumes earlier;
  • a held thread that already hit the hook goes back to the hook address and traps again after the window (defer; this PR's "queued hit runs to step");
  • exited or exiting threads are classified and never resumed (classify_hold, classify_status);
  • one difference by design: this PR refused to open the byte when a peer could not be suspended and retried the hit. startup: hold other threads while a hook steps over its byte #1408 steps and reports the window as unprotected, because a thread that is exiting cannot be suspended and a retry would never end. startup: hold other threads while a hook steps over its byte #1408 also adds a deadline, the --suspend-others 0 control and a real-thread test.

Compared at #1408 head 08f0f4e9f against this PR's head ff733ed34; no case of this PR's startup_debug_launch_concurrent_check.py is missing there.

Source: Microsoft Learn, Win32 debugging API (WaitForDebugEvent, ContinueDebugEvent) and SuspendThread documentation.

AI-assisted; commands listed were executed locally.

@tedin7 tedin7 closed this Nov 26, 2026
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.

3 participants