-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"WriteStream.close" from "fs" module not executing the callback #2950
Comments
The first example is basically a logic bug because Your second example works for me with v4.1.0. |
Sorry, I tested my original examples in REPL. The snippets below can be executed as scripts. In Scripts 1, 2 & 3, the file descriptor is already closed, when "close" function is called. In Script 4, the "closed" property is not set and "close" event has not been emitted yet, when "close" function is called. Script 1. // prints:
// "closed1"
var fs = require('fs');
var a = fs.createWriteStream('/tmp/aaa');
a.close(function () {
console.log('closed1');
a.close(console.log.bind(null, 'closed2'));
}); Script 2. // prints:
// "error { [Error: ENOSPC: no space left on device, write] errno: -28, code: 'ENOSPC', syscall: 'write' }"
// "closed1"
var fs = require('fs');
var a = fs.createWriteStream('/dev/full');
a.on('close', function () {
console.log('closed1');
a.close(console.log.bind(null, 'closed2'));
});
a.on('error', console.log.bind(null, 'error'));
a.write('a'); Script 3. // prints:
// "closed1"
var fs = require('fs');
var a = fs.createWriteStream('/tmp/aaa');
a.on('close', function () {
console.log('closed1');
a.close(console.log.bind(null, 'closed2'));
});
a.end(); Script 4. // prints:
// "finish closed=undefined"
// "closed1"
// "closed2"
// "error { [Error: EBADF: bad file descriptor, close] errno: -9, code: 'EBADF', syscall: 'close' }"
var fs = require('fs');
var a = fs.createWriteStream('/tmp/aaa');
a.on('error', console.log.bind(null, 'error'));
a.on('close', console.log.bind(null, 'closed1'));
a.on('finish', function () {
console.log('finish', 'closed=' + a.closed);
a.close(console.log.bind(null, 'closed2'));
});
a.end(); |
Calling fs.ReadStream#close() or fs.WriteStream#close() twice made it try to close the file descriptor twice, with the second attempt using the nulled out `.fd` property and failing with an EBADF error. Fixes: nodejs#2950
What is the correct behavior when calling
Asking for a friend. |
@Trott I've been coming around to the idea that an EBADF exception is the most appropriate. |
This is what I'm currently getting on v6.9.4: var fs = require('fs');
var a = fs.createWriteStream('/tmp/aaa');
a.close(console.log.bind(null, 'closed'));
// prints "closed"
a.close(console.log.bind(null, 'closed'));
Is that intended behavior now? |
@evanlucas I think that's not correct and we have a bug, a user should expect to receive that error in the callback, currently it is not. |
wait, |
@evanlucas The correct way to end a stream is to call So, if we think this is a needed feature, we should in fact document it, and add an error parameter to that callback. But I'm all in for deprecating However, we still need to document and fix If we look at: https://github.com/nodejs/node/blob/master/lib/fs.js#L1857, it's not doing anything. Ahum.. I'll try to assemble a PR for this anyway. |
Changed the logic in fs.ReadStream and fs.WriteStream so that close always calls the prototype method rather than the internal event listener. Fixes: nodejs#2950
Fixed in b1fc774. |
Changed the logic in fs.ReadStream and fs.WriteStream so that close always calls the prototype method rather than the internal event listener. Fixes: #2950 PR-URL: #11225 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
Changed the logic in fs.ReadStream and fs.WriteStream so that close always calls the prototype method rather than the internal event listener. Fixes: nodejs#2950 PR-URL: nodejs#11225 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
The callback passed to
WriteStream.close
function from the "fs" module is not called in certain situations (see below). The same applies to the ReadStream.The text was updated successfully, but these errors were encountered: