summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFedor Indutny <fedor.indutny@gmail.com>2014-02-09 14:59:31 +0400
committerTimothy J Fontaine <tjfontaine@gmail.com>2014-02-10 10:59:52 -0800
commitdee5270a6c093db32897d45c4ed18f56bd772987 (patch)
treeacea5f55eb17f2a00d167fa02b935a0cd594800b
parent5c832e44c3c61ad41506df0d283901aba6aea187 (diff)
downloadnode-dee5270a6c093db32897d45c4ed18f56bd772987.tar.gz
net: do not re-emit stream errors
fix #7015
-rw-r--r--lib/_stream_writable.js4
-rw-r--r--lib/net.js5
-rw-r--r--test/simple/test-net-error-twice.js50
3 files changed, 56 insertions, 3 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js
index 403cb7b47..257a400fa 100644
--- a/lib/_stream_writable.js
+++ b/lib/_stream_writable.js
@@ -104,6 +104,9 @@ function WritableState(options, stream) {
this.writelen = 0;
this.buffer = [];
+
+ // True if the error was already emitted and should not be thrown again
+ this.errorEmitted = false;
}
function Writable(options) {
@@ -232,6 +235,7 @@ function onwriteError(stream, state, sync, er, cb) {
else
cb(er);
+ stream._writableState.errorEmitted = true;
stream.emit('error', er);
}
diff --git a/lib/net.js b/lib/net.js
index 31de90cb0..047f8cd2f 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -122,7 +122,6 @@ exports._normalizeConnectArgs = normalizeConnectArgs;
// called when creating new Socket, or when re-using a closed Socket
function initSocketHandle(self) {
self.destroyed = false;
- self.errorEmitted = false;
self.bytesRead = 0;
self._bytesDispatched = 0;
@@ -436,11 +435,11 @@ Socket.prototype._destroy = function(exception, cb) {
function fireErrorCallbacks() {
if (cb) cb(exception);
- if (exception && !self.errorEmitted) {
+ if (exception && !self._writableState.errorEmitted) {
process.nextTick(function() {
self.emit('error', exception);
});
- self.errorEmitted = true;
+ self._writableState.errorEmitted = true;
}
};
diff --git a/test/simple/test-net-error-twice.js b/test/simple/test-net-error-twice.js
new file mode 100644
index 000000000..5435930a2
--- /dev/null
+++ b/test/simple/test-net-error-twice.js
@@ -0,0 +1,50 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+var net = require('net');
+
+var buf = new Buffer(2 * 1024 * 1024);
+
+buf.fill(0x62);
+
+var errs = [];
+
+var srv = net.createServer(function onConnection(conn) {
+ conn.write(buf);
+ conn.on('error', function (err) {
+ errs.push(err);
+ if (errs.length > 1 && errs[0] === errs[1])
+ assert(false, "We should not be emitting the same error twice");
+ });
+}).listen(common.PORT, function () {
+ var client = net.connect({ port: common.PORT });
+
+ client.on('connect', function () {
+ client.resume();
+ client.destroy();
+ });
+}).unref();
+
+process.on('exit', function() {
+ assert.equal(errs.length, 1);
+});