startup: open the trap log once per module #1750
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1757"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-24T18:10:03Z.
Problem
The startup stand-ins append their diagnostic lines to the file named by
X2W_TRAP_LOG. Each of the four copies of that code opened the file, wrote one line and closed it again, for every line. Under the startup launcher every file open is a traced call that stops the process, so the log alone caused a steady stream of stops.Change
x2w_trap::append_log(line, size, flush)insrc/startup_stubs/trap.hreplaces the four copies:x2w_trap::record()(trap, continue and fail lines)src/startup_stubs/trap.hx2w_local::logfsrc/startup_stubs/local_log.hlog()src/startup_stubs/d3d12_xs_device.cpplog()src/startup_stubs/xcrdapi.cppNo other copy of the open/append/close block exists in
src/.Contract:
FILE_APPEND_DATAand shared read, write and delete. Other modules and live readers keep working, and a reader or test can still delete the log between lines.InterlockedCompareExchangePointer. A thread that loses the race closes its own handle.log_retry_ms) before the next open attempt. Lines in that wait are dropped, as the per-line writer dropped a line it could not open for. Trap lines always attempt the open, so a trap still reaches the log once the path is usable. An unusable path costs at most one open attempt per 250 ms per module (measured: 4 attempts for 78,000 to 231,000 lines in one second).FlushFileBuffersbefore the process can be terminated. Ordinary lines do not flush.WriteFile, so lines from concurrent threads do not interleave within a line.GetLastError(logf) still do.X2W_TRAP_LOGis unset or too long, nothing is opened or written, as before.In a 600 s startup run of a console executable on Windows 10 19045, the traced opens of the log file fell from 5,076 to 8 (one per stand-in module).
Tests
Windows 10 19045, MSVC,
devandreleasepresets:Result: 8/8 passed in
devand 8/8 passed inrelease, 0 skipped.startup_local_logruns 8 threads × 2,000logflines in a child process, so its module starts with no log handle. Observed:opens=1 lines=16001 bad=0 missing=0 duplicate=0. Every line is present exactly once and intact.logfpath checks (CRLF, 1533-byte clamp,MAX_PATHlimits,GetLastErroron open failure, stderr copy) are unchanged. Before them the test keeps the module's handle onNUL, so those checks exercise the changed-path route and their files stay removable.startup_local_log_guard_drop_control(existing,WILL_FAIL) still fails only on its guard-page expectation.startup_local_log_reopen_control(new,WILL_FAIL) is the negative control. After the first line, the child respells the path as the same file (<dir>\.\trap.log), so every following line reopens the file. Observed:opens=16001, all 16,001 lines intact, and the only failure istrap log is opened once per module. The child exits 3 only when the open count is its sole failure. The parent returns failure only for exit code 3, so a lost or torn line, a crash, or a child that never starts makes the control fail rather than pass.X2W_TRAP_LOGpoints into a directory that does not exist, so the first append fails and its line is dropped. The child creates the directory, waits past the interval, and a later append must land as the file's only line; 100 more lines follow in order without another open.CreateFileWwithout sharing (a sharing violation), then released.startup_local_log_cache_failure_control(new,WILL_FAIL) builds the same check against a generated copy oftrap.hwhose failed open is kept forever. It passes only when the two recovery checks are its sole failures. Observed: both recovery children exit 3, failinga later line reaches the log once the path is usable; every other check passes.startup_stubs(the stand-in DLL check that also readsX2W_TRAP_LOG) andstartup_trap_headerwith its two controls (from #1555) pass.tests/docs_links_check.pyandtests/bidi_controls_check.pypass.Compatibility with #1555
#1555 (now in main) adds
startup_trap_header. Its step 4 callsDeleteFileWon the log after in-processrecord()calls, then expects a child process to write a fresh log. A kept handle without delete sharing made that delete fail. The child then appended to the old file, and the folder could not be removed. The kept handle therefore also shares delete.startup_trap_headerand its two controls pass on this branch in both presets.Not covered
d3d12_xsandXCRDAPIcall sites are covered bystartup_stubsand the shared helper test, not by a dedicated multi-threaded fixture each.AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T18:49:56Z.
Review: COMMENTED
Reviewed at
7955a4f1f2d8b8d906a8c48af07152c778f64343.No P1 or P2 defects found in local verification.
x2w_trap::append_loginsrc/startup_stubs/trap.hsafely publishes the first module-wideLogentry (handle+path) viaInterlockedCompareExchangePointerafter initializing it, opens withFILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETEso concurrent readers andDeleteFileWcleanup continue to work, caches failed opens asINVALID_HANDLE_VALUEso they are not retried per line, and falls back to open/write/close when%X2W_TRAP_LOG%changes in-process.src/startup_stubs/local_log.h,src/startup_stubs/d3d12_xs_device.cpp, andsrc/startup_stubs/xcrdapi.cppall delegate tox2w_trap::append_log, andtests/startup_local_log_check.cppverifies 8-thread x 2,000-line concurrency withopens == 1plus thestartup_local_log_reopen_controlnegative control.Verification executed:
git diff --check origin/main...HEAD-> pass/root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py-> pass (17 Markdown files, 364 links)/root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py-> pass (1287 tracked files)python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py win2025 1757 --targets startup_local_log_check,d3d12_xs_shim,startup_stub_xcrdapi --regex '^startup_local_log(_guard_drop_control|_reopen_control)?$' --wait 600-> pass on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP): Debug (--preset dev) 3/3 passed (startup_local_log,startup_local_log_guard_drop_control,startup_local_log_reopen_control), Release/O2(--preset release) 3/3 passedsrc/startup_stubs/trap.h(if (false && current && !std::wcscmp(current->path, path))) and ranpython3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py win2025 1757 --wt /tmp/rev-sm2-rexa/1757 --targets startup_local_log_check --regex '^startup_local_log$' --wait 600;startup_local_logfailed as expected (opens=16001,FAIL trap log is opened once per module,FAIL append_log reuses the kept handle), then restored withgit checkout -- ..AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-24T18:52:08Z.
Thanks for the review and the mutation check.
The review raises no defects and has no inline comments, so the PR needs no code change; the head stays at
7955a4f1. Your mutation result matches what the fixture is meant to catch. With the kept-handle reuse bypassed,startup_local_logreportsopens=16001and fails bothtrap log is opened once per moduleandappend_log reuses the kept handle, independently of thestartup_local_log_reopen_controlnegative control.Last local run on this head (Windows 10 19045, MSVC,
devandrelease):Result: 4/4 passed in
devand 4/4 passed inrelease, 0 skipped.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:31:22Z.
Review: COMMENTED
Re-review at head
d33f2d189521a13159a161aefc8a11e7c396d5ef(previously reviewed at7955a4f1f2d8b8d906a8c48af07152c778f64343).Verdict: no P1, no P2. Nothing in the PR changed; the push only merged
origin/main.What changed since the last review
git diff origin/main...7955a4f1andgit diff origin/main...d33f2d18are the same 7 files, 234 insertions / 45 deletions. Compared line by line, they differ only in theindexlines and in the hunk offsets and surrounding context ofCMakeLists.txt(startup_trap_header_checklanded in main next to the new test). The code, test and doc edits are identical, so the earlier read-through oftrap.h(compare-exchange publication of the module-wide handle, full share mode, failed open cached asINVALID_HANDLE_VALUE, fallback to open/write/close when%X2W_TRAP_LOG%changes) still stands.Commands run locally
git diff --check origin/main...HEAD: exit 0.tests/docs_links_check.py: 18 Markdown files, 376 links, passed.tests/bidi_controls_check.py: 1337 files, no bidirectional controls.startup_local_log_check d3d12_xs_shim startup_stub_xcrdapi, regex^startup_local_log(_guard_drop_control|_reopen_control)?$: Debug 3/3 and Release /O2 3/3 passed.Mutation (reverted with
git checkout -- .)src/startup_stubs/trap.h:84, disabled the kept-handle reuse (if (false && current && ...)) and ranstartup_local_logon Windows with a patched worktree: FAILED as expected (opens=16001, "trap log is opened once per module", "append_log reuses the kept handle").AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T21:34:05Z.
Review: CHANGES_REQUESTED
The concurrent append benchmark establishes the useful fast path. One recovery behavior changes: if the first open fails while the parent directory is absent or the file is temporarily inaccessible, caching INVALID_HANDLE_VALUE permanently drops every later line for that path. The old writer retries, so a later trap can still reach the log. Please avoid permanently caching open failure (a bounded retry/backoff is fine if steady-state tracing cost matters). Add a source-built first-open failure followed by a directory creation and a successful later append. Keep the one-open count for an available, unchanged path.
Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-24T21:54:54Z.
Thanks, agreed: caching the failed open was a recovery regression. Changes in
148d6daa(after merging main at3920220f):Failed opens are no longer cached.
append_logkeeps a handle only after a successful open. While no handle is kept, a failed open makes ordinary lines waitlog_retry_ms(250 ms) before the next open attempt. Lines in that wait are dropped, as the per-line writer dropped a line it could not open for; nothing is buffered. Trap lines (flush) always attempt the open, so a later trap still reaches the log as soon as the path is usable. Cost on a path that stays unusable: at most one open attempt per 250 ms per module for ordinary lines. Measured: 4 attempts while 78,000 to 231,000 lines were logged in one second; the per-line writer would have tried once per line.Tests, source-built, each case in a fresh child process:
X2W_TRAP_LOGpoints into a directory that does not exist; the first append fails and its line is dropped. The child then creates the directory, waits past the interval, and a later append must land as the file's only line. 100 more lines must follow in order without another open.CreateFileWand no sharing, so the first append fails with a sharing violation. After the handle is released, a later append must land, with the same follow-up checks. This is deterministic because the same process holds and releases the file.opens=1 lines=16001).startup_local_log_cache_failure_control(WILL_FAIL) builds the same check against a generated copy oftrap.hwhose failed open is kept forever. It passes only when the two recovery checks are its sole failures. Observed: both recovery children exit 3, witha later line reaches the log once the path is usablefailing; every other check passes.Command, Windows 10 19045, MSVC,
devandrelease:Result: 8/8 passed in
devand 8/8 passed inrelease, 0 skipped.tests/docs_links_check.pyandtests/bidi_controls_check.pypass.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T22:08:14Z.
Review: APPROVED
Rechecked the exact recovery fix. Windows Release passed all four startup_local_log tests, including the old failed-open configuration as a negative control.