Skip to content

Instantly share code, notes, and snippets.

@stelcheck
Last active June 15, 2017 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stelcheck/7c57c5b56748302d2aa71e8bce95f376 to your computer and use it in GitHub Desktop.
Save stelcheck/7c57c5b56748302d2aa71e8bce95f376 to your computer and use it in GitHub Desktop.
net: EADDRINUSE is an uncatchable error
const net = require('net')
try {
net.createServer((args) => console.log(...args)).listen('hello', (error) => console.error('listen error', error))
} catch (error) {
console.error('Uncaught listen error', error)
}
@stelcheck
Copy link
Author

To reproduce, run once, CTRL-C, then run once again. You should see the following output on the second run

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE hello
    at Object.exports._errnoException (util.js:1036:11)
    at exports._exceptionWithHostPort (util.js:1059:20)
    at Server._listen2 (net.js:1239:19)
    at listen (net.js:1288:10)
    at Server.listen (net.js:1377:5)
    at Object.<anonymous> (/private/tmp/listen/index.js:4:52)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)

As you can see, the error is neither caught by the try/catch nor passed to the listen callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment