{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/node/issues/6188","repository_url":"https://api.github.com/repos/nodejs/node","labels_url":"https://api.github.com/repos/nodejs/node/issues/6188/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/node/issues/6188/comments","events_url":"https://api.github.com/repos/nodejs/node/issues/6188/events","html_url":"https://github.com/nodejs/node/issues/6188","id":148198353,"number":6188,"title":"net: bind TCP server after close() has run","user":{"login":"trevnorris","id":289174,"avatar_url":"https://avatars.githubusercontent.com/u/289174?v=3","gravatar_id":"","url":"https://api.github.com/users/trevnorris","html_url":"https://github.com/trevnorris","followers_url":"https://api.github.com/users/trevnorris/followers","following_url":"https://api.github.com/users/trevnorris/following{/other_user}","gists_url":"https://api.github.com/users/trevnorris/gists{/gist_id}","starred_url":"https://api.github.com/users/trevnorris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trevnorris/subscriptions","organizations_url":"https://api.github.com/users/trevnorris/orgs","repos_url":"https://api.github.com/users/trevnorris/repos","events_url":"https://api.github.com/users/trevnorris/events{/privacy}","received_events_url":"https://api.github.com/users/trevnorris/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/nodejs/node/labels/good%20first%20contribution","name":"good first contribution","color":"bfdadc"},{"url":"https://api.github.com/repos/nodejs/node/labels/net","name":"net","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-13T22:14:46Z","updated_at":"2016-04-14T22:24:27Z","closed_at":null,"body":"Version: >= v4.x\r\nPlatform: All\r\nSubsystem: net\r\n\r\nWhen a hostname is passed to `.listen()` it must do a DNS lookup. This means that `server._handle` cannot be set immediately. So when `.close()` is called it sees there's no `_handle` and considers the call a noop.\r\n\r\nInstead it should check if a DNS request is in transit and set a flag or some such so that when the DNS request completes it can check that the server is closed and not bother to setup the TCP socket.\r\n\r\nEDIT: Addition to the above solution would be to delay the DNS lookup in a `nextTick()` and check if `.close()` has been run on the handle. This would allow the initial lookup to skip running at all. It would also solve for the edge case that someone runs:\r\n```js\r\nnet.createServer()\r\n  .listen(8080).close()\r\n  .listen(8081).close()\r\n  .listen(8082).close();\r\n```\r\nBy allowing proper toggling of the flag. Instead of running several lookups at the same time. Though a flag would still need to be set in case `close()` ran while the DNS lookup was in transit. Because there is then the conflict of checking after a `nextTick()` and when the DNS lookup completes.\r\n\r\nHere's the test to verify everything is working:\r\n```js\r\nconst net = require('net');\r\n\r\nnet.createServer(() => {}).listen(8080, 'localhost', () => {\r\n  net.connect(8080, () => {\r\n    throw new Error('a connection was made');\r\n  }).on('error', (e) => process._rawDebug('not able to connect:', e.message));\r\n}).close();\r\n```"},"comment":{"url":"https://api.github.com/repos/nodejs/node/issues/comments/210182344","html_url":"https://github.com/nodejs/node/issues/6188#issuecomment-210182344","issue_url":"https://api.github.com/repos/nodejs/node/issues/6188","id":210182344,"user":{"login":"trevnorris","id":289174,"avatar_url":"https://avatars.githubusercontent.com/u/289174?v=3","gravatar_id":"","url":"https://api.github.com/users/trevnorris","html_url":"https://github.com/trevnorris","followers_url":"https://api.github.com/users/trevnorris/followers","following_url":"https://api.github.com/users/trevnorris/following{/other_user}","gists_url":"https://api.github.com/users/trevnorris/gists{/gist_id}","starred_url":"https://api.github.com/users/trevnorris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trevnorris/subscriptions","organizations_url":"https://api.github.com/users/trevnorris/orgs","repos_url":"https://api.github.com/users/trevnorris/repos","events_url":"https://api.github.com/users/trevnorris/events{/privacy}","received_events_url":"https://api.github.com/users/trevnorris/received_events","type":"User","site_admin":false},"created_at":"2016-04-14T22:24:27Z","updated_at":"2016-04-14T22:24:27Z","body":"Awesome thanks."}}