joyent/node — Issue #2575: File descriptor improperly closed on OS X?
Author: danfuzz
Created: 2012-01-18 19:14:22+00:00
State: open

Found using Node v0.6.7, on OS X 10.7.2.

I've been playing with the "daemon" module, which includes a handy utility to create a process lockfile. The gist of the function is to open a file, lockf() it, and then leave the file descriptor open forever (until the process gets killed). If another process tries to do the same thing, the lockf() fails.

This all seems to work fine on Linux. OS X, though, it appears that soon after the daemon.lock() method returns, the file descriptor gets closed, which releases the lock and so lets a second process's lockf() attempt succeed, thereby thwarting the intent of the daemon.lock() method. I verified that the file got closed by using lsof, and I verified that it happened after the daemon.lock() method returned by adding a gratuitous usleep() in the daemon.lock() method.

You can find the daemon.lock() code here: https://github.com/indexzero/daemon.node/blob/master/src/daemon.cc

My best guess is that the OS X version has the wrong behavior, hence the title. Thanks for your help!