Skip to content

add flock API to fs module #122

Description

@andrewrk

Activity

  1. bnoordhuis commented on Dec 9, 2014

    @bnoordhuis
    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.

  2. andrewrk commented on Dec 9, 2014

    @andrewrk
    Author

    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.

  3. andrewrk commented on Dec 9, 2014

    @andrewrk
    Author

    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?

  4. bnoordhuis commented on Dec 9, 2014

    @bnoordhuis
    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.

  5. bajtos commented on Dec 15, 2014

    @bajtos
    Contributor

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

  6. mo22 commented on Oct 10, 2021

    @mo22

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions