fs.watch() doesn't trigger changes for /proc files on linux #7244

Closed
substack opened this Issue Mar 4, 2014 · 2 comments

Projects

None yet

3 participants

@substack
substack commented Mar 4, 2014

On linux you can get the lid state on a laptop by reading /proc/acpi/button/lid/LID/state:

$ cat /proc/acpi/button/lid/LID/state
state:      open

However, 'change' events do not fire for this kind of file (and possibly for all files in /proc?)

var fs = require('fs');
var lidfile = '/proc/acpi/button/lid/LID/state';
var w = fs.watch(lidfile);
w.on('change', onchange);
onchange();

function onchange () {
    fs.readFile(lidfile, 'utf8', function (err, src) {
        console.log(src.trim());
    });
}
$ node lid.js 
state:      open

However, if I add:

setInterval(onchange, 1000)

then it's clear that the file is actually changing:

$ node lid.js 
state:      open
state:      closed
state:      closed
state:      open
state:      closed
state:      open
^C
@TooTallNate

Try fs.watchFile() instead maybe, which uses a more rudimentary "polling" mechanism.

fs.watch() uses the OS's underlying file watching APIs, which appear to always be riddled with weird bugs and inconsistencies. There's likely nothing that can be done about the fs.watch() case unfortunately...

@tjfontaine

There's not much node can do for you here, to quote http://inotify.aiken.cz/?section=inotify&page=faq

Q: Can I watch sysfs (procfs, nfs...)?
Simply spoken: yes, but with some limitations. These limitations vary between kernel versions and tend to get smaller. Please read information about particular filesystems.

If you want a reliable notification mechanism here for a given device I would seek an alternate solution like the stat or poll mechanism, as I'm not sure node is ever going to be in a position to fill in these gaps, especially for pseudo file systems like procfs or sysfs

@tjfontaine tjfontaine closed this Mar 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment