Skip to content

Commit 927888b

Browse files
committed
fs: call the callback with an error if writeSync fails
Catch SyncWriteStream write file error. Fixes: #47948 Signed-off-by: killagu <[email protected]>
1 parent 226573b commit 927888b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/internal/fs/sync_write_stream.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ ObjectSetPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
2323
ObjectSetPrototypeOf(SyncWriteStream, Writable);
2424

2525
SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
26-
writeSync(this.fd, chunk, 0, chunk.length);
26+
try {
27+
writeSync(this.fd, chunk, 0, chunk.length);
28+
} catch (e) {
29+
cb(e);
30+
return;
31+
}
2732
cb();
28-
return true;
2933
};
3034

3135
SyncWriteStream.prototype._destroy = function(err, cb) {

test/parallel/test-internal-fs-syncwritestream.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,15 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
7474
assert.strictEqual(stream.fd, null);
7575
}));
7676
}
77+
78+
// Verify that an error on _write() triggers an 'error' event.
79+
{
80+
const fd = fs.openSync(filename, 'w');
81+
const stream = new SyncWriteStream(fd);
82+
83+
assert.strictEqual(stream.fd, fd);
84+
stream._write({}, null, common.mustCall((err) => {
85+
assert(err);
86+
fs.closeSync(fd);
87+
}));
88+
}

0 commit comments

Comments
 (0)