Skip to content

Deadlock when a process tries to open and lock on the same file twice #315

Open
@youkaichao

Description

@youkaichao

Minimal reproducible example:

import os
from filelock import Timeout, FileLock

file_path = "high_ground.txt"
lock_path = "high_ground.txt.lock"

lock1 = FileLock(lock_path)

with lock1:
    lock2 = FileLock(lock_path)
    with lock2:
        print("I have the high ground!")

This is dangerous, and even when we just create one filelock, we don't know if any other code already created the filelock in the same process and we are within the filelock.

A possible solution would be to check and get existing fd on a file in UnixFileLock._acquire function. In Linux, this is easy because Python fd is just integer, and all fds are checkable in /proc/{process_id}/fd/ .

cc @gaborbernat

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions