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

fs.watch failed with adding a UNIX Domain Sock file under macOS #38014

Open
XadillaX opened this issue Apr 1, 2021 · 6 comments
Open

fs.watch failed with adding a UNIX Domain Sock file under macOS #38014

XadillaX opened this issue Apr 1, 2021 · 6 comments
Labels
fs Issues and PRs related to the fs subsystem / file system. macos Issues and PRs related to the macOS platform / OSX.

Comments

@XadillaX
Copy link
Contributor

XadillaX commented Apr 1, 2021

  • Version: 14.15.4
  • Platform: macOS
  • Subsystem: darwin

What steps will reproduce the bug?

How often does it reproduce? Is there a required condition?

/tmp/temp.js

'use strict';

const fs = require('fs');
fs.watch('/tmp', (eventType, filename) => {
  console.log(`event type is: ${eventType}`);
  if (filename) {
    console.log(`filename provided: ${filename}`);
  } else {
    console.log('filename not provided');
  }
});

/tmp/tempp.js

'use strict';

const net = require('net');

net.createServer(() => {}).listen(`/tmp/ttt-${Date.now()}`);

What is the expected behavior?

temp.js should have output, either filename provided: ... or filename not provided.

What do you see instead?

Nothing outputed.

Additional information

If a UNIX Domain Sock file is removed, the event outputted. But add occurs no event.

btw, Linux (Ubuntu) is OK.

@XadillaX XadillaX added fs Issues and PRs related to the fs subsystem / file system. macos Issues and PRs related to the macOS platform / OSX. labels Apr 1, 2021
@schamberg97
Copy link
Contributor

schamberg97 commented May 3, 2021

It appears as if Apple macOS-specific APIs treats sock files differently. For instance, if I try to create a sock file using Python like this:

python -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.bind('/tmp/test.sock')"

the temp.js will also produce no result under macOS, but will produce a successful result under Ubuntu. Similarly, Finder under macOS will never see sock files, even if enabled to see "hidden" files and they can only be seen through the ls command (even though these are different APIs). Most likely, the issue lies somewhere with Apple-specific APIs and their treatment of sock files. Unfortunately, I have no specific knowledge of how these APIs operate

@schamberg97
Copy link
Contributor

schamberg97 commented May 3, 2021

Looks like I may have accidentally deleted my last comment here.

Apple's FSEventStreamCreate doesn't appear to allow watching for UNIX Domain Sockets (maybe due to a bug). This can also be demonstrated by using fswatch utility on MacOS with different file system event monitor APIs:

  • fswatch -m fsevents_monitor /tmp
  • fswatch -m kqueue_monitor /tmp
  • fswatch -m poll_monitor /tmp

Only Apple's FSEvents will not notice UNIX Domain Sockets being created (but it will notice them being deleted), with kqueue and poll seeing changes without any issue. Theoretically, one could use kqueue inherited from FreeBSD to solve this issue. Apple even suggests that there are cases, when kqueue should be used instead of FSEvents API, but is rather vague about it. However, there is a bit of an issue: it requires an open file descriptor for every file being watched. This will become a huge problem, once the open file descriptor limit is reached.

How I see this problem could be partially solved:

  1. Node.js and libuv will allow to choose file system events API amongst those available on the system
  2. On MacOS FSEvents and kqueue will be available (poll is rather inefficient), available sane options on other platforms
  3. Documentation update to account for 1-2

@schamberg97
Copy link
Contributor

To simplify things a bit, kqueue is good when there are few files or we need to monitor a single file. However, when there are a lot of files, FSEvents is a better friend. For this reason, an obviously easier option of totally switching to kqueue is IMHO much worse than what I proposed in a previous comment.

Speaking of this, I think this conversation needs to involve libuv guys

@XadillaX
Copy link
Contributor Author

XadillaX commented May 5, 2021

/cc @nodejs/libuv

@schamberg97
Copy link
Contributor

As soon as there is any further word on the issue, I may try to draft the PR solving the issue, as I've already had experience solving libuv-related bug

@XadillaX
Copy link
Contributor Author

XadillaX commented May 5, 2021

As soon as there is any further word on the issue, I may try to draft the PR solving the issue, as I've already had experience solving libuv-related bug

Exactly, I think it's a macOS-related bug.

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

No branches or pull requests

2 participants