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

stream: do not defer construction by one microtick #52005

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ function _construct(callback) {
} else {
stream.fd = fd;
callback();
stream.emit('open', stream.fd);
stream.emit('ready');
// Ready must be deferred to the next tick, because callback will schedule
// some microtick work that we need to complete first.
process.nextTick(emitReady, stream);
}
});
}
}

function emitReady(stream) {
stream.emit('open', stream.fd);
stream.emit('ready');
}

// This generates an fs operations structure for a FileHandle
const FileHandleOperations = (handle) => {
return {
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-fs-writestream-open-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

Check failure on line 1 in test/parallel/test-fs-writestream-open-write.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon

const common = require('../common');
const { strictEqual } = require('assert');
const { tmpdir } = require('os');
const { join } = require('path');
mcollina marked this conversation as resolved.
Show resolved Hide resolved
const fs = require('fs');

// Regression test for https://github.com/nodejs/node/issues/51993

const file = join(tmpdir(), `test-fs-writestream-open-write-${process.pid}.txt`);
mcollina marked this conversation as resolved.
Show resolved Hide resolved

const w = fs.createWriteStream(file);

w.on('open', common.mustCall(() => {
w.write('hello');

process.nextTick(() => {
w.write('world');
w.end();
});
}));

w.on('close', common.mustCall(() => {
strictEqual(fs.readFileSync(file, 'utf8'), 'helloworld');
fs.unlinkSync(file);
}));
Loading