Node: Unref FSWatcher

Created on 27 Apr 2020  路  6Comments  路  Source: nodejs/node

Is your feature request related to a problem? Please describe.

Currently, when you do fs.watchFile(...) the process will run forever.

Describe the solution you'd like

A possibility to watcher.unref() would be great.

Describe alternatives you've considered

const watchFile = (path, callback, onError) => {
    let previousTime = null;

    const interval = setInterval(async () => {
        try {
            const {mtimeMs} = await stat(path);

            if (previousTime !== null && mtimeMs !== previousTime) {
                callback(mtimeMs, previousTime);
            }

            previousTime = mtimeMs;
        } catch (error) {
            clearInterval(interval);

            // The error part is off the Node.js API
            onError(error);
        }
    }, 1000 * 60).unref();
};

Edit: seems like the code above is completely unnecessary :man_facepalming:

feature request fs

Most helpful comment

@rickyes Sure!

Basically, this would require adding .ref() and .unref() methods to the StatWatcher and FSWatcher classes in lib/internal/fs/watchers.js. StatWatcher is for fs.watchFile(), FSWatcher is for fs.watch().

For the FSWatcher case, the C++ FSEventWrap class needs to be fixed up a bit:

  • On the JS side, the handle currently only inherits from AsyncWrap, but it should inherit from HandleWrap. This can be fixed by replacing AsyncWrap::GetConstructorTemplate with HandleWrap::GetConstructorTemplate.
  • In that case, the extra close() method can also be removed, it鈥檚 no longer necessary.

For the StatWatcher case, this is tricky to get right, because Node.js currently de-duplicates StatWatchers. If the same file is watched twice, the same watcher is returned, so calling .unref() once on the StatWatcher should not actually unref the handle. You probably need to add some kind of reference count there, or start returning distinct values from .watchFile().

All 6 comments

I found fs.unwatchFile(filename[, listener]), or you mean something different?

@juanarbol fs.unwatchFile() removes the listener entirely, what this issue is asking for is a .unref() method that keeps the listener in place but stops is from keeping the process alive.

I'm more interested in this part, can you let me try work it ? @addaleax

@rickyes Sure!

Basically, this would require adding .ref() and .unref() methods to the StatWatcher and FSWatcher classes in lib/internal/fs/watchers.js. StatWatcher is for fs.watchFile(), FSWatcher is for fs.watch().

For the FSWatcher case, the C++ FSEventWrap class needs to be fixed up a bit:

  • On the JS side, the handle currently only inherits from AsyncWrap, but it should inherit from HandleWrap. This can be fixed by replacing AsyncWrap::GetConstructorTemplate with HandleWrap::GetConstructorTemplate.
  • In that case, the extra close() method can also be removed, it鈥檚 no longer necessary.

For the StatWatcher case, this is tricky to get right, because Node.js currently de-duplicates StatWatchers. If the same file is watched twice, the same watcher is returned, so calling .unref() once on the StatWatcher should not actually unref the handle. You probably need to add some kind of reference count there, or start returning distinct values from .watchFile().

@szmarczak I looked at the implementation logic and maybe fs.watchFile(path, { persistent: false }, callback) will solve your problem. However, we can still add ref, unref functions to StatWatcher and FSWatcher classes.

Indeed, thanks. I'd still go for .ref() & .unref() to be consistent with other Node.js API.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Brekmister picture Brekmister  路  3Comments

addaleax picture addaleax  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments

cong88 picture cong88  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments