Skip to content

Commit f2338ed

Browse files
committed
doc: fix up warning text about character devices
The text contained a number of inaccuracies: - The main thread is never blocked by a stream close. - There is no such thing as an EOF character on the OS level, the libuv level, or the Node.js stream level. - These streams *do* respond to `.close()`, but pending reads can delay this indefinitely. - Pushing a random character into the stream works only when the source data can be controlled; Using the JS `.push()` method is something different, and does not “unblock” any threads. Refs: #21212 PR-URL: #22569 Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent a58b8dd commit f2338ed

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

doc/api/fs.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,23 +1454,24 @@ the specified file descriptor. This means that no `'open'` event will be
14541454
emitted. `fd` should be blocking; non-blocking `fd`s should be passed to
14551455
[`net.Socket`][].
14561456

1457-
The blocking `fd`, if pointing to a character device (such as keyboard or
1458-
sound card) can potentially block the main thread on stream close. This is
1459-
because these devices do not produce EOF character as part of their data
1460-
flow cycle, and thereby exemplify endless streams. As a result, they do not
1461-
respond to `stream.close()`. A workaround is to close the stream first
1462-
using `stream.close()` and then push a random character into the stream, and
1463-
issue a single read. This unblocks the reader thread, leads to the completion
1464-
of the data flow cycle, and the actual closing of the stream.
1457+
If `fd` points to a character device that only supports blocking reads
1458+
(such as keyboard or sound card), read operations do not finish until data is
1459+
available. This can prevent the process from exiting and the stream from
1460+
closing naturally.
14651461

14661462
```js
14671463
const fs = require('fs');
14681464
// Create a stream from some character device.
14691465
const stream = fs.createReadStream('/dev/input/event0');
14701466
setTimeout(() => {
1471-
stream.close(); // This does not close the stream.
1467+
stream.close(); // This may not close the stream.
1468+
// Artificially marking end-of-stream, as if the underlying resource had
1469+
// indicated end-of-file by itself, allows the stream to close.
1470+
// This does not cancel pending read operations, and if there is such an
1471+
// operation, the process may still not be able to exit successfully
1472+
// until it finishes.
14721473
stream.push(null);
1473-
stream.read(0); // Pushing a null and reading leads to close.
1474+
stream.read(0);
14741475
}, 100);
14751476
```
14761477

0 commit comments

Comments
 (0)