Skip to content

Commit

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

Fixes: #47948
Signed-off-by: killagu <killa123@126.com>
PR-URL: #47949
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
killagu authored and RafaelGSS committed Jul 3, 2023
1 parent 4cad9fd commit 19fde63
Show file tree
Hide file tree
Showing 2 changed files with 18 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;
}
cb();
};

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-internal-fs-syncwritestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
assert.strictEqual(stream.fd, null);
}));
}

// Verify that an error on _write() triggers an '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);
fs.closeSync(fd);
}));
}

0 comments on commit 19fde63

Please sign in to comment.