Skip to content

Commit 16f0240

Browse files
committed
Catch errors from stream events in net.js
Pipe into 'error' event.
1 parent e72b7b8 commit 16f0240

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

lib/net.js

+51-20
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,13 @@ function _doFlush () {
267267
// Stream becomes writeable on connect() but don't flush if there's
268268
// nothing actually to write
269269
if (socket.flush()) {
270-
if (socket._events && socket._events['drain']) socket.emit("drain");
271-
if (socket.ondrain) socket.ondrain(); // Optimization
270+
try {
271+
if (socket._events && socket._events['drain']) socket.emit("drain");
272+
if (socket.ondrain) socket.ondrain(); // Optimization
273+
} catch (e) {
274+
socket.destroy(e);
275+
return;
276+
}
272277
}
273278
}
274279

@@ -305,7 +310,7 @@ function initStream (self) {
305310
if (secureBytesRead === null && !self.server) {
306311
// Client needs to write as part of handshake
307312
self._writeWatcher.start();
308-
return;
313+
return;
309314
}
310315
} else {
311316
bytesRead = read(self.fd,
@@ -320,7 +325,7 @@ function initStream (self) {
320325

321326
//debug('bytesRead ' + bytesRead + '\n');
322327

323-
if (self.secure && bytesRead == 0 && secureBytesRead > 0){
328+
if (self.secure && bytesRead == 0 && secureBytesRead > 0) {
324329
// Deal with SSL handshake
325330
if (self.server) {
326331
self._checkForSecureHandshake();
@@ -338,8 +343,13 @@ function initStream (self) {
338343
if (!self.writable) self.destroy();
339344
// Note: 'close' not emitted until nextTick.
340345

341-
if (self._events && self._events['end']) self.emit('end');
342-
if (self.onend) self.onend();
346+
try {
347+
if (self._events && self._events['end']) self.emit('end');
348+
if (self.onend) self.onend();
349+
} catch (e) {
350+
self.destroy(e);
351+
return;
352+
}
343353
} else if (bytesRead > 0) {
344354

345355
timeout.active(self);
@@ -348,17 +358,22 @@ function initStream (self) {
348358
var end = pool.used + bytesRead;
349359
pool.used += bytesRead;
350360

351-
if (!self._encoding) {
352-
if (self._events && self._events['data']) {
353-
// emit a slice
354-
self.emit('data', pool.slice(start, end));
355-
}
361+
try {
362+
if (!self._encoding) {
363+
if (self._events && self._events['data']) {
364+
// emit a slice
365+
self.emit('data', pool.slice(start, end));
366+
}
356367

357-
// Optimization: emit the original buffer with end points
358-
if (self.ondata) self.ondata(pool, start, end);
359-
} else {
360-
var string = pool.toString(self._encoding, start, end);
361-
self.emit('data', string);
368+
// Optimization: emit the original buffer with end points
369+
if (self.ondata) self.ondata(pool, start, end);
370+
} else {
371+
var string = pool.toString(self._encoding, start, end);
372+
self.emit('data', string);
373+
}
374+
} catch (e) {
375+
self.destroy(e);
376+
return;
362377
}
363378
}
364379
};
@@ -586,9 +601,14 @@ Stream.prototype._writeOut = function (data, encoding) {
586601
} else {
587602
var secureBytesWritten = write(this.fd, securePool, 0, secureLen);
588603
}
589-
if(!this.secureEstablished && this.secureStream.isInitFinished()) {
604+
if (!this.secureEstablished && this.secureStream.isInitFinished()) {
590605
this.secureEstablished = true;
591-
if (this._events && this._events['secure']) this.emit('secure');
606+
try {
607+
if (this._events && this._events['secure']) this.emit('secure');
608+
} catch (e) {
609+
this.destroy(e);
610+
return;
611+
}
592612
}
593613
} else {
594614
bytesWritten = write(this.fd, buffer, off, len);
@@ -696,7 +716,12 @@ function doConnect (socket, port, host) {
696716
socket.resume();
697717
socket.readable = socket.writable = true;
698718
socket._writeWatcher.callback = _doFlush;
699-
socket.emit('connect');
719+
try {
720+
socket.emit('connect');
721+
} catch (e) {
722+
socket.destroy(e);
723+
return;
724+
}
700725
} else if (errno != EINPROGRESS) {
701726
socket.destroy(errnoException(errno, 'connect'));
702727
}
@@ -902,9 +927,15 @@ function Server (listener) {
902927
s.resume();
903928

904929
self.emit('connection', s);
930+
905931
// The 'connect' event probably should be removed for server-side
906932
// sockets. It's redundant.
907-
s.emit('connect');
933+
try {
934+
s.emit('connect');
935+
} catch (e) {
936+
s.destroy(e);
937+
return;
938+
}
908939
}
909940
};
910941
}

0 commit comments

Comments
 (0)