startup: check that a reused or destroyed save lock frees its lock #1684
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1691"
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 Asimmimx on 2026-11-24T17:12:18Z.
Refs #1149.
Change
src/startup_stubs/file_lock.his the cross-process lock of the game-save stand-in store:FileLock::acquireopens a lock file and takesLockFileEx(orflock) on it with a bounded wait, and the system drops the lock when the handle closes.release()closes the handle, and the destructor callsrelease().Two tests reach the header today:
startup_gamesave_lock_errorschecks the busy/error code mapping, the overflow-free deadline, and the error reporting with real lock files;startup_gamesave_publish_locked/_unlockedrun the store across processes on a generated copy of the header.Neither test checks that a lock object gives its lock back except through an explicit
release()or the end of a child process. Three single-point mutants of those paths left both tests passing:acquirewithout its leadingrelease(): re-acquiring with the same object leaks the earlier lock behind an open handle;Store::read_blobs,delete_blobanddelete_containerrely on it, because they keep aFileLockon the stack and never callrelease();release()that keeps the stale handle value: a secondrelease(), for example an explicit one followed by the destructor, closes whatever the system has since handed out under that value.This PR adds
check_release_pathstotests/startup_gamesave_lock_errors.cpp. Each case takes the lock from a second object with a zero timeout, so it succeeds only if the first object's handle is really closed:acquiredrops its earlier hold firsttimeoutrelease()leaves alone a handle the object no longer ownsNegative control:
startup_gamesave_lock_errors_reacquire_controlbuilds the same check against a generated copy offile_lock.hwithout the leadingrelease()inacquire. This follows thestartup_gamesave_publish_unlockedpattern next to it. CMake makes the copy with a regex that tolerates CRLF and stops withFATAL_ERRORif the anchor is gone. The test usesPASS_REGULAR_EXPRESSION "FAIL: re-acquire"instead ofWILL_FAIL, so it passes only when the check reports this specific defect. A crash would not pass it. The copy exists only in the build tree.The change touches
tests/startup_gamesave_lock_errors.cpp(one new function and one call; no existing case changed) and adds 13 lines toCMakeLists.txtdirectly after thestartup_gamesave_lock_errorsregistration.src/startup_stubs/file_lock.hitself is unchanged.docs/startup-runtime.mddoes not list this check's coverage, so it is not changed.Verification
Commit
a9852263onc864f138(main),devpreset (Debug), Windows 11 Pro build 26300.9974, MSVC 19.51.36260. No GPU is involved.When run directly, the control binary prints
FAIL: re-acquire: taking the same lock again on the same object succeeds ...andFAIL: re-acquire: moving to another file frees the lock the object held before. Four later cases fail too, because the leaked lock carries over, and it exits 1.tests/docs_links_check.py,tests/bidi_controls_check.pyandgit diff --checkpassed.Mutation probe
I applied each mutation to
src/startup_stubs/file_lock.halone. Then I re-rancmake --preset dev(the publish tests compile a configure-time copy of the header), rebuiltstartup_gamesave_lock_errorsandstartup_gamesave_publish_locked, ran both tests and reverted. None of this is committed. "Before" counts as killed if either test failed. These are Windows-branch mutants.acquiredoes notrelease()a lock it already holdsrelease()release()keeps the stale handle valuereturn now)ERROR_SHARING_VIOLATIONnot busyERROR_IO_PENDINGnot busyOPEN_EXISTINGinstead ofOPEN_ALWAYSGetLastErrorGetLastErrorerrorNote on the stale-handle case: whether the third object's open receives the freed handle value is up to Windows (on POSIX the lowest free descriptor is reused, so it is deterministic there). On the correct header the case cannot fail either way, so it never fails falsely. At worst it misses the mutant. It killed the mutant in all three probe runs here.
Not pinned (left as is, on purpose)
last_error_ = 0(survives). After a wait that sees busy and then locks,last_error()is 0 in the code. The comment says "the last failed open or lock attempt; 0 when none failed", which can also be read as "the busy code from earlier in this call". I did not pin either reading.breakonAttempt::error, therelease()before returningerror, "every lock error is busy"). They survive. Reaching them needsLockFileExto fail with a code other than busy on a handle that opened, and I found no device-free way to make that happen.release()before returningtimeout(survives). A timed-out object keeps an unlocked handle open until it is reused or destroyed. No lock behavior can observe that.FILE_SHARE_DELETEdropped andGENERIC_READonly (survive). The store never deletes or writes a lock file, so this behavior is outside the lock contract.last_error_ = 0(the per-attempt reset oropenoverwrite it on every path),</<=at the zero timeout,>/>=at the deadline and saturation tests, and a fixed retry pause, which changes only timing.Checklist
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:23:46Z.
Review: COMMENTED
Review of head
a9852263c2(comment only, not an approval).Verdict: 0 P1 / 0 P2. Test and CMake only; no production change. No test is removed, loosened or skipped, and no expected value comes from the code under test.
Run
g++ -std=c++17 -O1 -Isrc tests/startup_gamesave_lock_errors.cpp -pthread: printsstartup_gamesave_lock_errors: ok, exit 0.cmake -Pand the PR's exact regex (removesrelease();at the start ofacquire): compiles, exits 1 withFAIL: re-acquire,scope,destructoranddouble releaselines, soPASS_REGULAR_EXPRESSIONmatches.x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -Isrcandclang++-19 -std=c++17 -fsyntax-only -Wall -Wextra: clean.~FileLock() { release(); }->~FileLock() {}fails the destructor and double-release checks; removingfd_ = -1;fromrelease()fails "releasing an object twice leaves the lock another object now holds" (the stale-descriptor case, caught only by this new test). Green again after restore.Non-blocking
acquire; destructor and double-release are guarded by the main test alone (which the mutations show can fail).src; it compiles today but would break if the test includes another header from src/.release();/last_error_ = 0;anchor disappears, which is the right failure mode.Not run: the real CMake configure and ctest, Windows LockFileEx behaviour (the Windows branch was only syntax-checked), MSVC. No overlap found with open PRs (#1334 touches the save provider; check only if it edits startup_gamesave_*).
Linux limits: no Windows, MSVC, DXC or D3D12 device here; LLVM 19 only. Anything that needs them was not run.
AI-assisted; commands listed were executed locally.