Skip to content

Unref FSWatcher #33096

Description

@szmarczak

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 🤦‍♂️

Activity

  1. juanarbol commented on Apr 27, 2020

    @juanarbol
    Member

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

  2. added
    feature requestIssues requesting new Node.js features.
    fsIssues and PRs related to file-system APIs and the fs module.
    on Apr 27, 2020
  3. addaleax commented on Apr 27, 2020

    @addaleax
    Member

    @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.

  4. rickyes commented on Apr 27, 2020

    @rickyes
    Contributor

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

  5. addaleax commented on Apr 27, 2020

    @addaleax
    Member

    @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’s 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().

  6. rickyes commented on Apr 28, 2020

    @rickyes
    Contributor

    @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.

  7. szmarczak commented on Apr 28, 2020

    @szmarczak
    MemberAuthor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions