Skip to content

Commit

Permalink
fs: fix callback with error if SyncWriteStream writeSync failed
Browse files Browse the repository at this point in the history
Catch SyncWriteStream write file error.

Fixes: #47948
  • Loading branch information
killagu committed May 14, 2023
1 parent 226573b commit eff6f0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/internal/fs/sync_write_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ ObjectSetPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
ObjectSetPrototypeOf(SyncWriteStream, Writable);

SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
writeSync(this.fd, chunk, 0, chunk.length);
try {
writeSync(this.fd, chunk, 0, chunk.length);
} catch (e) {
cb(e);
return true;
}
cb();
return true;
};
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-internal-fs-syncwritestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
assert.strictEqual(stream.fd, null);
}));
}

// Verify write file failed trigger error event
{
const fd = fs.openSync(filename, 'w');
const stream = new SyncWriteStream(fd);

assert.strictEqual(stream.fd, fd);
stream._write({}, null, common.mustCall((err) => {
assert(err);
}));
}

0 comments on commit eff6f0f

Please sign in to comment.