From 4b0e36810a5afddc45c5c069055659137adbfa5f Mon Sep 17 00:00:00 2001 From: Simen Brekken Date: Wed, 12 Oct 2011 13:06:16 +0300 Subject: [PATCH] net: register net.Server callback only once Only register once for listening when passing a callback to Server.listen(), this prevents servers recycled using close() from invoking the callback when Server.listen() is called later. --- lib/net.js | 2 +- .../test-net-server-listen-remove-callback.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/simple/test-net-server-listen-remove-callback.js diff --git a/lib/net.js b/lib/net.js index 561ebaec1f..8404d6f44c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -698,7 +698,7 @@ Server.prototype.listen = function() { var lastArg = arguments[arguments.length - 1]; if (typeof lastArg == 'function') { - self.addListener('listening', lastArg); + self.once('listening', lastArg); } var port = toPort(arguments[0]); diff --git a/test/simple/test-net-server-listen-remove-callback.js b/test/simple/test-net-server-listen-remove-callback.js new file mode 100644 index 0000000000..50733da9e4 --- /dev/null +++ b/test/simple/test-net-server-listen-remove-callback.js @@ -0,0 +1,19 @@ +var common = require('../common'); +var assert = require('assert'); +var net = require('net'); + +// Server should only fire listen callback once +var server = net.createServer(); + +server.on('close', function() { + var listeners = server.listeners('listening'); + console.log('Closed, listeners:', listeners.length); + assert.equal(0, listeners.length); +}); + +server.listen(3000, function() { + server.close(); + server.listen(3001, function() { + server.close(); + }); +});