fs: guarantee order of callbacks in ws.close

Refactor WriteStream.prototype.close and WriteStream.prototype._destroy
to always call the callback passed to close in order. Protects from
calling .close() without a callback.

Fixes: https://github.com/nodejs/node/issues/17951
See: https://github.com/nodejs/node/pull/15407

PR-URL: https://github.com/nodejs/node/pull/18002
Fixes: https://github.com/nodejs/node/issues/17951
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Matteo Collina authored and Myles Borins committed 2018-01-10 01:42:24 -05:00
1 parent 0e116a01c8
commit f1485565ef
4 files changed
+77 -27

No files matched your search

@@ -10,8 +10,9 @@ let stream = fs.createWriteStream(file, { flags: 'w+', autoClose: false });
stream.write('Test1');
stream.end();
stream.on('finish', common.mustCall(function() {
stream.on('close', common.mustNotCall());
process.nextTick(common.mustCall(function() {
assert.strictEqual(stream.closed, undefined);
assert.strictEqual(stream.closed, false);
assert.notStrictEqual(stream.fd, null);
next();
}));
@@ -23,9 +24,12 @@ function next() {
stream.write('Test2');
stream.end();
stream.on('finish', common.mustCall(function() {
assert.strictEqual(stream.closed, true);
assert.strictEqual(stream.closed, false);
assert.strictEqual(stream.fd, null);
process.nextTick(common.mustCall(next2));
stream.on('close', common.mustCall(function() {
assert.strictEqual(stream.closed, true);
process.nextTick(next2);
}));
}));
}
@@ -44,9 +48,10 @@ function next3() {
stream.write('Test3');
stream.end();
stream.on('finish', common.mustCall(function() {
process.nextTick(common.mustCall(function() {
assert.strictEqual(stream.closed, false);
assert.strictEqual(stream.fd, null);
stream.on('close', common.mustCall(function() {
assert.strictEqual(stream.closed, true);
assert.strictEqual(stream.fd, null);
}));
}));
}
@@ -0,0 +1,12 @@
'use strict';
const common = require('../common');
const fs = require('fs');
const path = require('path');
common.refreshTmpDir();
const s = fs.createWriteStream(path.join(common.tmpDir, 'nocallback'));
s.end('hello world');
s.close();
@@ -1,12 +1,45 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
common.refreshTmpDir();
const s = fs.createWriteStream(path.join(common.tmpDir, 'rw'));
{
const s = fs.createWriteStream(path.join(common.tmpDir, 'rw'));
s.close(common.mustCall());
s.close(common.mustCall());
s.close(common.mustCall());
s.close(common.mustCall());
}
{
const s = fs.createWriteStream(path.join(common.tmpDir, 'rw2'));
let emits = 0;
s.on('close', () => {
emits++;
});
s.close(common.mustCall(() => {
assert.strictEqual(emits, 1);
s.close(common.mustCall(() => {
assert.strictEqual(emits, 1);
}));
process.nextTick(() => {
s.close(common.mustCall(() => {
assert.strictEqual(emits, 1);
}));
});
}));
}
{
const s = fs.createWriteStream(path.join(common.tmpDir, 'rw'), {
autoClose: false
});
s.close(common.mustCall());
s.close(common.mustCall());
}