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

It doesn't work in Windows, or I don't understand it's logic #115

Open
jsonslim opened this issue May 9, 2023 · 1 comment
Open

It doesn't work in Windows, or I don't understand it's logic #115

jsonslim opened this issue May 9, 2023 · 1 comment

Comments

@jsonslim
Copy link

jsonslim commented May 9, 2023

I'm able to write to a file despite the library is working. Here is my "locking" code:


const file = './file.txt';

function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

// === main logic ===
lockfile.lock(file)
.then(async (release) => {

    await delay(10000);

    return release();
})
.catch((e) => {
    console.error(e)
});

It locks the file.txt for 10 seconds, and during this time I shouldn't be able to write the file from other processes(if I understand it correctly), but I can open the file in windows "notepad" and change the text.

Here is the code I run in parallel, and it also does rewrite the file.txt :


(() => {
    try {
        const fd = fs.openSync('./file.txt', 'a+');
        fs.writeFileSync(fd, 'some nasty text')
    } catch (e) {
        console.error(e);
    }
})();

So, what does the lib do?
Thanks!

@agustin-golmar
Copy link

Hi @SpacewormSera!

I was searching for a solution of AWS EFS advisory locking mechanism inside Node.js and just find this library.

As I understand the mechanics of the library, every thread, process, application or machine must access the shared file-system with this library, because it uses mkdir and mtime system functions to provide the locking behaviour. These guarantee atomicity of operations. If you lock a file with proper-lockfile, but then access the same file directly (even using Node.js), then you are not using the locking behaviour, you're just "jumping over it". This is because it works like an advisory lock in PostgreSQL, that is, it works if every party respects the mechanism, but is not enforced by the system.

See Understanding Node.js file locking (A. Evans, 2022).

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

2 participants