Skip to content

🐛 fix(win): restore best-effort lock file cleanup on release#511

Merged
gaborbernat merged 2 commits intotox-dev:mainfrom
gaborbernat:509
Mar 9, 2026
Merged

🐛 fix(win): restore best-effort lock file cleanup on release#511
gaborbernat merged 2 commits intotox-dev:mainfrom
gaborbernat:509

Conversation

@gaborbernat
Copy link
Copy Markdown
Member

Version 3.25.0 introduced a regression on Windows where lock files were no longer cleaned up after release, leaving orphaned .lock files on disk. 🗑️ Users upgrading from 3.18.0 reported that their single-threaded applications now leave persistent lock files, breaking the expected behavior and diverging from how Unix platforms work (where lock files are cleaned up).

The regression came from PR #484, which removed the unlink() call to fix threading race conditions. While that fix correctly addressed multi-threaded scenarios where Windows cannot delete files with open handles, it sacrificed cleanup for the common single-threaded use case where deletion would succeed.

This fix restores opportunistic cleanup by attempting to unlink the lock file after closing it, with errors suppressed via suppress(OSError). ✨ In single-threaded scenarios, the file deletes successfully and users get the expected behavior. In multi-threaded scenarios where other threads hold handles, the deletion fails silently and the lock file persists, preserving the thread-safety guarantees from PR #484.

The test suite is updated to remove the Windows skip condition from test_lock_file_removed_after_release, as Windows now supports cleanup in typical usage patterns.

Closes #509

In version 3.25.0, PR tox-dev#484 removed lock file cleanup on Windows to fix
threading race conditions where unlink() would fail when other threads
held open handles. This created a regression where single-threaded usage
(the common case) left orphaned lock files, breaking user expectations
and diverging from Unix behavior where lock files are cleaned up.

Restore opportunistic cleanup with suppressed OSError. This provides the
expected behavior for single-threaded scenarios where the file deletes
successfully after close(), while gracefully handling multi-threaded
failures by silently suppressing deletion errors when other threads hold
handles. The suppressed exception preserves thread-safety from PR tox-dev#484
while fixing the orphaned lock file issue for typical usage.
@gaborbernat gaborbernat added the bug label Mar 9, 2026
Added documentation clarifying that Windows lock file cleanup is
best-effort and not guaranteed in multi-threaded scenarios, while Unix
and macOS provide reliable cleanup. This helps users understand the
expected behavior and avoid confusion when lock files persist on Windows.

Documentation updated in:
- Platform-specific details section in concepts.rst
- WindowsFileLock and UnixFileLock class docstrings
@gaborbernat gaborbernat merged commit 7f2247d into tox-dev:main Mar 9, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows orphan file locks

1 participant