Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flock API to fs module #122

Closed
andrewrk opened this issue Dec 9, 2014 · 6 comments
Closed

add flock API to fs module #122

andrewrk opened this issue Dec 9, 2014 · 6 comments

Comments

@andrewrk
Copy link

andrewrk commented Dec 9, 2014

https://www.freebsd.org/cgi/man.cgi?query=flock&sektion=2

@bnoordhuis
Copy link
Member

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.

@andrewrk
Copy link
Author

andrewrk commented Dec 9, 2014

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.

@andrewrk
Copy link
Author

andrewrk commented Dec 9, 2014

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?

@bnoordhuis
Copy link
Member

Note that rust uses fcntl(F_SETLKW), not flock(). 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 all comment is in there.

If you want a flock() in your node, see this npm module.

@bajtos
Copy link
Contributor

bajtos commented Dec 15, 2014

Well, fs-ext's flock does not work on windows: baudehlo/node-fs-ext#30

@mo22
Copy link

mo22 commented Oct 10, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants