Core: Unable to start kestrel. System.IO.IOException: Failed to bind to address https://127.0.0.1:5001: address already in use.

Created on 23 Dec 2019  路  5Comments  路  Source: dotnet/core

I'm trying to run my project (that was working locally) on an archlinux VPS with .net core 3.1 installed, but I'm getting address in use errors. I've checked port usage and they were not in use..
ports 5001 and 5000 were also not released after this happened and I had to kill the process manually

crit: Microsoft.AspNetCore.Server.Kestrel[0] Unable to start Kestrel. System.IO.IOException: Failed to bind to address https://127.0.0.1:5001: address already in use. ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use ---> System.Net.Sockets.SocketException (98): Address already in use at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)

Most helpful comment

So here is the solution, if you are working on linux

while running the dotnet run if you stop the server using ctr + z then it _Ctrl+z_ sends the SIGTSTP (Signal Tty SToP) signal to the foreground job but don't actually stop the running kestrel server, so you have to mannualy end the process of the kestrel server.
SOLLUTION Only use ctr + c to stop the kestrel server, actually its even displayed on the shell while you run dotnet run.
And use only 5000 port inside launchSettings.json in kestrel (one with the project name) profile

To mannually kill the kestrel server use this command to see the running process and it pid on the port 5000

sudo lsof -iTCP -sTCP:LISTEN -P | grep :5000

then kill it with

sudo kill -9 PID

All 5 comments

output of sudo netstat -tunlp before running the app
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5355 0.0.0.0:* LISTEN 408/systemd-resolve tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 408/systemd-resolve tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1155/sshd tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 457/postgres tcp6 0 0 :::3306 :::* LISTEN 478/mysqld tcp6 0 0 :::5355 :::* LISTEN 408/systemd-resolve tcp6 0 0 :::22 :::* LISTEN 1155/sshd tcp6 0 0 :::3000 :::* LISTEN 566/gitea tcp6 0 0 ::1:5432 :::* LISTEN 457/postgres udp 0 0 0.0.0.0:5355 0.0.0.0:* 408/systemd-resolve udp 0 0 127.0.0.53:53 0.0.0.0:* 408/systemd-resolve udp6 0 0 :::5355 :::* 408/systemd-resolve

none of the suggestions in #948 and #2001 helped..

there was another dotnet app running in a docker container that autostarts 4-5 seconds after I killed it..
closing.

So here is the solution, if you are working on linux

while running the dotnet run if you stop the server using ctr + z then it _Ctrl+z_ sends the SIGTSTP (Signal Tty SToP) signal to the foreground job but don't actually stop the running kestrel server, so you have to mannualy end the process of the kestrel server.
SOLLUTION Only use ctr + c to stop the kestrel server, actually its even displayed on the shell while you run dotnet run.
And use only 5000 port inside launchSettings.json in kestrel (one with the project name) profile

To mannually kill the kestrel server use this command to see the running process and it pid on the port 5000

sudo lsof -iTCP -sTCP:LISTEN -P | grep :5000

then kill it with

sudo kill -9 PID

This is a problem we have a main domain (abc.com) running and then try to run subdomain.abc.com. I noticed you can't run BOTH at the same time, or even back to back (on Linux) with a browser without manually intervening and killing the process. I have in my Program.cs

            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>()
                .UseUrls("http://localhost:5001")
                ;
            });

Should the UseUrls be taken out? Does kestrel know to listen automatically on 5001?

Is there a way to handle this without manual intervention?

It is also a constant problem if you try to start 2 UNITS that start a dotnet process for a different app and do

sudo systemctl status myfirst.service <--- works
sudo systemctl status mysecond.service <------ FAILS with the error

I need both running

Was this page helpful?
0 / 5 - 0 ratings