From a848a3efbfeb70181a95383e022474fdf2c83aae Mon Sep 17 00:00:00 2001 From: koichik Date: Wed, 28 Dec 2011 15:13:57 +0900 Subject: [PATCH] net: fix Socket.pause null reference when called on a closed Stream Fixes #1980. --- lib/net.js | 9 +++-- test/simple/test-net-after-close.js | 52 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 test/simple/test-net-after-close.js diff --git a/lib/net.js b/lib/net.js index b6697136d2f..072896b4c4e 100644 --- a/lib/net.js +++ b/lib/net.js @@ -166,7 +166,10 @@ Socket.prototype.setKeepAlive = function(setting, msecs) { Socket.prototype.address = function() { - return this._handle.getsockname(); + if (this._handle && this._handle.getsockname) { + return this._handle.getsockname(); + } + return null; }; @@ -189,7 +192,9 @@ Object.defineProperty(Socket.prototype, 'readyState', { Object.defineProperty(Socket.prototype, 'bufferSize', { get: function() { - return this._handle.writeQueueSize + this._connectQueueSize; + if (this._handle) { + return this._handle.writeQueueSize + this._connectQueueSize; + } } }); diff --git a/test/simple/test-net-after-close.js b/test/simple/test-net-after-close.js new file mode 100644 index 00000000000..65fda21900d --- /dev/null +++ b/test/simple/test-net-after-close.js @@ -0,0 +1,52 @@ +// 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 closed = false; + +var server = net.createServer(function(s) { + s.end(); +}); + +server.listen(common.PORT, function() { + var c = net.createConnection(common.PORT); + c.on('close', function() { + assert.strictEqual(c._handle, null); + closed = true; + assert.doesNotThrow(function() { + c.setNoDelay(); + c.setKeepAlive(); + c.bufferSize; + c.pause(); + c.resume(); + c.address(); + c.remoteAddress; + c.remotePort; + }); + server.close(); + }); +}); + +process.on('exit', function() { + assert(closed); +});