Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unref FSWatcher #33096

Closed
szmarczak opened this issue Apr 27, 2020 · 6 comments
Closed

Unref FSWatcher #33096

szmarczak opened this issue Apr 27, 2020 · 6 comments
Labels
feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system.

Comments

@szmarczak
Copy link
Member

szmarczak commented Apr 27, 2020

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

@juanarbol
Copy link
Member

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

@addaleax addaleax added feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system. labels Apr 27, 2020
@addaleax
Copy link
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.

@rickyes
Copy link
Contributor

rickyes commented Apr 27, 2020

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

@addaleax
Copy link
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().

@rickyes
Copy link
Contributor

rickyes commented Apr 28, 2020

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

@szmarczak
Copy link
Member Author

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

codebytere pushed a commit that referenced this issue May 11, 2020
PR-URL: #33134
Fixes: #33096
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this issue Nov 3, 2020
Backport-PR-URL: #35555
PR-URL: #33134
Fixes: #33096
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this issue Nov 16, 2020
Backport-PR-URL: #35555
PR-URL: #33134
Fixes: #33096
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
abhishekumar-tyagi pushed a commit to abhishekumar-tyagi/node that referenced this issue May 5, 2024
PR-URL: nodejs/node#33134
Fixes: nodejs/node#33096
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants