Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save koichik/856662 to your computer and use it in GitHub Desktop.
Save koichik/856662 to your computer and use it in GitHub Desktop.
Fix GH-746 process.stdin.destroy() breaks http server
From bd798f2f244676bc0b0fd5fc6e1664c0ac69f109 Mon Sep 17 00:00:00 2001
From: koichik <koichik@improvement.jp>
Date: Sun, 6 Mar 2011 04:06:12 +0900
Subject: [PATCH 1/2] Fix net.Server does not work when the FD is 0
Issue: GH-746 process.stdin.destroy() breaks http server.
Solution: Use ==/!= for the check of the FD rather than a truthy/falsy.
---
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..908838a 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -877,7 +877,7 @@ function Server(/* [ options, ] listener */) {
self.watcher.stop();
}
- while (self.fd) {
+ while (self.fd != null) {
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 (self.fd == null) 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
From a218cce7f0b4c19ffb3c79dd3283d89e6e68bd19 Mon Sep 17 00:00:00 2001
From: koichik <koichik@improvement.jp>
Date: Wed, 9 Mar 2011 03:00:52 +0900
Subject: [PATCH 2/2] Fix net.Server does not work when a FD is 0
Issue: GH-746 process.stdin.destroy() breaks http server.
Solution: Use ==/!= for the check of the FD rather than a truthy/falsy.
---
lib/fs.js | 8 ++++----
lib/net.js | 12 ++++++------
2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/lib/fs.js b/lib/fs.js
index d07a152..c56fe63 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -779,7 +779,7 @@ var ReadStream = fs.ReadStream = function(path, options) {
}
}
- if (this.fd !== null) {
+ if (this.fd != null) {
return;
}
@@ -900,7 +900,7 @@ ReadStream.prototype.destroy = function(cb) {
});
}
- if (this.fd) {
+ if (this.fd != null) {
close();
} else {
this.addListener('open', close);
@@ -958,7 +958,7 @@ var WriteStream = fs.WriteStream = function(path, options) {
this.busy = false;
this._queue = [];
- if (this.fd === null) {
+ if (this.fd == null) {
this._queue.push([fs.open, this.path, this.flags, this.mode, undefined]);
this.flush();
}
@@ -1082,7 +1082,7 @@ WriteStream.prototype.destroy = function(cb) {
});
}
- if (this.fd) {
+ if (this.fd != null) {
close();
} else {
this.addListener('open', close);
diff --git a/lib/net.js b/lib/net.js
index 908838a..9a5254f 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 (recvMsg.fd != null) {
var fd = recvMsg.fd;
process.nextTick(function() {
self.emit('fd', fd);
@@ -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 (self.fd != null) 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 (this.fd != null) { timers.active(this); }
if (callback) {
this.once('timeout', callback);
}
@@ -739,7 +739,7 @@ Socket.prototype.pause = function() {
Socket.prototype.resume = function() {
- if (this.fd === null) throw new Error('Cannot resume() closed Socket.');
+ if (this.fd == null) throw new Error('Cannot resume() closed Socket.');
if (this._readWatcher) {
this._readWatcher.stop();
this._readWatcher.set(this.fd, true, false);
@@ -986,7 +986,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 (self.fd != null) throw new Error('Server already opened');
var lastArg = arguments[arguments.length - 1];
if (typeof lastArg == 'function') {
@@ -1041,7 +1041,7 @@ Server.prototype.listen = function() {
};
Server.prototype.listenFD = function(fd, type) {
- if (this.fd) {
+ if (this.fd != null) {
throw new Error('Server already opened');
}
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment