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

FIFO stream is automatically closed when receiving data on Linux (and not on macOS) #45210

Closed
Bacto opened this issue Oct 27, 2022 · 2 comments
Closed

Comments

@Bacto
Copy link

Bacto commented Oct 27, 2022

Version

16 & 18 at least

Platform

Linux and macOS

Subsystem

No response

What steps will reproduce the bug?

Hi,

I'm trying to read from a FIFO file using Node.js but I get 2 different behaviors on macOS and Linux.

On macOS, everything seems working as expected.
On Linux, when a chunk of data is received the FIFO stream is automatically closed.

Here is the code example:

const fs = require('fs');
const net = require('net');
const child_process = require('child_process');

const filename = 'test';


// Clean and create FIFO
fs.rmSync(filename, { force: true });
child_process.execSync(`mkfifo -m 622 ${filename}`);


// Open FIFO
// See https://github.com/nodejs/node/issues/23220#issuecomment-426345872
const fd = fs.openSync(filename, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
const fifoStream = new net.Socket({ fd });


// Handle FIFO events
fifoStream.on('data', data => console.log('Data received in FIFO stream:', data.toString()));
fifoStream.on('close', () => console.log('FIFO stream closed'));


// Send data to FIFO
setInterval(
  () => {
    console.log('Writing data "Test" to FIFO');
    fs.writeFileSync(filename, 'Test');
  },
  1000
);

On macOS (M1, Node.js v16.13.2, v16.17.1 and v18.12.0), the FIFO stream stays opened and continues to show data received:

$ node test.js
Writing data "Test" to FIFO
Data received in FIFO stream: Test
Writing data "Test" to FIFO
Data received in FIFO stream: Test
Writing data "Test" to FIFO
Data received in FIFO stream: Test
Writing data "Test" to FIFO
Data received in FIFO stream: Test
Writing data "Test" to FIFO
Data received in FIFO stream: Test
Writing data "Test" to FIFO
Data received in FIFO stream: Test

On Linux (Ubuntu 22.10 with Node.js v18.7.0 and Alpine Linux 3.16.2 with Node.js v16.17.1), the stream close on first chunk of data received:

# node test.js
Writing data "Test" to FIFO
Data received in FIFO stream: Test
FIFO stream closed

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

Each time on Linux

What is the expected behavior?

Shouldn't close the FIFO stream when a chunk is received

What do you see instead?

Closes the FIFO stream

Additional information

No response

@bnoordhuis
Copy link
Member

See #23220 (comment) from the same issue you linked to. The behavior of a FIFO in non-blocking mode is not well-defined, it doesn't even have to behave the same on different versions of the same operating system.

@Bacto
Copy link
Author

Bacto commented Oct 31, 2022

Thanks for the hint @bnoordhuis :)

@bnoordhuis bnoordhuis closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants