🐛 fix(win): eliminate lock file race in threaded usage#484
Merged
gaborbernat merged 1 commit intotox-dev:mainfrom Feb 14, 2026
Merged
Conversation
d7b6ed7 to
28546c7
Compare
On Windows, _release() called unlink() after close(), but between those calls another thread would already hold an open handle via os.open(), preventing deletion (Windows can't delete files with open handles). The suppressed unlink added a syscall that widened the race window for no benefit. Additionally, O_TRUNC in _acquire() mapped to CREATE_ALWAYS which caused EACCES when truncating a file with open handles from other threads, pushing them into the retry loop at the os.open level. Remove unlink() from _release() — the lock file persists on disk but _acquire() already handles existing files via O_CREAT. Remove O_TRUNC from _acquire() — lock file content is irrelevant, only the msvcrt byte-range lock matters. This lets the threaded tests run at full Unix-equivalent intensity on Windows.
28546c7 to
3dc2db2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Windows threaded lock tests flake on CI because
_release()callsunlink()afterclose(), but between those two calls another thread has already acquired an open handle viaos.open(). Since CPython's_wopendoesn't setFILE_SHARE_DELETE, Windows refuses the deletion — and the suppressedunlink()just widens the race window for no benefit. On top of that,O_TRUNCin_acquire()maps toCREATE_ALWAYSinCreateFileW, which causesEACCESwhen truncating a file that other threads hold open, pushing contending threads into the retry loop at theos.openlevel rather than just atmsvcrt.locking.The fix removes
unlink()from_release()entirely — the lock file persists on disk, but_acquire()already handles existing files viaO_CREAT, and the base class documents that lock files are not automatically deleted. It also dropsO_TRUNCfrom_acquire(), since lock file content is irrelevant; only themsvcrt.lockingbyte-range lock provides mutual exclusion. WithoutO_TRUNC,O_RDWR | O_CREATmaps toOPEN_ALWAYS, which opens without truncation and avoids the write-sharing conflict.With both race conditions eliminated, the threaded tests no longer need reduced iteration counts on Windows and now run at full Unix-equivalent intensity (100×100 and 10×1000).