Permalink
Browse files

Unhandled 'error' events kill program

By default 'error' throws. addListener to prevent this.
  • Loading branch information...
1 parent 11f673e commit 6abdf051d3d91d05f262d2dfe98e1c768ff0b543 @ry ry committed Apr 23, 2010
Showing with 75 additions and 19 deletions.
  1. +40 −0 lib/events.js
  2. +5 −1 lib/http.js
  3. +18 −12 lib/net.js
  4. +0 −3 src/node.js
  5. +3 −3 test/pummel/test-keep-alive.js
  6. +9 −0 test/pummel/test-tcp-timeout.js
View
@@ -1,5 +1,44 @@
exports.EventEmitter = process.EventEmitter;
+
+process.EventEmitter.prototype.emit = function (type) {
+ if (type == 'error') {
+ if (!this._events || !this._events.error ||
+ (this._events.error instanceof Array && !this._events.error.length))
+ {
+ if (arguments[1] instanceof Error) {
+ throw arguments[1];
+ } else {
+ throw new Error("Uncaught, unspecfied 'error' event.");
+ }
+ return false;
+ }
+ }
+
+ if (!this._events) return false;
+ if (!this._events[type]) return false;
+
+ if (typeof this._events[type] == 'function') {
+ var args = Array.prototype.slice.call(arguments, 1);
+
+ this._events[type].apply(this, args);
+ return true;
+
+ } else if (this._events[type] instanceof Array) {
+ var args = Array.prototype.slice.call(arguments, 1);
+
+
+ var listeners = this._events[type].slice(0);
+ for (var i = 0, l = listeners.length; i < l; i++) {
+ listeners[i].apply(this, args);
+ }
+ return true;
+
+ } else {
+ return false;
+ }
+};
+
// process.EventEmitter is defined in src/node_events.cc
@ry
ry Apr 23, 2010

yes, it's still used by some modules (ones that doesn't emit 'error', though)

@ry
ry Apr 23, 2010

I mean this: http://github.com/ry/node/blob/c9e27b11c5b995b7ff9d3c1dde4687ccd54f0071/src/node_signal_watcher.cc#L35

Yeah the commet should probably be changed.. I'll just wait until I've got rid of node_events.cc completely

// process.EventEmitter.prototype.emit() is also defined there.
process.EventEmitter.prototype.addListener = function (type, listener) {
@@ -27,6 +66,7 @@ process.EventEmitter.prototype.addListener = function (type, listener) {
return this;
};
+
process.EventEmitter.prototype.removeListener = function (type, listener) {
if ('function' !== typeof listener) {
throw new Error('removeListener only takes instances of Function');
View
@@ -548,9 +548,13 @@ function connectionListener (socket) {
parser.socket = socket;
if (self.secure) {
- socket.setSecure(self.credentials);
+ socket.setSecure(self.credentials);
}
+ socket.addListener('error', function (e) {
+ sys.puts('node http server: ' + e);
+ });
+
socket.ondata = function (d, start, end) {
var bytesParsed = parser.execute(d, start, end - start);
if (parser.incoming && parser.incoming.upgrade) {
View
@@ -826,20 +826,26 @@ Stream.prototype.destroy = function (exception) {
Stream.prototype._shutdown = function () {
- if (this.writable) {
- this.writable = false;
+ if (!this.writable) {
+ throw new Error('The connection is not writable');
+ } else {
+ if (this.readable) {
+ // readable and writable
+ this.writable = false;
- if (this.secure) {
- this._shutdownSecure();
- }
- try {
- shutdown(this.fd, 'write')
- } catch (e) {
- this.destroy(e);
- return;
+ if (this.secure) {
+ this._shutdownSecure();
+ }
+ try {
+ shutdown(this.fd, 'write')
+ } catch (e) {
+ this.destroy(e);
+ return;
+ }
+ } else {
+ // writable but not readable
+ this.destroy();
}
-
- if (!this.readable) this.destroy();
}
};
View
@@ -132,8 +132,6 @@ process.__defineGetter__('stdout', function () {
var net = module.requireNative('net');
stdout = new net.Stream(process.binding('stdio').stdoutFD);
- stdout.addListener('error', function (err) { throw err; });
-
return stdout;
});
@@ -143,7 +141,6 @@ process.openStdin = function () {
var net = module.requireNative('net');
var fd = process.binding('stdio').openStdin();
stdin = new net.Stream(fd);
- stdin.addListener('error', function (err) { throw err; });
stdin.resume();
stdin.readable = true;
return stdin;
@@ -5,9 +5,9 @@ exec = require("child_process").exec;
body = "hello world\n";
server = http.createServer(function (req, res) {
- res.writeHead(200, {
- "Content-Length": body.length,
- "Content-Type": "text/plain",
+ res.writeHead(200, {
+ "Content-Length": body.length,
+ "Content-Type": "text/plain"
});
res.write(body);
res.end();
@@ -5,6 +5,8 @@ starttime = null;
timeouttime = null;
timeout = 1000;
+gotError = false
+
var echo_server = net.createServer(function (socket) {
socket.setTimeout(timeout);
@@ -14,6 +16,11 @@ var echo_server = net.createServer(function (socket) {
p(timeouttime);
});
+ socket.addListener("error", function (e) {
+ assert.ok(e instanceof Error);
+ gotError = true;
+ })
+
socket.addListener("data", function (d) {
puts(d);
socket.write(d);
@@ -77,4 +84,6 @@ process.addListener("exit", function () {
// Allow for 800 milliseconds more
assert.ok(diff < timeout + 800);
+
+ assert.ok(gotError);
});

0 comments on commit 6abdf05

Please sign in to comment.