{
  "id": 6087423,
  "title": "Cross-platform file locking in Python — fcntl vs msvcrt from scratch",
  "url": "https://urgent.news/2026/09/07/cross-platform-file-locking-in-python-fcntl-vs-msvcrt-from-scratch",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-07T02:41:31.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/susumun/cross-platform-file-locking-in-python-fcntl-vs-msvcrt-from-scratch-19c5"
  },
  "original_language": "en",
  "account": "When two processes attempt to write to the same configuration file simultaneously, there is a risk that one write operation may overwrite the other. This can lead to file corruption or loss of data. File locking is a solution to this problem, ensuring that only one process can modify the file at a time. However, implementing file locking in pure Python across different operating systems is challenging because Unix-like systems and Windows use distinct APIs for this purpose.\n\nThe article explains how to implement inter-process locking using only the standard library's fcntl module for Unix-like systems and the msvcrt module for Windows. On Unix-like systems, the fcntl.flock() function is used to create an advisory lock on a file. It is important to note that fcntl.flock() is an advisory lock, meaning that the operating system kernel does not force a process to follow the lock protocol if it chooses not to. The lock function can be called with the LOCK_EX flag for exclusive access or with LOCK_NB for non-blocking behavior, which returns immediately if the lock cannot be acquired.\n\nFor Windows, the equivalent functionality is provided by the msvcrt.locking() function. This function locks a specified range of bytes within a file, rather than the entire file as fcntl.flock() does. To achieve a similar effect, the byte range can be set to just one byte, which is sufficient for locking a sidecar file dedicated to locking purposes. The main difference is that msvcrt.locking() requires explicitly specifying the byte range to lock.\n\nA common approach to implementing cross-platform file locking involves creating a sidecar file (e.g., sites.json.lock) and using this file for locking instead of the actual data file. This strategy avoids the pitfalls associated with locking the data file itself on Windows, where opening the file in an exclusive mode can prevent other processes from performing basic operations such as reads or opens. Instead, the locking mechanism works by creating and deleting a sidecar file, which is separate from the actual data file. This separation prevents platform-specific quirks from interfering with the data I/O operations.\n\nTo prevent race conditions when creating the sidecar file, the article suggests using the O_CREAT | O_EXCL flags with os.open(). This combination ensures that the file is created only if it does not already exist, as the operation is atomic at the operating system level. If the sidecar file already exists, it indicates that another process has already acquired the lock, and the lock acquisition fails.\n\nHowever, creating a sidecar file for locking has its own challenges. If two processes each attempt to create the sidecar file using the O_CREAT | O_EXCL method, one process may create the file after checking its existence, leading to a situation where the lock is not acquired by any process. To mitigate this risk, the article proposes checking the modification time of the sidecar file. If the file's modification time is older than a certain threshold (e.g., 30 minutes), it is treated as stale, and the lock is attempted again using the O_CREAT | O_EXCL method. This approach helps to deal with stale locks resulting from crashed processes.",
  "summary": "What happens when a GUI app and a separate background process both try to write to the same configuration file at the same time? If the timing is bad, one write clobbers the other, and in the worst case the file ends up corrupted. File locking is the standard answer to this \"concurrent writes from multiple processes\" problem. Trying to implement it in pure Python across both Unix-like systems and…",
  "key_points": [
    "fcntl.flock() creates advisory lock on file for Unix-like systems",
    "msvcrt.locking() provides equivalent functionality for Windows",
    "Sidecar file approach avoids platform-specific quirks in locking"
  ],
  "editors_take": "Using a sidecar file for cross-platform locking resolves issues with data file locking on Windows, but introduces new challenges like handling stale locks and preventing race conditions during sidecar file creation.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}