-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Node.js fs.open() hangs after trying to open more than 4 named pipes (FIFOs) #23220
Comments
Dealing with FIFOs is currently a bit tricky. The The solution is to open the file in non-blocking mode, but that has the difficulty that the other So, the solution would look something like this: fs.open('path/to/fifo/', fs.constants.O_RDONLY | fs.constants.O_NONBLOCK, (err, fd) => {
// Handle err
const pipe = new net.Socket({ fd });
// Now `pipe` is a stream that can be used for reading from the FIFO.
}); |
Perhaps this could be included in the |
@addaleax Thank you, that seems to do the trick. Out of curiosity, in that case Node creates one thread per socket? |
@sami-sweng No – Node.js doesn’t use threading for any I/O other than file access, because it’s hard to implement async I/O for that in a cross-platform fashion. Everything else is handled through an event-based OS mechanism. |
@addaleax: Thanks for this solution! I should just mention that there is at least one behavioural difference compared to pure fs read-streams. It seems an EOF is not properly signalled through the socket, so we won't know when the stream on the other end of the fifo closes. For my particular use-case, I happen to have another reliable hook from which I can call (I am vaguely aware that EOF is not actually a character being sent through the stream, but I don't know enough to answer my own question.) |
@mhelvens Can you share an example? If I use the above script, add |
Hm... At the time it seemed obvious that the socket was to blame, but with you saying that, it becomes a lot more likely that I simply messed up somewhere. I can't share the code itself, but I'll try to reduce it to a simple example. Thanks! |
@addaleax I'm using the same example you are, apparently getting different results. Using Node 10.6.0 on macOS High Sierra. Could it be a Linux vs Mac thing? |
@addaleax poke |
Why 4? Because there are 4 threads in the thread pool? @addaleax Isn't a fifo a file too from the os perspective? |
By default, yes.
Because the behavior when there's no one on the other end is underspec'd. On some platforms it works, on others you'll get an error or it simply hangs. The behavior need not even be consistent on the same platform. 2.6.x linux kernels behave differently from 4.x and 5.x kernels, for example. |
@bnoordhuis Thanks for the clarification. In case anyone needs to write to more than 4 pipes simultaneously without blocking the thread pool here is a workaround: // Read write flag is required even if you only need to write because otherwise you get ENXIO https://linux.die.net/man/4/fifo
// Non blocking flag is required to avoid blocking threads in the thread pool
const fileHandle = await fs.promises.open(fifoPath, fs.constants.O_RDWR | fs.constants.O_NONBLOCK);
// readable: false avoids buffering reads from the pipe in memory
const fifoStream = new net.Socket({ fd: fileHandle.fd, readable: false });
// Write to fifo
const shouldContinue = fifoStream.write(buffer);
// Backpressure if buffer is full
if (!shouldContinue) {
await once(fifoStream, 'drain');
}
// Be aware that if you close without waiting for drain you will have errors on next write from the Socket class
await fileHandle.close(); I noticed that there is a bug in the NodeJS garbage collector when using |
I have a node.js process that needs to read from multiple named pipes fed by different other processes as an IPC method.
I realized after opening and creating read streams from more than four fifos, that fs seems to no longer be able to open fifos and just hangs there.
It seems that this number is a bit low, considering that it is possible to open thousands of files concurrently without trouble (for instance by replacing
mkfifo
bytouch
in the following script).I tested with node.js v10.1.0 on MacOS 10.13 and with node.js v8.9.3 on Ubuntu 16.04 with the same result.
The faulty script
And a script that displays this behavior:
This script doesn't clean behind him, don't forget to
rm -r /tmp/tmpfifo
Repl.it link
NOTE, The following part of this questions is related to what I already tried to answer the question but might not be central to it
Two interesting facts with this script
echo hello > fifo
) Node is then able to open one more fifo, but no longer receives from the one in which we wroteDebug informations
I then tried to verify whether that could be related to some OS limit, for instance the number of file descriptor open.
Ouput of
ulimit -a
on the Mac isNothing points to some limit at 4.
C++ tentative
I then tried to write a similar script in C++.
In C++ the script successfully open a hundred fifos.
Note that there are a few differences between the two implementations. In the C++ one,
As a side note, the fifos need to be created before executing the script, for instance with
for i in $(seq 0 100); do mkfifo /tmp/tmpfifo/$i; done
Potential Node.js related issue
After a bit of search, it also seems to be linked to that issue on the Node.js Github:
#1941.
But people seems to be complaining of the opposite behavior (fs.open() throwing EMFILE errors and not hanging silently...)
As you can see I tried to search in many directions and all of this lead me to my question:
Do you know what could cause this behavior?
Thank you
The text was updated successfully, but these errors were encountered: