Skip to content

Commit

Permalink
listen to 'error' event on the server until listen has finished
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Sep 12, 2017
1 parent a8ec8ea commit 4d6baea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions fastify.js
Expand Up @@ -170,13 +170,26 @@ function build (options) {
if (listening) {
return _cb(new Error('Fastify is already listening'))
}

server.on('error', wrap)
if (hasAddress) {
server.listen(port, address, _cb)
server.listen(port, address, wrap)
} else {
server.listen(port, _cb)
server.listen(port, wrap)
}
listening = true
})

function wrap (err) {
server.removeListener('error', wrap)
if (_cb) {
_cb(err)
} else {
// this will crash the process
// it will go to 'uncaughtException'
throw err
}
}
}

function startHooks (req, res, params, store) {
Expand Down
13 changes: 13 additions & 0 deletions test/listen.test.js
Expand Up @@ -72,3 +72,16 @@ test('double listen errors', t => {
})
})
})

test('listen twice on the same port', t => {
t.plan(2)
const fastify = Fastify()
fastify.listen(0, (err) => {
t.error(err)
const s2 = Fastify()
s2.listen(fastify.server.address().port, (err) => {
fastify.close()
t.ok(err)
})
})
})

0 comments on commit 4d6baea

Please sign in to comment.