Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix.EADDRINUSE #139

Closed
idkjs opened this issue Jul 21, 2021 · 19 comments
Closed

Unix.EADDRINUSE #139

idkjs opened this issue Jul 21, 2021 · 19 comments
Labels

Comments

@idkjs
Copy link
Contributor

idkjs commented Jul 21, 2021

I was running https://github.com/jsthomas/sensors from @jsthomas and came across the following error after hitting control c. How does one clear this error out?

❯ dune exec ./server/bin/server.exe
21.07.21 14:04:00.318                       Running on 0.)
21.07.21 14:04:00.319                       Type Ctrl+C tp
21.07.21 14:04:00.321      dream.http ERROR Dream.run: ex)
Fatal error: exception Unix.Unix_error(Unix.EADDRINUSE, "bind", "")

Thanks.

@jsthomas
Copy link
Contributor

Interesting. From your excerpt it looks like this error happened immediately after you tried to start the server. Is that correct? EADDRINUSE can occur if some other service is already using the port that server.exe is configured to use (by default, that's 8080). Perhaps you could check for existing processes listening on 8080 with netstat?

@aantron
Copy link
Owner

aantron commented Jul 21, 2021

I can't immediately think of what might be calling bind after Ctrl+C. @jsthomas, I don't see anything in sensors that would do that, after looking admittedly for only one minute. Any ideas?

@thangngoc89
Copy link
Contributor

I got this error so often that I decided to add simple script to kill the port before starting dream server. Simple and effective.

@jsthomas
Copy link
Contributor

@aantron : Unfortunately, no. I've tried to reproduce this on two different machines (Debian and MacOS), and thus far the only way I get an error like the one listed above is if I try to run two copies of the server process at once.

@thangngoc89 : When you wrote

I got this error so often that...

Could you clarify, is this specific to the example project (sensors), or working with Dream in general? I'd like to understand if there should be a more user friendly set of scripts/commands on the example project.

@aantron
Copy link
Owner

aantron commented Jul 21, 2021

@idkjs, @thangngoc89 what OS are you on?

Can you give steps to reproduce this?

@idkjs
Copy link
Contributor Author

idkjs commented Jul 21, 2021

@aantron here is the reproduction. https://github.com/idkjs/sensors.

❯ sw_vers
ProductName:    macOS
ProductVersion: 11.4
BuildVersion:   20F71

You run it. It works. Then you decide to do something to it. You CTRL +C to stop the project. dune build -w. Do some things and recompile. Then you run dune exec ./server/bin/server.exe again, and you get the error.

@jsthomas It is not impossible that I did something like that, so I closed any editors, checked to see if there was anything on the port, there was nothing, and I ran it again. Same thing.

@jsthomas
Copy link
Contributor

@idkjs : I wonder if the problem is that I set the server up to listen on all interfaces. Do you encounter the same problem if you change Dream.run ~interface:"0.0.0.0" to just Dream.run on this line?

@aantron
Copy link
Owner

aantron commented Jul 21, 2021

Have not been able to observe it on

Linux MSI 4.4.0-19041-Microsoft #488-Microsoft Mon Sep 01 13:43:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux

In particular, with commit idkjs/sensors@fb7d214, I did:

git clone https://github.com/idkjs/sensors.git
cd sensors
opam update
opam switch create . ocaml-base-compiler.4.11.1 --deps-only --locked
eval (opam env)
dune exec ./server/bin/server.exe
[Ctrl+C]
dune build -w
[make some edits and trigger a bunch of rebuilds]
[Ctrl+C]
dune exec ./server/bin/server.exe
[Ctrl+C]

all was well. I also tried running the server while dune build -w was still running, and again, all worked.

@thangngoc89 what system are you on?

@aantron
Copy link
Owner

aantron commented Jul 21, 2021

Also @jsthomas, what system are you using?

@idkjs
Copy link
Contributor Author

idkjs commented Jul 21, 2021

@aantron I am able to reproduce your results and not mine though now I am getting another error that seems unrelated maybe.

This machine is weird today. I can't install melange anywhere at the moment either

Details ```sh /Github/sensors main 4m 16s  3.0.1 ❯ eval (opam env)

~/Github/sensors main  3.0.1
❯ dune exec ./server/bin/server.exe
21.07.21 21:26:04.352 Running on 0.0.0.0:8080 (http://localhost:8080)
21.07.21 21:26:04.352 Type Ctrl+C to stop

~/Github/sensors main ✘ INT 6s  3.0.1
❯ dune exec ./server/bin/server.exe
21.07.21 21:26:09.804 Running on 0.0.0.0:8080 (http://localhost:8080)
21.07.21 21:26:09.804 Type Ctrl+C to stop

~/Github/sensors main ✘ INT 4s  3.0.1
❯ dune exec ./server/bin/server.exe
21.07.21 21:26:16.972 Running on 0.0.0.0:8080 (http://localhost:8080)
21.07.21 21:26:16.973 Type Ctrl+C to stop

~/Github/sensors main ✘ INT 1m 45s  3.0.1
❯ dune exec ./server/bin/server.exe
21.07.21 21:28:04.462 Running on 0.)
21.07.21 21:28:04.463 Type Ctrl+C tp

~/Github/sensors main ✘ INT 36s  3.0.1
❯ dune exec ./server/bin/server.exe
21.07.21 21:28:42.642 Running on 0.0.0.0:8080 (http://loc)
21.07.21 21:28:42.642 Type Ctrl+C to stop
21.07.21 21:28:54.714 dream.log INFO REQ 1 GET / 127.0.0.1:58768 Mozilla0
21.07.21 21:28:54.786 dream.log WARN REQ 1 Aborted by: Failed to connectt

21.07.21 21:28:54.786 dream.log WARN REQ 1 Raised at Dream__middleware__3
21.07.21 21:28:54.786 dream.http ERROR Failed to connect to <postgresql://t

21.07.21 21:28:54.786 dream.http ERROR Raised at Stdlib__map.Make.find in 5
21.07.21 21:28:54.786 dream.http ERROR Called from Logs.Tag.find in file "2
21.07.21 21:28:54.824 dream.log INFO REQ 2 GET /favicon.ico 127.0.0.1:580
21.07.21 21:28:54.831 dream.log WARN REQ 2 Aborted by: Failed to connectt

21.07.21 21:28:54.831 dream.log WARN REQ 2 Raised at Dream__middleware__3
21.07.21 21:28:54.831 dream.http ERROR Failed to connect to <postgresql://t

21.07.21 21:28:54.831 dream.http ERROR Raised at Stdlib__map.Make.find in 5
21.07.21 21:28:54.831 dream.http ERROR Called from Logs.Tag.find in file "2
21.07.21 21:30:50.296 dream.log INFO REQ 3 POST /api/login 127.0.0.1:5871
21.07.21 21:30:50.329 dream.log WARN REQ 3 Aborted by: Failed to connectt

21.07.21 21:30:50.329 dream.log WARN REQ 3 Raised at Dream__middleware__3
21.07.21 21:30:50.329 dream.http ERROR Failed to connect to <postgresql://t

21.07.21 21:30:50.329 dream.http ERROR Raised at Stdlib__map.Make.find in 5
21.07.21 21:30:50.329 dream.http ERROR Called from Logs.Tag.find in file "2
21.07.21 21:30:56.497 dream.log INFO REQ 4 POST /api/sensor/ 127.0.0.1:51
21.07.21 21:30:56.506 dream.log WARN REQ 4 Aborted by: Failed to connectt

21.07.21 21:30:56.506 dream.log WARN REQ 4 Raised at Dream__middleware__3
21.07.21 21:30:56.506 dream.http ERROR Failed to connect to <postgresql://t

21.07.21 21:30:56.506 dream.http ERROR Raised at Stdlib__map.Make.find in 5
21.07.21 21:30:56.506 dream.http ERROR Called from Logs.Tag.find in file "2

~/Github/sensors main ✘ INT 2m 49s  3.0.1
❯ dune exec ./server/bin/server.exe
21.07.21 21:31:34.211 Running on 0.0.0.0:8080 (http://localhost:8080)
21.07.21 21:31:34.211 Type Ctrl+C to stop
21.07.21 21:31:40.941 dream.log INFO REQ 1 POST /api/sensor/ 127.0.0.1:58798 insomnia/21
21.07.21 21:31:40.970 dream.log WARN REQ 1 Aborted by: Failed to connect to <postgresqlt

21.07.21 21:31:40.970 dream.log WARN REQ 1 Raised at Dream__middleware__Session.middlew3
21.07.21 21:31:40.970 dream.http ERROR Failed to connect to <postgresql://[email protected]

21.07.21 21:31:40.970 dream.http ERROR Raised at Stdlib__map.Make.find in file "map.ml", 5
21.07.21 21:31:40.970 dream.http ERROR Called from Logs.Tag.find in file "src/logs.ml", l2
21.07.21 21:31:50.169 dream.log INFO REQ 2 GET /api/sensor/1/readings?start=2020-11-01&1
21.07.21 21:31:50.176 dream.log WARN REQ 2 Aborted by: Failed to connect to <postgresqlt

21.07.21 21:31:50.176 dream.log WARN REQ 2 Raised at Dream__middleware__Session.middlew3
21.07.21 21:31:50.176 dream.http ERROR Failed to connect to <postgresql://[email protected]

21.07.21 21:31:50.176 dream.http ERROR Raised at Stdlib__map.Make.find in file "map.ml", 5
21.07.21 21:31:50.177 dream.http ERROR Called from Logs.Tag.find in file "src/logs.ml", l2

</details>

@idkjs
Copy link
Contributor Author

idkjs commented Jul 21, 2021

@thangngoc89 what's your script?

@jsthomas
Copy link
Contributor

The systems I tested against are

Linux 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5+deb10u2 (2019-08-08) x86_64 GNU/Linux

and

Darwin Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64 x86_64

@aantron
Copy link
Owner

aantron commented Jul 21, 2021

Forgot to add, disto is

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:        20.04
Codename:       focal

@thangngoc89
Copy link
Contributor

Let’s me clarify this. I often have this problem when I develop my app in watch mode using watchexec/fswatch/whatever/….
This is usually happen if I save multiple files too fast. So I decided to add a script that would kill the process that occupying the port before starting the application.

@aantron
Copy link
Owner

aantron commented Jul 22, 2021

Let’s me clarify this. I often have this problem when I develop my app in watch mode using watchexec/fswatch/whatever/….
This is usually happen if I save multiple files too fast. So I decided to add a script that would kill the process that occupying the port before starting the application.

To comment on something similar, VSCode usually triggers dune build -w and most other watchers twice in a row on my system. @idkjs is it possible that you were starting the server using a watcher, but the watcher was running more times than you expected? If so, what watcher are you using? What editor are you using?

@thangngoc89 The script could still be very useful as we eventually develop some kind of "integrated" watcher, rather than just offer a slightly janky shell script in an example :) Would you mind sharing it in a gist or the like?

@aantron
Copy link
Owner

aantron commented Jul 22, 2021

@thangngoc89 I'd also like to, if possible and you have time, to try to reproduce your version of this issue.

@thangngoc89
Copy link
Contributor

thangngoc89 commented Jul 22, 2021

@aantron @idkjs well it's some clever shell script. I think this could be packaged into an opam package (that calls lsof under the hood).

lsof -i tcp:5000 | grep LISTEN | awk '{print $2}' | xargs kill -9 && dune exec ./src/main.exe

@idkjs
Copy link
Contributor Author

idkjs commented Jul 22, 2021

. @idkjs is it possible that you were starting the server using a watcher

Given your question. Its entirely possible that I had the app running in one terminal then dune build -w in another. Then, after green in watch terminal, I CTRL+c in running terminal and restarted main.exe.

@aantron
Copy link
Owner

aantron commented Jul 29, 2021

I'm closing this for now, as it seems we don't have an issue to debug here. Please do comment if you can reproduce this in some way.

I moved the watchexec/fswatch-related command line from @thangngoc89 to #81 (comment).

@aantron aantron closed this as completed Jul 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants