Skip to content

Commit

Permalink
listen() cannot be called multiple times.
Browse files Browse the repository at this point in the history
  • Loading branch information
justsml committed Apr 18, 2016
1 parent 4a74fc9 commit c9fa9ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ function Server(options, connectionListener) {
throw new TypeError('options must be an object');
}

this._listenHasBeenCalled = false;
this._connections = 0;

Object.defineProperty(this, 'connections', {
Expand Down Expand Up @@ -1300,6 +1301,11 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {
Server.prototype.listen = function() {
var self = this;

if (self._listenHasBeenCalled)
throw new Error('listen() cannot be called multiple times.');

self._listenHasBeenCalled = true;

var lastArg = arguments[arguments.length - 1];
if (typeof lastArg === 'function') {
self.once('listening', lastArg);
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-net-listen-multiple-calls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const server = net.createServer();

server.on('error', () => {
console.error('server had an error');
});

assert.throws(function() {
server.listen(common.PORT).listen(common.PORT+1);
}, /listen\(\) cannot be called multiple times/i);

process.on('exit', () => {
// Is this needed? assert.equal(server.address().port, 8080);
const key = server._connectionKey;
assert.equal(key.substr(key.lastIndexOf(':')+1), common.PORT);
});

server.close();

0 comments on commit c9fa9ab

Please sign in to comment.