Skip to content

wg_state: retry a state file's replace while another program briefly holds it - #266

Merged
phantoslayer21 merged 3 commits into
mainfrom
rootcause/wgstate
Nov 16, 2026
Merged

phantoslayer21 merged 3 commits into
mainfrom
rootcause/wgstate

Conversation

@phantoslayer21

@phantoslayer21 phantoslayer21 commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

What

The state files' atomic replace (MoveFileExW of the new temporary file over settings.ini and the other state files) is now retried for up to 2 s when it fails with ERROR_ACCESS_DENIED, ERROR_SHARING_VIOLATION or ERROR_LOCK_VIOLATION. These are the errors you get while another program briefly holds the file. The retry runs while the state lock is held. Before each retry, UpdateStateFile checks that the file is still the one it read. If the program that held the file wrote it meanwhile, the save is refused (SetResult::Changed) and that program's bytes stay. Any other error, or the same error after 2 s, fails exactly as before: the file is unchanged, the temporary file is deleted, and the error is reported (WriteResult, and SetResult::AccessDenied / WriteFailed).

WriteAtomic, UpdateStateFile (SetSettings) and the guard's completion marker all use it (MoveIntoPlace in wg_state.h).

Why

Under heavy file activity, such as a build or an antivirus or indexer scan of a file that was just written, another program can hold settings.ini or the temporary file for a moment. The single rename then failed with error 5 and the save was refused, even though the state lock was held and nothing else in WrongGPU touched the file. In the test suite this showed up as an occasional failure of the concurrent-increment check in wgshared_state. Children exited with code 4 (write failed, error 5). No increment was lost.

Tests

  • Retry: the first 3 replaces are refused (test seam move-busy) and the 4th writes the file.
  • Bound: replaces refused for longer than the bound make WriteAtomic fail with error 5 after about 2 s. SetSettings reports AccessDenied. The file is unchanged and no temporary file is left.
  • Changed: the first replace is refused while the holder writes the file. The retry stops with Changed, the holder's bytes stay, and no temporary file is left.
  • Real handle: settings.ini is opened read-only with read sharing, as a scanner does. Held for 300 ms, the write succeeds. Held past the bound, it fails visibly with its error and the file is unchanged.
  • Diagnostics (separate commit): each child of the concurrent-increment check now records why it stopped. On a failure the parent prints the final count, the exit codes and those reasons. The checks read settings through Val(), so a missing key fails the check instead of crashing the test.

test_wg_state: 69 passed, 0 failed (/W4 /WX).

…h on a missing key

The check "4 processes, 4 different aliases, 50 locked increments each: 200" did not tell a
child that stopped from an increment that was lost.

- Each --child-add writes why it stopped to a file of its own: the lock not held (with its
  error), settings.ini unreadable (now exit 5; it never counts from 0 again), no counter
  (exit 6), or the write's error words with the counter it had read.
- On a failure the parent prints the final count, every child's exit code and those reasons.
- The checks read a setting through Val(), "(none)" for a missing key, instead of dereferencing
  Get(): a failed write or read fails the check instead of crashing the test.

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
…holds it

Under heavy file activity (a build, an antivirus or indexer scan of a file just
written) another program can hold settings.ini or the new temporary file for a
moment. The single MoveFileExW then failed with ERROR_ACCESS_DENIED and the save
was refused, although the state lock was held and nothing else in WrongGPU
touched the file.

- MoveIntoPlace: the rename into place is retried on ERROR_ACCESS_DENIED,
  ERROR_SHARING_VIOLATION and ERROR_LOCK_VIOLATION, with short pauses, for up to
  kMoveRetryMs (2 s). Any other error, or the same one after that time, fails as
  before: the file unchanged, the temporary file deleted, the error reported.
- Used by WriteAtomic, UpdateStateFile (SetSettings) and the guard's completion
  marker.
- Test seam "move-busy": an attempt fails as if the file were held.

Tests: the first 3 replaces refused, the 4th writes the file; refused past the
bound, WriteAtomic fails with error 5 after about 2 s and SetSettings reports
AccessDenied, the file unchanged, no temporary file left; the same with a real
handle opened read-only sharing read (as a scanner does): held for 300 ms the
write succeeds, held past the bound it fails visibly.

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
@phantoslayer21 phantoslayer21 added this to the 0.6.5 milestone Nov 16, 2026
UpdateStateFile checked the file's identity once and then replaced it. With the
retried replace, the program that held the file could write it and let go
during the retries; the next attempt then overwrote its change silently.

MoveIntoPlace takes an optional `unchanged` check, asked before every attempt
after the first; false stops without replacing. UpdateStateFile passes
"absent before, or still the same file" and returns Changed (temporary file
deleted, the other program's bytes kept). WriteAtomic and the guard marker pass
none.

Test: the first replace refused while the holder writes the file: Changed, the
holder's bytes stay, no temporary file left.

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
@phantoslayer21
phantoslayer21 merged commit d7bb4b0 into main Nov 16, 2026
10 checks passed
@phantoslayer21
phantoslayer21 deleted the rootcause/wgstate branch November 16, 2026 15:52
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.

1 participant