Repository navigation
add flock API to fs module #122
Description
Activity
Sorry, but that is unlikely to happen for the reasons outlined here and elsewhere. To summarize, it's not possible to implement flock() cross-platform in a consistent manner.
Could you keep this issue open a bit longer please? This is purportedly a community-driven project. Let's give the community a chance to weigh in.
To summarize, it's not possible to implement flock() cross-platform in a consistent manner.
How about: drop support for platforms that don't support a locking mechanism.
Or less radically: have fs.flock throw an error on platforms that do not support it.
I just implemented this in C in Windows and POSIX. It works fine, and that covers every operating system that anyone cares about. This "lowest common denominator" excuse is not satisfying.
Here's rust's flock implementation: https://github.com/rust-lang/rust/blob/master/src/librustdoc/flock.rs
Looks like it works on:
- linux
- freebsd
- dragonflybsd
- osx
- windows
Is this list really too small to bother implementing it at all?
Note that rust uses
fcntl(F_SETLKW), notflock(). From the man page:- If a process closes any file descriptor referring to a file, then all of the process's locks on that file are released, regardless of the file descriptor(s) on which the locks were obtained. This is bad: it means that a process can lose its locks on a file such as /etc/passwd or /etc/mtab when for some reason a library function decides to open, read, and close the same file.
- The threads in a process share locks. In other words, a multithreaded program can't use record locking to ensure that threads don't simultaneously access the same region of a file.
In other words, it's completely unsafe to use in a concurrent program. That's probably why that
not production quality at allcomment is in there.If you want a flock() in your node, see this npm module.
Well, fs-ext's flock does not work on windows: baudehlo/node-fs-ext#30
for anyone reaching this page debugging strange deadlocks with fs-ext:
fs-ext calls flock() using uv's thread pool, which has a default size of 4. That means if a single node process has 4 blocking fs-ext flock calls running, all other uv threadpool based calls (i.e. nearly all fs.* functions) hang.
https://github.com/joyent/node-lockfd solves that using a dedicated thread (not uv threadpool)
Reacted by Paulo Coghi and Adrien
https://www.freebsd.org/cgi/man.cgi?query=flock&sektion=2