startup: make the save lock wait without overflow and name a failed open #1508

Merged
tedin7 merged 2 commits from upstream-pr/1515 into main 2026-11-24 14:10:03 +00:00
tedin7 commented 2026-11-24 13:26:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T13:26:53Z.


What

  • FileLock::acquire computes its deadline with saturation. A timeout too large for the clock (for example std::chrono::milliseconds::max()) now means "wait for ever"; a zero or negative timeout (including milliseconds::min()) means one attempt. Ordinary timeouts are unchanged.
  • FileLock::acquire resets last_error() at the start and records GetLastError() / errno right after a failed open. The comment of last_error() now says it covers a failed open too.
  • tests/startup_gamesave_lock_errors.cpp keeps the integer-mapping checks and adds the wait, one-attempt, failed-open and no-error cases.

Why

  • The deadline was steady_clock::now() + timeout. The conversion of milliseconds::max() to nanoseconds overflows (signed overflow, undefined behaviour), in practice wrapping to a deadline in the past. A caller asking to wait for ever gave up at once with LockResult::timeout.
  • A failed open() returned LockResult::error but left last_error() at the value of the previous call (possibly the busy code) or 0, although the comment promises it says why a lock ended in busy or error.

Tested

Linux (g++, native):

g++ -std=c++17 -Wall -Wextra -pthread -I src tests/startup_gamesave_lock_errors.cpp -o t && ./t
-> startup_gamesave_lock_errors: ok
g++ -std=c++17 -g -fsanitize=undefined,address -pthread -I src tests/startup_gamesave_lock_errors.cpp -o ts && ./ts
-> startup_gamesave_lock_errors: ok
x86_64-w64-mingw32-g++ -std=c++17 -Wall -Wextra -pthread -static -I src tests/startup_gamesave_lock_errors.cpp -o t.exe
-> builds without warnings; wine t.exe -> startup_gamesave_lock_errors: ok (LockFileEx semantics under wine, not native Windows)
g++ ... tests/startup_gamesave_persist.cpp -> ok
python3 -B tests/docs_links_check.py; python3 -B tests/bidi_controls_check.py; git diff --check -> clean

Negative control: the new test compiled against the unchanged header from origin/main:

FAIL: a wait for ever ends locked, not timeout, once the holder lets go
FAIL: a wait for ever really waits for the holder
FAIL: a failed open does not keep the earlier busy code

With -fsanitize=undefined the old header also traps: chrono.h:227: signed integer overflow: 9223372036854775807 * 1000000 cannot be represented in type 'long int'. The new header passes all cases.

Not run: MSVC and native Windows. tests/startup_gamesave_publish.cpp builds on Linux but hangs there (pause point 2) with the old header as well as the new one, so it is Windows-oriented and gives no signal here.

Audited, nothing found

  • dstorage_file_read.h: the >4 GiB case is closed by #1493; zero-byte and end-of-file edge reads behave.
  • storage_root.h: capacity and separator edges are already covered by its test.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6582833366 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1515). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T13:26:53Z. --- ## What - `FileLock::acquire` computes its deadline with saturation. A timeout too large for the clock (for example `std::chrono::milliseconds::max()`) now means "wait for ever"; a zero or negative timeout (including `milliseconds::min()`) means one attempt. Ordinary timeouts are unchanged. - `FileLock::acquire` resets `last_error()` at the start and records `GetLastError()` / `errno` right after a failed open. The comment of `last_error()` now says it covers a failed open too. - `tests/startup_gamesave_lock_errors.cpp` keeps the integer-mapping checks and adds the wait, one-attempt, failed-open and no-error cases. ## Why - The deadline was `steady_clock::now() + timeout`. The conversion of `milliseconds::max()` to nanoseconds overflows (signed overflow, undefined behaviour), in practice wrapping to a deadline in the past. A caller asking to wait for ever gave up at once with `LockResult::timeout`. - A failed `open()` returned `LockResult::error` but left `last_error()` at the value of the previous call (possibly the busy code) or 0, although the comment promises it says why a lock ended in busy or error. ## Tested Linux (g++, native): ``` g++ -std=c++17 -Wall -Wextra -pthread -I src tests/startup_gamesave_lock_errors.cpp -o t && ./t -> startup_gamesave_lock_errors: ok g++ -std=c++17 -g -fsanitize=undefined,address -pthread -I src tests/startup_gamesave_lock_errors.cpp -o ts && ./ts -> startup_gamesave_lock_errors: ok x86_64-w64-mingw32-g++ -std=c++17 -Wall -Wextra -pthread -static -I src tests/startup_gamesave_lock_errors.cpp -o t.exe -> builds without warnings; wine t.exe -> startup_gamesave_lock_errors: ok (LockFileEx semantics under wine, not native Windows) g++ ... tests/startup_gamesave_persist.cpp -> ok python3 -B tests/docs_links_check.py; python3 -B tests/bidi_controls_check.py; git diff --check -> clean ``` Negative control: the new test compiled against the unchanged header from origin/main: ``` FAIL: a wait for ever ends locked, not timeout, once the holder lets go FAIL: a wait for ever really waits for the holder FAIL: a failed open does not keep the earlier busy code ``` With -fsanitize=undefined the old header also traps: `chrono.h:227: signed integer overflow: 9223372036854775807 * 1000000 cannot be represented in type 'long int'`. The new header passes all cases. Not run: MSVC and native Windows. `tests/startup_gamesave_publish.cpp` builds on Linux but hangs there (pause point 2) with the old header as well as the new one, so it is Windows-oriented and gives no signal here. ## Audited, nothing found - `dstorage_file_read.h`: the >4 GiB case is closed by #1493; zero-byte and end-of-file edge reads behave. - `storage_root.h`: capacity and separator edges are already covered by its test. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-24 14:03:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T13:42:05Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit f4d2a92f855cb24aacd8be45784577748ac9e2ff:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target startup_gamesave_lock_errors -- -j 6): 4.19s (configure 0.00s)
    • Test (ctest --preset dev -R '^startup_gamesave_lock_errors' --parallel 4 --no-tests=error --output-on-failure): 1/1 passed (0.25s: startup_gamesave_lock_errors 0.17s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target startup_gamesave_lock_errors -- -j 6): 1.84s (configure 0.00s)
    • Test (ctest --preset release -R '^startup_gamesave_lock_errors' --parallel 4 --no-tests=error --output-on-failure): 1/1 passed (0.24s: startup_gamesave_lock_errors 0.17s)
  • sccache: 0/2 hits; wall 12.1s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7133209624 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1515#issuecomment-7133209624). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T13:42:05Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `f4d2a92f855cb24aacd8be45784577748ac9e2ff`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target startup_gamesave_lock_errors -- -j 6`): `4.19s` (configure `0.00s`) - Test (`ctest --preset dev -R '^startup_gamesave_lock_errors' --parallel 4 --no-tests=error --output-on-failure`): **1/1 passed** (`0.25s`: `startup_gamesave_lock_errors` `0.17s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target startup_gamesave_lock_errors -- -j 6`): `1.84s` (configure `0.00s`) - Test (`ctest --preset release -R '^startup_gamesave_lock_errors' --parallel 4 --no-tests=error --output-on-failure`): **1/1 passed** (`0.24s`: `startup_gamesave_lock_errors` `0.17s`) - `sccache`: 0/2 hits; wall `12.1s` AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-24 14:03:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T13:42:16Z.


Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T13:45:10.438345Z f4d2a92 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7133213996 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1515#issuecomment-7133213996). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T13:42:16Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"f4d2a92f855cb24aacd8be45784577748ac9e2ff","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-24T13:45:10.438345Z">2026-11-24T13:45:10.438345Z</relative-time> | `f4d2a92` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). 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. </details> </details>
upstream-sync closed this pull request 2026-11-24 15:01:49 +00:00
ChazY11 commented 2026-11-24 15:02:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T14:47:41Z.


Saturation is covered; the zero/negative early return is an equivalent mutant

Independent reviewer, not the author. Reviewed f4d2a92f; nothing pushed to the branch. Mutations were made locally in
src/startup_stubs/file_lock.h, rebuilt, reverted; working tree is clean.

Environment: Windows 11 build 26300.9974, MSVC 14.44.35207, SDK 10.0.26100.

cmake --preset dev
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^startup_gamesave_lock' -V

startup_gamesave_lock_errors passes as submitted.

Mutation A — the saturating branch

-    if (timeout >= room) return (Clock::time_point::max)();
+    if (false) return (Clock::time_point::max)();
startup_gamesave_lock_errors ***Failed

Good: the new wait-for-ever case is what notices the missing saturation, so milliseconds::max() really is being
exercised rather than only described in the comment.

Mutation B — the zero/negative early return

-    if (timeout <= std::chrono::milliseconds::zero()) return now;
+    if (timeout < std::chrono::milliseconds::zero()) return now;
startup_gamesave_lock_errors Passed

This one is expected and I do not think it needs a test. For timeout == 0 the tail path computes room, finds
0 >= room false and returns now + 0, which is now; for a negative timeout now + timeout is in the past and the
acquire loop already performs a single attempt. So the early return is a shortcut, not a behaviour change, and no
observable assertion could distinguish it.

Worth a half-line in the comment if you want it documented ("a zero or negative timeout means one attempt; the early
return keeps that out of the loop arithmetic") — otherwise it reads like a branch that ought to be covered.

What I did not test

The failed-open last_error() recording. It needs an open that fails for a known reason and I would rather not guess at
which GetLastError/errno value your test asserts; if you say which case to drive I will run it.

Verdict

Reviewed f4d2a92f. Builds clean, the test passes, and the overflow fix is genuinely the thing the fixture measures.
No objection from me.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7134957738 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1515#issuecomment-7134957738). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T14:47:41Z. --- ## Saturation is covered; the zero/negative early return is an equivalent mutant Independent reviewer, not the author. Reviewed `f4d2a92f`; nothing pushed to the branch. Mutations were made locally in `src/startup_stubs/file_lock.h`, rebuilt, reverted; working tree is clean. Environment: Windows 11 build 26300.9974, MSVC 14.44.35207, SDK 10.0.26100. ```powershell cmake --preset dev cmake --build --preset dev ctest --preset dev --no-tests=error -R '^startup_gamesave_lock' -V ``` `startup_gamesave_lock_errors` passes as submitted. ### Mutation A — the saturating branch ```diff - if (timeout >= room) return (Clock::time_point::max)(); + if (false) return (Clock::time_point::max)(); ``` ```text startup_gamesave_lock_errors ***Failed ``` Good: the new wait-for-ever case is what notices the missing saturation, so `milliseconds::max()` really is being exercised rather than only described in the comment. ### Mutation B — the zero/negative early return ```diff - if (timeout <= std::chrono::milliseconds::zero()) return now; + if (timeout < std::chrono::milliseconds::zero()) return now; ``` ```text startup_gamesave_lock_errors Passed ``` This one is expected and I do not think it needs a test. For `timeout == 0` the tail path computes `room`, finds `0 >= room` false and returns `now + 0`, which is `now`; for a negative timeout `now + timeout` is in the past and the acquire loop already performs a single attempt. So the early return is a shortcut, not a behaviour change, and no observable assertion could distinguish it. Worth a half-line in the comment if you want it documented ("a zero or negative timeout means one attempt; the early return keeps that out of the loop arithmetic") — otherwise it reads like a branch that ought to be covered. ### What I did not test The failed-open `last_error()` recording. It needs an open that fails for a known reason and I would rather not guess at which `GetLastError`/`errno` value your test asserts; if you say which case to drive I will run it. ### Verdict Reviewed `f4d2a92f`. Builds clean, the test passes, and the overflow fix is genuinely the thing the fixture measures. No objection from me.
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
R0Y00/Xbox2Windows!1508
No description provided.