Closed
Description
Version
v18.16.0 and up, v19.8.0 and up, v20.x, v21.x
Platform
Darwin x86_64
Subsystem
fs, stream
What steps will reproduce the bug?
const fs = require('fs');
const w = fs.createWriteStream('file.txt');
w.on('open', () => {
w.write('hello');
process.nextTick(() => {
w.write('world');
w.end();
});
});
w.on('close', () => {
console.log(fs.readFileSync('file.txt', 'utf8'));
});
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
helloworld
What do you see instead?
worldhello
Additional information
The issue was caused by this change: #46818, which landed in node v18.16.0
and v19.8.0
.
(Thanks to @Fadorico for tracking down this change. I have also confirmed this by building node locally.)
This is causing the 'unzipper' module to create corrupted files, because its 'fstream' dependency uses fs.createWriteStream()
like in the example above.
It looks like when the stream's "open" event fires, the stream does not yet have the "constructed" flag set, so that might be why things are getting weird.
As a workaround, if I wait an additional tick after the "open" event, there are no issues:
w.on('open', () => {
process.nextTick(() => {
w.write('hello');
process.nextTick(() => {
w.write('world');
w.end();
});
});
});