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 10, 2023
1 parent 0736d0b commit c5b5375
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/internal/fs/sync_write_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ ObjectSetPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
ObjectSetPrototypeOf(SyncWriteStream, Writable);

SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
writeSync(this.fd, chunk, 0, chunk.length);
cb();
try {
writeSync(this.fd, chunk, 0, chunk.length);
cb();
} catch (e) {
cb(e);
}
return true;
};

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 @@ -74,3 +74,15 @@ 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.on('error', common.mustCall());
stream._write({}, common.mustCall((err) => {

Check failure on line 85 in test/parallel/test-internal-fs-syncwritestream.js

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- node:internal/fs/sync_write_stream:30 cb(e); ^ TypeError: cb is not a function at SyncWriteStream._write (node:internal/fs/sync_write_stream:30:5) at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-internal-fs-syncwritestream.js:85:10) at Module._compile (node:internal/modules/cjs/loader:1255:14) at Module._extensions..js (node:internal/modules/cjs/loader:1309:10) at Module.load (node:internal/modules/cjs/loader:1113:32) at Module._load (node:internal/modules/cjs/loader:960:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) at node:internal/main/run_main_module:23:47 Node.js v21.0.0-pre Command: out/Release/node --expose-internals /home/runner/work/node/node/test/parallel/test-internal-fs-syncwritestream.js

Check failure on line 85 in test/parallel/test-internal-fs-syncwritestream.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:internal/fs/sync_write_stream:30 cb(e); ^ TypeError: cb is not a function at SyncWriteStream._write (node:internal/fs/sync_write_stream:30:5) at Object.<anonymous> (/Users/runner/work/node/node/test/parallel/test-internal-fs-syncwritestream.js:85:10) at Module._compile (node:internal/modules/cjs/loader:1255:14) at Module._extensions..js (node:internal/modules/cjs/loader:1309:10) at Module.load (node:internal/modules/cjs/loader:1113:32) at Module._load (node:internal/modules/cjs/loader:960:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) at node:internal/main/run_main_module:23:47 Node.js v21.0.0-pre Command: out/Release/node --expose-internals /Users/runner/work/node/node/test/parallel/test-internal-fs-syncwritestream.js

Check failure on line 85 in test/parallel/test-internal-fs-syncwritestream.js

View workflow job for this annotation

GitHub Actions / test-asan

--- stderr --- node:internal/fs/sync_write_stream:30 cb(e); ^ TypeError: cb is not a function at SyncWriteStream._write (node:internal/fs/sync_write_stream:30:5) at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-internal-fs-syncwritestream.js:85:10) at Module._compile (node:internal/modules/cjs/loader:1255:14) at Module._extensions..js (node:internal/modules/cjs/loader:1309:10) at Module.load (node:internal/modules/cjs/loader:1113:32) at Module._load (node:internal/modules/cjs/loader:960:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) at node:internal/main/run_main_module:23:47 Node.js v21.0.0-pre Command: out/Release/node --expose-internals /home/runner/work/node/node/test/parallel/test-internal-fs-syncwritestream.js
assert(err);
}));
}

0 comments on commit c5b5375

Please sign in to comment.