Skip to content

Commit

Permalink
stream: fix fd is null when calling clearBuffer
Browse files Browse the repository at this point in the history
PR-URL: #50994
Fixes: #50979
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Raz Luvaton <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
  • Loading branch information
kylo5aby authored and RafaelGSS committed Dec 15, 2023
1 parent 735bad3 commit 5112306
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,8 @@ function errorBuffer(state) {

// If there's something in the buffer waiting, then process it.
function clearBuffer(stream, state) {
if ((state[kState] & (kDestroyed | kBufferProcessing | kCorked | kBuffered)) !== kBuffered) {
if ((state[kState] & (kDestroyed | kBufferProcessing | kCorked | kBuffered | kConstructed)) !==
(kBuffered | kConstructed)) {
return;
}

Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-file-write-stream5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

// Test 'uncork' for WritableStream.
// Refs: https://github.com/nodejs/node/issues/50979

const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const test = require('node:test');
const tmpdir = require('../common/tmpdir');

const filepath = tmpdir.resolve('write_stream.txt');
tmpdir.refresh();

const data = 'data';

test('writable stream uncork', () => {
const fileWriteStream = fs.createWriteStream(filepath);

fileWriteStream.on('finish', common.mustCall(() => {
const writtenData = fs.readFileSync(filepath, 'utf8');
assert.strictEqual(writtenData, data);
}));
fileWriteStream.cork();
fileWriteStream.write(data, common.mustCall());
fileWriteStream.uncork();
fileWriteStream.end();
});

0 comments on commit 5112306

Please sign in to comment.