Skip to content

Windows orphan file locks #509

Closed

Description

@Leo-Marbehan

I was using filelock 3.18.0 one Windows and the lock files were removed automatically. I updated the package to 3.25.0 and now the lock files are still present after releasing the locks. Is this an intended behavior?

Activity

  1. gaborbernat commented on Mar 9, 2026

    @gaborbernat
    Member

    Can you provide more details? What lock, what config, what exact enviornment?

  2. Leo-Marbehan commented on Mar 9, 2026

    @Leo-Marbehan
    Author

    @gaborbernat I made a simple test repository : https://github.com/Leo-Marbehan/filelock-issue-example.

    I ran the test module on both filelock versions on three OS, you can see the results here.

    I saw that a fix fore UNIX orphan locks was released on #408 a year ago. Lock files were not removed on MacOS and linux but are now. For Windows, it seems that the lock files were removed but are not anymore.

  3. gaborbernat commented on Mar 9, 2026

    @gaborbernat
    Member

    I've identified the root cause of the Windows orphan file locks regression.

    In version 3.18.0, lock files were deleted on release on Windows but NOT on Unix/macOS. In version 3.25.0, the behavior flipped: lock files are now deleted on Unix/macOS but NOT on Windows.

    Two conflicting fixes created this inconsistency:

    PR #408 (Feb 12, 2026) - "Enable removal of UNIX lock files" added Path(self.lock_file).unlink() to Unix _release() method in _unix.py:101, which fixed orphan lock files on Unix/macOS.

    PR #484 (Feb 13, 2026) - "Eliminate lock file race in threaded usage" (commit 3dc2db2) removed Path(self.lock_file).unlink() from Windows _release() method to fix threading races. The commit message explains: "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."

    The fix prioritized correctness in multi-threaded scenarios over cleaning up orphan lock files. Currently in src/filelock/_windows.py:80-84, the _release() method just unlocks and closes the file descriptor without unlinking. This is a design decision: Windows lock files persist on disk, but _acquire() handles existing files via O_CREAT.

    Investigating potential solutions that satisfy both requirements (cleanup + thread safety)...

  4. added a commit that references this issue on Mar 9, 2026
    7f2247d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions