Repository navigation
Unref FSWatcher #33096
Description
Activity
I found fs.unwatchFile(filename[, listener]), or you mean something different?
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.fsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.
on Apr 27, 2020 @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.Reacted by Juan José, Szymon Marczak and Alex YangI'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 theStatWatcherandFSWatcherclasses inlib/internal/fs/watchers.js.StatWatcheris forfs.watchFile(),FSWatcheris forfs.watch().For the
FSWatchercase, the C++FSEventWrapclass needs to be fixed up a bit:- On the JS side, the handle currently only inherits from
AsyncWrap, but it should inherit fromHandleWrap. This can be fixed by replacingAsyncWrap::GetConstructorTemplatewithHandleWrap::GetConstructorTemplate. - In that case, the extra
close()method can also be removed, it’s no longer necessary.
For the
StatWatchercase, this is tricky to get right, because Node.js currently de-duplicatesStatWatchers. If the same file is watched twice, the same watcher is returned, so calling.unref()once on theStatWatchershould not actually unref the handle. You probably need to add some kind of reference count there, or start returning distinct values from.watchFile().Reacted by Szymon Marczak, ricky, Alex Yang and Kirill GroshkovReacted by Alex Yang and Kirill GroshkovReacted by ricky- On the JS side, the handle currently only inherits from
@szmarczak I looked at the implementation logic and maybe
fs.watchFile(path, { persistent: false }, callback)will solve your problem. However, we can still addref,unreffunctions toStatWatcherandFSWatcherclasses.Reacted by Szymon Marczak and Kirill GroshkovIndeed, thanks. I'd still go for
.ref()&.unref()to be consistent with other Node.js API.- added a commit that references this issue
on May 11, 2020 - added a commit that references this issue
on Nov 3, 2020 - added a commit that references this issue
on Nov 16, 2020 - added a commit that references this issue
on May 5, 2024 - added a commit that references this issue
on Jul 27, 2026
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
Edit: seems like the code above is completely unnecessary 🤦♂️