From b24e269a482812193fb3cd8b6ced4477f8e5e1c5 Mon Sep 17 00:00:00 2001 From: Eduard Bondarenko Date: Mon, 22 May 2017 13:35:22 +0300 Subject: [PATCH] net: multiple listen() events fail silently Problem: It's possible to run listen() on a net.Server that's already listening to a port. The result is silent failure, with the side effect of changing the connectionKey and or pipeName. Solution: throw an error if listen method called more than once. close() method should be called between listen() method calls. Refs: https://github.com/nodejs/node/pull/8294 Fixes: https://github.com/nodejs/node/issues/6190 Fixes: https://github.com/nodejs/node/issues/11685 PR-URL: https://github.com/nodejs/node/pull/13149 Reviewed-By: Matteo Collina Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Joyee Cheung --- doc/api/http.md | 15 +++--- doc/api/net.md | 6 +-- lib/internal/errors.js | 2 + lib/net.js | 4 ++ ...t-net-server-call-listen-multiple-times.js | 51 +++++++++++++++++++ 5 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 test/parallel/test-net-server-call-listen-multiple-times.js diff --git a/doc/api/http.md b/doc/api/http.md index 2a2a87ce60..c0fe0e72bc 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -809,8 +809,9 @@ This function is asynchronous. `callback` will be added as a listener for the Returns `server`. -*Note*: The `server.listen()` method may be called multiple times. Each -subsequent call will *re-open* the server using the provided options. +*Note*: The `server.listen()` method can be called again if and only if there was an error +during the first `server.listen()` call or `server.close()` has been called. +Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error will be thrown. ### server.listen(path[, callback])