From dee5270a6c093db32897d45c4ed18f56bd772987 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sun, 9 Feb 2014 14:59:31 +0400 Subject: [PATCH] net: do not re-emit stream errors fix #7015 --- lib/_stream_writable.js | 4 +++ lib/net.js | 5 ++- test/simple/test-net-error-twice.js | 50 +++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 test/simple/test-net-error-twice.js diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 403cb7b477..257a400fa5 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 31de90cb02..047f8cd2f0 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 0000000000..5435930a24 --- /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); +});