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

Cannot catch EADDRINUSE #184

Closed
c7hm4r opened this issue Sep 20, 2022 · 4 comments
Closed

Cannot catch EADDRINUSE #184

c7hm4r opened this issue Sep 20, 2022 · 4 comments
Assignees
Labels
exp/beginner Can be confidently tackled by newcomers good first issue Good issue for new contributors help wanted Seeking public contribution on this issue P2 Medium: Good to have, but can wait until someone steps up

Comments

@c7hm4r
Copy link

c7hm4r commented Sep 20, 2022

Hello! I want to implement that a different port is tried automatically when the currently tried out port is in use. However, it is impossible to catch the EADDRINUSE exception when running await node.start();. NodeJS terminates before node.start() returns a Promise.

Following is output:

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::8000
    at Server.setupListenHandle [as _listen2] (node:net:1485:16)
    at listenInCluster (node:net:1533:12)
    at Server.listen (node:net:1621:7)
    at file:///project/node_modules/it-ws/dist/src/server.js:14:25
    at new Promise (<anonymous>)
    at Server.listen (file:///project/node_modules/it-ws/dist/src/server.js:12:22)
    at WebSocketListener.listen (file:///project/node_modules/@libp2p/websockets/dist/src/listener.js:72:27)
    at DefaultTransportManager.listen (file:///project/node_modules/libp2p/dist/src/transport-manager.js:155:37)
    at DefaultTransportManager.start (file:///project/node_modules/libp2p/dist/src/transport-manager.js:44:20)
    at file:///project/node_modules/@libp2p/components/dist/src/index.js:67:29
Emitted 'error' event on WebSocketServer instance at:
    at Server.emit (node:events:513:28)
    at emitErrorNT (node:net:1512:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 8000
}

Node.js v18.9.0

To me, the issue has a medium severity, because the workaround to check the port usage before is tedious, platform dependent, and cannot be reliable because it is not atomic.

Versions:

  • Node.js: 18.9.0
  • libp2p: 0.39.1
  • @libp2p/websockets: 3.0.3

Possibly related:

@achingbrain
Copy link
Member

You could use 0 for the port number, then read the relevant addr from getMultiaddrs on the node after startup?

Might be a nicer approach than trying (potentially) lots of new sockets until you find a free one?

@c7hm4r
Copy link
Author

c7hm4r commented Sep 21, 2022

Thank you. It would be a partial solution for my problem at hand. But in general, it would be nice to be able to at least print a human-friendly error message instead of confronting the user with some technical stack trace.

@achingbrain
Copy link
Member

I think this should be validated - if a port is in used the error should be catchable, it should not take the process down.

From the error message the WebSocketServer needs a handler listening for the error event.

@achingbrain achingbrain added help wanted Seeking public contribution on this issue good first issue Good issue for new contributors exp/beginner Can be confidently tackled by newcomers labels Oct 5, 2022
@mpetrunic mpetrunic added the P2 Medium: Good to have, but can wait until someone steps up label Nov 15, 2022
@mpetrunic
Copy link
Member

alanshaw/it-ws#34 should fix this but it's not working for some reason

achingbrain pushed a commit that referenced this issue Dec 8, 2022
github-actions bot pushed a commit that referenced this issue Dec 8, 2022
## [5.0.1](v5.0.0...v5.0.1) (2022-12-08)

### Bug Fixes

* cannot catch EADDRINUSE ([#198](#198)) ([c7312db](c7312db)), closes [#184](#184)

### Dependencies

* **dev:** bump @libp2p/interface-mocks from 7.1.0 to 8.0.2 ([#199](#199)) ([daff533](daff533)), closes [#318](https://github.com/libp2p/js-libp2p-websockets/issues/318) [#315](https://github.com/libp2p/js-libp2p-websockets/issues/315) [#313](https://github.com/libp2p/js-libp2p-websockets/issues/313) [#312](https://github.com/libp2p/js-libp2p-websockets/issues/312)
* **dev:** bump it-all from 1.0.6 to 2.0.0 ([#193](#193)) ([6213f8f](6213f8f)), closes [#28](#28) [#28](#28) [#27](#27) [#24](#24)
* **dev:** bump it-drain from 1.0.5 to 2.0.0 ([#191](#191)) ([e549691](e549691)), closes [#28](#28) [#28](#28) [#27](#27) [#24](#24)
* **dev:** bump it-take from 1.0.2 to 2.0.0 ([#192](#192)) ([4c037fc](4c037fc)), closes [#28](#28)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
exp/beginner Can be confidently tackled by newcomers good first issue Good issue for new contributors help wanted Seeking public contribution on this issue P2 Medium: Good to have, but can wait until someone steps up
Projects
None yet
Development

No branches or pull requests

3 participants