Core: Failed to bind to address https://127.0.0.1:5001: address already in use

Created on 16 Oct 2018  路  38Comments  路  Source: dotnet/core

Trying to run a freshly created project I get the following. Seems like a race condition because port 5001 is not in use on my system.

[41mcrit: 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: Address already in use
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransport.BindAsync()
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransport.BindAsync()
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.<>c__DisplayClass22_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding)
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
Microsoft.AspNetCore.Server.Kestrel:Critical: 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: Address already in use
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransport.BindAsync()
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransport.BindAsync()
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.<>c__DisplayClass22_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IServerAddressesFeature addresses, KestrelServerOptions serverOptions, ILogger logger, Func`2 createBinding)
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
The thread 1215731 has exited with code 0 (0x0).
The thread 1215727 has exited with code 0 (0x0).
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll

Most helpful comment

If people are still commenting on a closed issue, it's probably because it's what they are most directed to which likely means the issue shouldn't be closed in the first place. We have 948 (closed), this ticket (closed). I'm not sure opening yet another one makes sense.

For those who want another experience report: this exact same issue currently happens to me all the time as well, with a fresh install of dotnet. The project is here:

https://github.com/jeffnyman/testing-aspnet-core

Running dotnet run works just fine with this project on Windows, not at all on Mac (either Sierra, High Sierra or Mojave).

It initially tells me port 5000 is in use. It's not; I've checked. But I did run the kill steps on anything on the port as provided by seba-i. I then added the following line to Startup.cs:

.UseUrls("http://*:5001")

When I do that, then it tells me port 5001 is already in use. I realize the issue is closed but a lot of people are going to probably end up here as they experiment.

All 38 comments

There is already this issue logged. Did you check the solution of checking Port Usage (by some other application) and killing it?

https://github.com/dotnet/core/issues/948

Are you on HTTPS on localhost?

Duplicate of #948

Okay, but what is the solution?

@jenyckee did you check the primary issue?

@karelz Yes, when I "Start Without Debugging" the application even crashes

This also happens to me

I had the same issue on a dotnet generate web project recently. But I can fix the issue by editing Properties\launchSettings.json to modify the applicationUrl in your app config.

From this

"applicationUrl": "https://localhost:5001;http://localhost:5000",

to this

"applicationUrl": "http://localhost:5000",

I suspect it could be an issue of the dotnet boilerplate project "ASP.NET Core Web API".

@reevelau This doesn't help for me

@jenyckee did you check the primary issue?
...
Yes, when I "Start Without Debugging" the application even crashes

See https://github.com/dotnet/core/issues/948#issuecomment-336284252 ... unless the right team knows about the problem and understands the root-cause, they can't provide solutions.

@karelz That's very unfortunate, what kind of information do you need in order to understand the root-cause?

@jenyckee please read the link https://github.com/dotnet/core/issues/948#issuecomment-336284252 - it tells you where to file the bug (aka where to find the right team to talk to). No information you provide here or on the #948 bug is going to help as it won't reach the expert.

I had the same issue on a dotnet generate web project recently. But I can fix the issue by editing Properties\launchSettings.json to modify the applicationUrl in your app config.

From this

"applicationUrl": "https://localhost:5001;http://localhost:5000",

to this

"applicationUrl": "http://localhost:5000",

I suspect it could be an issue of the dotnet boilerplate project "ASP.NET Core Web API".

Still can't

{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5001",
"sslPort": 5001
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"johnTestBO": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

In my case the reason was that I had a web site on IIS bound to use https://localhost:5001

Funny enough "netstat -o" didn't show it

on CentOS 7 锛孖 had the same issue on a dotnet generate web project recently.
image
image

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56271/",
      "sslPort": 0
    }
  },

  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Logging": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "CategoryProjectCore": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  },
  "ConnectionStrings": {
    "SopContext": "server=127.0.0.1;port=3306;database=sopcce;uid=root;pwd=123456;Charset=utf8mb4;SslMode = none;"
  }
}

I do not know ,try other way
#948 not help me.

@Sopcce please do not comment on close issues. We rarely monitor them.
I would suggest to file a new issue with details - it would be good to summarize what EXACTLY you tried that did not help you. The issues are now too long and too messy to make heads and tails clearly. Thanks!

If people are still commenting on a closed issue, it's probably because it's what they are most directed to which likely means the issue shouldn't be closed in the first place. We have 948 (closed), this ticket (closed). I'm not sure opening yet another one makes sense.

For those who want another experience report: this exact same issue currently happens to me all the time as well, with a fresh install of dotnet. The project is here:

https://github.com/jeffnyman/testing-aspnet-core

Running dotnet run works just fine with this project on Windows, not at all on Mac (either Sierra, High Sierra or Mojave).

It initially tells me port 5000 is in use. It's not; I've checked. But I did run the kill steps on anything on the port as provided by seba-i. I then added the following line to Startup.cs:

.UseUrls("http://*:5001")

When I do that, then it tells me port 5001 is already in use. I realize the issue is closed but a lot of people are going to probably end up here as they experiment.

fix the issue by editing Properties\launchSettings.json to modify the applicationUrl in your app config.

From this

"applicationUrl": "https://localhost:5001;http://localhost:5000",

to this

"applicationUrl": "http://localhost:5000",

I suspect it could be an issue of the dotnet boilerplate project "ASP.NET Core Web API".

I just tried to create a new project on .net core 2.2 and had the same issue.
dotnet new mvc --auth Individual -o AspNetCoreTodo
dotnet run

making this change worked for me.

If you use Visual Studio Code you need to close application in terminal. Maybe you run your application using dotnet run command. Then it started. Even you close your tab in browser it still works. You need to go to terminal tab in your Visual Studio Code and then press "ctrl + c" to stop the application. After that you should have problem with starting it using debug or withoud debug command.

I solved it here by deleting the "applicationUrl" in launchSettings.json
image

I created a new project in VSCode with dotnet new mvc command
Then run the app through terminal using dotnet run.
It just froze after a few info statements.
I ran using the debugger.
Gave me the error - Failed to bind to address https://127.0.0.1:5001: address already in use.

Here's what I did -
Stopped the debugger.
Hit Ctrl+C on terminal.
Reran using the Debugger.
Worked fine for me.

You need just Kill all networks.... adapters... and run again - then all will be good.

I 'm not sure if someone else has been facing the same issue. If that is the case, simply end the .Net Core Task running on your computer. Run your dotnet App afterwards.

None of the previous suggestions have worked for me so far. I've tried running and using ctrl + c in the terminal like MayurSapaliga suggested. I've edited the applicationUrl in the launchSettings.json file like tuliopaim suggested. I've edited the Program.cs file as suggested on the Microsoft docs page here https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-3.1

None of this works. I tried changing the port my app runs on, but I keep getting "No webpage was found for the web address: http://localhost:5000/", and the same message for every other port I tried, which include 5002, 5003, 5004, 5005, 5050, 5051, and 5052. I have no idea which ports are the common second choice for such purposes. I basically just trying random ports I think aren't in use.

All of this happened because I started a new dotnet project. I ran it a couple of times, and then went back to the previous project I was working on, and now it absolutely will not run. I can go back to the other project (the new one I started), and it runs fine. It looks like VS Code just will not release the port. I've done ctrl + c both in VS Code and in the terminal. Interestingly enough, dotnet run works in VS Code (for the new program that's causing this whole mess), but the same command won't run in the terminal. I'll get the same error I get when trying to run my first program,

Unable to start Kestrel.
System.IO.IOException: Failed to bind to address http://127.0.0.1:5000: address already in use.

I've shut down my computer and turned off my router as well. I've killed the processes running on the ports. Absolutely nothing works. What do you want from me, VS Code?!?!?!

Not sure how this would be relevant, but it may be worth mentioning that I'm running Linux Mint 18.

Eu troquei de porta, e funcionou:
Antes: "applicationUrl": "http://localhost:5000",
Depois: "applicationUrl": "http://localhost:5001",

For those running dotnet through the CLI (and linux) and not properly closing the process, I did basic CLI steps to kill dotnet. Type ps aux | grep dotnet or just ps and you'll get the PIDs of the kestrel processes running. Then type, kill -9 <PID> where <PID> is your process ID.

Just make sure in the future to use ctrl-c when you're done with dotnet run as that's the proper way to close the process from the CLI.

Damn, I had my firewall turned off and when I turned it back on it started to work. Weird.

I had the same issue on a dotnet generate web project recently. But I can fix the issue by editing Properties\launchSettings.json to modify the applicationUrl in your app config.

From this

"applicationUrl": "https://localhost:5001;http://localhost:5000",

to this

"applicationUrl": "http://localhost:5000",

I suspect it could be an issue of the dotnet boilerplate project "ASP.NET Core Web API".
This fixed my issue it looks like it tries to start this on http and https not sure though.

Kill dotnet host in Task Manager and it worked.

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.

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

why make this simple thing so complicated ???
why not just provide a server.listen(port) API, so not the framework don't need to care other stuffs. and in user land code, we can do the config stuff or hard code a port here

I had the same issue on a dotnet generate web project recently. But I can fix the issue by editing Properties\launchSettings.json to modify the applicationUrl in your app config.

From this

"applicationUrl": "https://localhost:5001;http://localhost:5000",

to this

"applicationUrl": "http://localhost:5000",

I suspect it could be an issue of the dotnet boilerplate project "ASP.NET Core Web API".

that really helped! thank you

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.

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

fix the issue. thanks

My issue was caused by a docker container keeping a connection in a CLOSE_WAIT state

Using Windows 10 with .NET Core 2.1 web api.

I tried killing the PID I found with netstat -aon | more, but received the message ERROR: The process "xxxx" not found.

After hours of investigation, netstat -b yielded a clue:
image

Docker Desktop comes with a standalone Kubernetes server and client, so I tried restarting Docker Desktop.

Fixed it right away. Hope it helps someone !

This is a problem with the Windows operating system. You can use commands
netsh interface ipv4 show excludedportrange protocol=tcp
to view the ports reserved by the system. Note that these reserved ports are not static. It will change with windows updates.You may find that there are 5000 or 5001 in the reserved ports

Solved

About a day ago, I was using docker.

docker run -p 5000:5003 tag/name

its was listening on that port.

Now typing

docker ps

and guess what... the container was still up (for 17 hours lol), I never shut it down.

docker kill ContainerID

helped then I could run it

thanks to you'll!. the problem is your https request is insecure all you need do is go to your web browser and click on the advance button then click continue, or check for a running project on your terminal stop it and the run it again thank you

I had the same issue on a dotnet generate web project recently. But I can fix the issue by editing Properties\launchSettings.json to modify the applicationUrl in your app config.

From this

"applicationUrl": "https://localhost:5001;http://localhost:5000",

to this

"applicationUrl": "http://localhost:5000",

I suspect it could be an issue of the dotnet boilerplate project "ASP.NET Core Web API".

That actually did help me, thanks!

"applicationUrl": "http://localhost:5000",

This worked! Thanks for the tip!

Was this page helpful?
0 / 5 - 0 ratings