Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix: handle listen error (#224)
Browse files Browse the repository at this point in the history
The `net.Server` `.listen` callback is not a node-style callback that gets passed an error, it's a listener for the `listen` event.

To handle errors when `.listen`ing, one must listen for the `error` event, so update the code to do that and add a test to prevent regressions.
  • Loading branch information
achingbrain authored Nov 5, 2022
1 parent 2778c38 commit 4125e9e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene

return await new Promise<void>((resolve, reject) => {
const options = multiaddrToNetConfig(listeningAddr)
this.server.listen(options, (err?: any) => {
if (err != null) {
return reject(err)
}
this.server.on('error', (err) => {
reject(err)
})
this.server.listen(options, () => {
log('Listening on %s', this.server.address())
resolve()
})
Expand Down
16 changes: 16 additions & 0 deletions test/listen-dial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ describe('listen', () => {
await listener.listen(mh)
})

it('errors when listening on busy port', async () => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
listener = transport.createListener({
upgrader
})
await listener.listen(mh)

const listener2 = transport.createListener({
upgrader
})

const mh2 = listener.getAddrs()[0]
await expect(listener2.listen(mh2)).to.eventually.be.rejected()
.with.property('code', 'EADDRINUSE')
})

it('listen on IPv6 addr', async () => {
if (isCI != null) {
return
Expand Down

0 comments on commit 4125e9e

Please sign in to comment.