Skip to content

Commit 0a51a6d

Browse files
koichikry
authored andcommitted
Fix process.stdout.end() throws ENOTSOCK error.
1 parent f918e57 commit 0a51a6d

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

lib/tty_posix.js

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ function ReadStream(fd) {
3535
if (!(this instanceof ReadStream)) return new ReadStream(fd);
3636
net.Socket.call(this, fd);
3737

38+
if (this._writeWatcher) {
39+
this._writeWatcher.stop();
40+
this._writeWatcher = null;
41+
}
42+
this.writable = false;
43+
3844
var self = this,
3945
keypressListeners = this.listeners('keypress');
4046

@@ -285,6 +291,12 @@ ReadStream.prototype._emitKey = function(s) {
285291
function WriteStream(fd) {
286292
if (!(this instanceof WriteStream)) return new WriteStream(fd);
287293
net.Socket.call(this, fd);
294+
295+
if (this._readWatcher) {
296+
this._readWatcher.stop();
297+
this._readWatcher = null;
298+
}
299+
this.readable = false;
288300
}
289301
inherits(WriteStream, net.Socket);
290302
exports.WriteStream = WriteStream;

test/disabled/test-tty-stdio.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Can't test this when 'make test' doesn't assign a tty to the stdout.
2+
var common = require('../common');
3+
var assert = require('assert');
4+
var tty = require('tty');
5+
6+
assert.ok(process.stdin instanceof tty.ReadStream);
7+
assert.ok(process.stdin.readable);
8+
assert.ok(!process.stdin.writable);
9+
10+
assert.ok(process.stdout instanceof tty.WriteStream);
11+
assert.ok(!process.stdout.readable);
12+
assert.ok(process.stdout.writable);

test/simple/test-tty-stdout-end.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Can't test this when 'make test' doesn't assign a tty to the stdout.
2+
var common = require('../common');
3+
var assert = require('assert');
4+
var tty = require('tty');
5+
6+
var closed = false;
7+
process.stdout.on('close', function() {
8+
closed = true;
9+
});
10+
process.on('exit', function() {
11+
assert.ok(closed);
12+
});
13+
14+
process.stdout.end();

0 commit comments

Comments
 (0)