npm / lockfile

A very polite lock file utility, which endeavors to not litter, and to wait patiently for others.
ISC License
260 stars 33 forks source link

[question] What about locking the file until exit? #16

Closed fritx closed 9 years ago

fritx commented 9 years ago

So that when the node process quits, the locked file will be removed.

isaacs commented 9 years ago

This module already makes a best-effort attempt to unlock files on process exit.

https://github.com/npm/lockfile/blob/master/lockfile.js#L33-L37

fritx commented 9 years ago

Thanks for reply, but when I taskkill the process, the unlock didnt work. Maybe I misunderstood the use case of file lock?

I want to lock a user file after user login, and make sure the lock removed when the same process quit (including taskkill, etc)

isaacs commented 9 years ago

The process.on('exit') event doesn't fire if the process is killed by a unix signal.

You can use the module signal-exit, and add this:

var signalExit = require('signal-exit')
signalExit(function (code, signal) {
  if (signal)
    process.emit('exit')
})

var lockfile = require('lockfile')
// do whatever with lock files

Doing this requires that the program add os-level listeners to every possible fatal signal, and do some other hoop-jumping to make sure it doesn't accidentally prevent the exit when the signal should be fatal. That's what the signal-exit module does, but it would be excessive to do that in this module.

fritx commented 9 years ago

Thanks! But I guess some of these signals are inavailable in Windows, :/ as well as killing the process from task monitor?