-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
readline unexpected behavior #42581
Comments
The docs say: Lines 466 to 468 in 059b890
The reason your original snippet doesn't work is that the stream has already completed once you start iterating over the readline interface. 'use strict'
const fs = require('node:fs/promises')
const readline = require('node:readline')
;(async function () {
const inputFileName = './in.txt'
const outputFileName = './out.txt'
let inFh, outFh
try {
inFh = await fs.open(inputFileName)
outFh = await fs.open(outputFileName, 'w')
await outFh.appendFile('0')
const rl = readline.createInterface({
input: inFh.createReadStream(),
crlfDelay: Infinity,
})
await outFh.appendFile(rl)
} finally {
await Promise.all([inFh?.close(), outFh?.close()])
console.log('closed')
}
})() |
hey @aduh05
|
hmm @aduh95 why does this work just fine in node v16.x then? Any ideas? |
Not sure I understand your question, what do you mean by "this"? |
I mean that the code snippet given in the description of this issue produces expected result in node v16 but fails in v10 - v14. However, v16 docs have the same statement:
|
On v15.x the FS stream implementation has changed quite significantly, which probably explains the behavior change. But you shouldn't rely on this behavior, it's just a race condition, if you add a |
I see, thank you. |
Version
10.24.1, 12.22.10, 14.19.1
Platform
Linux l1 5.15.25-1-MANJARO #1 SMP PREEMPT Wed Feb 23 14:44:03 UTC 2022 x86_64 GNU/Linux
Subsystem
No response
What steps will reproduce the bug?
in.txt
file with the following contents:test.js
file in the same directory and paste the following code snippet:node test.js
out.txt
file and verify the contentsHow often does it reproduce? Is there a required condition?
always
What is the expected behavior?
the
out.txt
should contain01
What do you see instead?
the
out.txt
file contains0
Additional information
Script produces expected result with Node.js 16.14.2
Script returns expected results in case if synchronous versions of
fs
module functions are usedAdditionally, if you modify the script in the following way, you'll get the expected result:
readline
interface is created after the append operation.resulting contents of
out.txt
:The text was updated successfully, but these errors were encountered: