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

Strange behavior with fs.watch on file with JSON stringified -> first update isn't emitted if too fast after fs.watch #53111

Open
2 tasks done
Apokalypt opened this issue May 21, 2024 · 13 comments
Labels
confirmed-bug Issues with confirmed bugs. fs Issues and PRs related to the fs subsystem / file system. macos Issues and PRs related to the macOS platform / OSX. repro-exists Issues with reproductions.

Comments

@Apokalypt
Copy link

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 of fs.watch. The strange thing is that if the content is only a text (without JSON format) the watcher emit the event correctly...

Minimal Reproduction

const fs = require('fs');
const path = require('path');

const filePath = path.join(__dirname, 'test.json');
// Create the file if it doesn't exist
if (!fs.existsSync(filePath)) {
    fs.writeFileSync(filePath, "{ }");
}

fs.watch(filePath, (eventType, filename) => {
    console.log(`Event type is: ${eventType}`);
    if (filename) {
        console.log(`Filename provided: ${filename}`);
    } else {
        console.log('Filename not provided');
    }
});

fs.writeFileSync(filePath, "{ \"test\": \"test\", \"test2\": \"test2\" }"); // same with JSON.stringify(...)
// Listener above is called if we replace line above with:
// fs.writeFileSync(filePath, "very ok or not but I need a longer text to test something...");

Output

Expected

Event type is: change
Filename provided: test.json

Received

Nothing

Before You Submit

  • I have looked for issues that already exist before submitting this
  • My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask
@gireeshpunathil
Copy link
Member

interesting!

able to reproduce with v20.12.2 too.

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.

@Apokalypt
Copy link
Author

Oh, I forgot to tell that I've also tested the behaviour on node version :

  • 20.11.0
  • 16.20.1

@gireeshpunathil Are you also on a MacOS computer or on another operating system ?

@gireeshpunathil
Copy link
Member

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.

@preveen-stack preveen-stack added the macos Issues and PRs related to the macOS platform / OSX. label May 22, 2024
@Apokalypt
Copy link
Author

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

@RedYetiDev
Copy link
Member

@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

@gireeshpunathil
Copy link
Member

I deem it as a bug. the watch method does not talk about when it will be ready so the implicit assumption is as soon the call returns. it is also not specified / documented if it is an asynchronous API in which case it's last param call back could have been used as a completion indicator, but that is used for something else (the actual file change event handler). so a natural expectation is that the watch is in effect as soon as the call returns, but reported problem indicates that a small delay is required for the watch to function well - in darwin.

/cc @nodejs/platform-macos

@gireeshpunathil gireeshpunathil added the confirmed-bug Issues with confirmed bugs. label May 23, 2024
@jakecastelli
Copy link
Contributor

jakecastelli commented May 29, 2024

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?

@Apokalypt
Copy link
Author

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

@RedYetiDev RedYetiDev added fs Issues and PRs related to the fs subsystem / file system. and removed confirmed-bug Issues with confirmed bugs. labels May 29, 2024
@RedYetiDev
Copy link
Member

RedYetiDev commented May 29, 2024

Added repro-exists label: #53111 (comment)


CC @nodejs/fs - FS Related Issue
CC @nodejs/libuv - Possibly a libuv issue
CC @MoLow - Involved with a possibly related issue: #53100


(Ignore my changes to confirmed-bug, I didn't realize it was added after switching to this repository)

@RedYetiDev RedYetiDev added repro-exists Issues with reproductions. confirmed-bug Issues with confirmed bugs. labels May 29, 2024
@saghul
Copy link
Member

saghul commented May 29, 2024

interesting!

able to reproduce with v20.12.2 too.

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.

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.

@Apokalypt
Copy link
Author

Apokalypt commented May 29, 2024

interesting!
able to reproduce with v20.12.2 too.
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.

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 :

  • text smaller (only 1 character)
  • text of equal length
  • huge text (~50 MB)

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

@saghul
Copy link
Member

saghul commented May 29, 2024

Now that is surprising!

@Shahindavoodicom
Copy link

tested this on linux and works ok

node version v21.7.1

uname -a
6.5.0-35-generic #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 7 09:00:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. fs Issues and PRs related to the fs subsystem / file system. macos Issues and PRs related to the macOS platform / OSX. repro-exists Issues with reproductions.
Projects
None yet
Development

No branches or pull requests

8 participants
@saghul @gireeshpunathil @preveen-stack @RedYetiDev @jakecastelli @Apokalypt @Shahindavoodicom and others