Created
March 11, 2011 17:07
-
-
Save koichik/866205 to your computer and use it in GitHub Desktop.
Fix GH-746 process.stdin.destroy() breaks http server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From feac531600ca073dc5f50bed9862ccd8ac0619b2 Mon Sep 17 00:00:00 2001 | |
From: koichik <koichik@improvement.jp> | |
Date: Sat, 12 Mar 2011 01:00:46 +0900 | |
Subject: [PATCH 1/2] Fix GH-746 process.stdin.destroy() breaks http server | |
--- | |
lib/net.js | 4 ++-- | |
test/simple/test-net-server-acceept.js | 27 +++++++++++++++++++++++++++ | |
2 files changed, 29 insertions(+), 2 deletions(-) | |
create mode 100644 test/simple/test-net-server-acceept.js | |
diff --git a/lib/net.js b/lib/net.js | |
index ab4fd25..6aa1453 100644 | |
--- a/lib/net.js | |
+++ b/lib/net.js | |
@@ -877,7 +877,7 @@ function Server(/* [ options, ] listener */) { | |
self.watcher.stop(); | |
} | |
- while (self.fd) { | |
+ while (typeof self.fd === 'number') { | |
try { | |
var peerInfo = accept(self.fd); | |
} catch (e) { | |
@@ -1098,7 +1098,7 @@ Server.prototype.address = function() { | |
Server.prototype.close = function() { | |
var self = this; | |
- if (!self.fd) throw new Error('Not running'); | |
+ if (typeof self.fd !== 'number') throw new Error('Not running'); | |
self.watcher.stop(); | |
diff --git a/test/simple/test-net-server-acceept.js b/test/simple/test-net-server-acceept.js | |
new file mode 100644 | |
index 0000000..c19015b | |
--- /dev/null | |
+++ b/test/simple/test-net-server-acceept.js | |
@@ -0,0 +1,27 @@ | |
+var common = require('../common'); | |
+var assert = require('assert'); | |
+var net = require('net'); | |
+ | |
+process.stdin.destroy(); | |
+ | |
+var accepted = null; | |
+var server = net.createServer(function(socket) { | |
+ console.log('accepted'); | |
+ accepted = socket; | |
+ socket.end(); | |
+ server.close(); | |
+}); | |
+ | |
+server.listen(common.PORT, function() { | |
+ console.log('listening...'); | |
+ assert.equal(server.fd, 0); | |
+}); | |
+ | |
+setTimeout(function() { | |
+ var client = net.createConnection(common.PORT); | |
+}, 100); | |
+ | |
+process.on('exit', function() { | |
+ assert.ok(accepted); | |
+}); | |
+ | |
-- | |
1.7.1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 326d3cceebe03de2dc1c76dea2f1d1cb3e3d3764 Mon Sep 17 00:00:00 2001 | |
From: koichik <koichik@improvement.jp> | |
Date: Sat, 12 Mar 2011 01:35:02 +0900 | |
Subject: [PATCH 2/2] Fix GH-746 process.stdin.destroy() breaks http server | |
--- | |
lib/net.js | 26 ++++++++++++++------------ | |
1 files changed, 14 insertions(+), 12 deletions(-) | |
diff --git a/lib/net.js b/lib/net.js | |
index 6aa1453..90e8cf7 100644 | |
--- a/lib/net.js | |
+++ b/lib/net.js | |
@@ -118,7 +118,7 @@ function setImplmentationMethods(self) { | |
// emitting the same value that we see now. Otherwise, we can end up | |
// calling emit() after recvMsg() has been called again and end up | |
// emitting null (or another FD). | |
- if (recvMsg.fd !== null) { | |
+ if (typeof recvMsg.fd === 'number') { | |
var fd = recvMsg.fd; | |
process.nextTick(function() { | |
self.emit('fd', fd); | |
@@ -251,16 +251,16 @@ Object.defineProperty(Socket.prototype, 'readyState', { | |
if (this._connecting) { | |
return 'opening'; | |
} else if (this.readable && this.writable) { | |
- assert(typeof this.fd == 'number'); | |
+ assert(typeof this.fd === 'number'); | |
return 'open'; | |
} else if (this.readable && !this.writable) { | |
- assert(typeof this.fd == 'number'); | |
+ assert(typeof this.fd === 'number'); | |
return 'readOnly'; | |
} else if (!this.readable && this.writable) { | |
- assert(typeof this.fd == 'number'); | |
+ assert(typeof this.fd === 'number'); | |
return 'writeOnly'; | |
} else { | |
- assert(typeof this.fd != 'number'); | |
+ assert(typeof this.fd !== 'number'); | |
return 'closed'; | |
} | |
} | |
@@ -665,7 +665,7 @@ Socket.prototype._onReadable = function() { | |
Socket.prototype.connect = function() { | |
var self = this; | |
initSocket(self); | |
- if (self.fd) throw new Error('Socket already opened'); | |
+ if (typeof self.fd === 'number') throw new Error('Socket already opened'); | |
if (!self._readWatcher) throw new Error('No readWatcher'); | |
timers.active(this); | |
@@ -723,7 +723,7 @@ Socket.prototype.setKeepAlive = function(enable, time) { | |
Socket.prototype.setTimeout = function(msecs, callback) { | |
if (msecs > 0) { | |
timers.enroll(this, msecs); | |
- if (this.fd) { timers.active(this); } | |
+ if (typeof this.fd === 'number') { timers.active(this); } | |
if (callback) { | |
this.once('timeout', callback); | |
} | |
@@ -739,7 +739,9 @@ Socket.prototype.pause = function() { | |
Socket.prototype.resume = function() { | |
- if (this.fd === null) throw new Error('Cannot resume() closed Socket.'); | |
+ if (typeof this.fd !== 'number') { | |
+ throw new Error('Cannot resume() closed Socket.'); | |
+ } | |
if (this._readWatcher) { | |
this._readWatcher.stop(); | |
this._readWatcher.set(this.fd, true, false); | |
@@ -793,7 +795,7 @@ Socket.prototype.destroy = function(exception) { | |
} | |
// FIXME Bug when this.fd == 0 | |
- if (typeof this.fd == 'number') { | |
+ if (typeof this.fd === 'number') { | |
debug('close ' + this.fd); | |
close(this.fd); | |
this.fd = null; | |
@@ -986,7 +988,7 @@ Server.prototype._rejectPending = function() { | |
// server.listen(8000, '192.168.1.2'); | |
Server.prototype.listen = function() { | |
var self = this; | |
- if (self.fd) throw new Error('Server already opened'); | |
+ if (typeof self.fd === 'number') throw new Error('Server already opened'); | |
var lastArg = arguments[arguments.length - 1]; | |
if (typeof lastArg == 'function') { | |
@@ -1041,7 +1043,7 @@ Server.prototype.listen = function() { | |
}; | |
Server.prototype.listenFD = function(fd, type) { | |
- if (this.fd) { | |
+ if (typeof this.fd === 'number') { | |
throw new Error('Server already opened'); | |
} | |
@@ -1076,7 +1078,7 @@ Server.prototype._doListen = function() { | |
// It could be that server.close() was called between the time the | |
// original listen command was issued and this. Bail if that's the case. | |
// See test/simple/test-net-eaddrinuse.js | |
- if (typeof self.fd != 'number') return; | |
+ if (typeof self.fd !== 'number') return; | |
try { | |
listen(self.fd, self._backlog || 128); | |
-- | |
1.7.1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment