-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Strange behavior with fs.watch
on file with JSON stringified -> first update isn't emitted if too fast after fs.watch
#53111
Comments
interesting! able to reproduce with if I replace the writing section with this code: if(process.argv[2] === 'json') {
console.log('json')
fs.writeFileSync(filePath, "{ \"test\": \"test\"}");
}
else {
console.log('text')
fs.writeFileSync(filePath, "hello!");
} then the behaviour is arbitrary. sometimes the text change is called back, sometimes not. but the json callback is always omitted. |
Oh, I forgot to tell that I've also tested the behaviour on node version :
@gireeshpunathil Are you also on a MacOS computer or on another operating system ? |
yes - Mac . if I add an artificial delay using setTimeout before the write, things are fine; so looks like there is a race condition between the watch registration and the writing (though both are done by the same thread). not sure what is going on. |
@gireeshpunathil Do you think this could be considered as a bug and should be reported in the main repository containing the nodejs runtime source code? |
Maybe, it seems like a bug IMO |
I deem it as a bug. the /cc @nodejs/platform-macos |
I dug a bit on this on and I think first of all this is a macOS related issue as it cannot reproduce on Linux. I think this its related to libuv as Moshe said in #53100 @Apokalypt do you consider to open an issue in libuv? |
I confess I hadn't thought of that. However, if the problem is on their side (which seems to be admitted by you 3), I'll take the time to make a ticket at libuv tomorrow |
Added CC @nodejs/fs - FS Related Issue (Ignore my changes to |
Have you tried using some text of equal length? I don't see how JSON affects anything since there is no processing of the data, but its length might affect things with regards to timing. |
(I'm not the one targeted by your answer, but I'm going to answer anyway since I did the tests before creating the problem) When I've found out the bug, I tried with :
For every test, the result is the same -> the .watch callback is called right after the update while with JSON data (small or not) it's never received... |
Now that is surprising! |
tested this on linux and works ok node version v21.7.1 uname -a |
Node.js Version
v18.17.1
NPM Version
9.6.7
Operating System
23.2.0 Darwin
Subsystem
fs
Description
I would like to watch changes on a file storing JSON stringified data but the watcher created with
fs.watch
is not emitted if the change happen to quickly after the call offs.watch
. The strange thing is that if the content is only a text (without JSON format) the watcher emit the event correctly...Minimal Reproduction
Output
Expected
Received
Nothing
Before You Submit
The text was updated successfully, but these errors were encountered: