0

I have a asp.net core 2.1 application that uses Kestrel to setup a web server. To improve user experience, when the application is started, I also start Chrome. But lately, I've been getting this error while debugging:

System.IO.IOException: Failed to bind to address https://127.0.0.1:1985: address already in use. ---> 

Microsoft.AspNetCore.Connections.AddressInUseException: Only one usage of each socket address (protocol/network address/port) is normally permitted ---> 

System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted

And the only way to prevent this from happening is making sure any existing instance of Chrome is closed before starting the application.

I've verified that prior to starting the debugger/application that my application is not running via Task Manager.

Here is the code that sets up the server:

    var hostUrl = "https://company.site.com:1980/";
    var host = new WebHostBuilder()
        .UseKestrel(options =>
        {
            options.Listen(IPAddress.Parse("127.0.0.1"), 1980, listenOptions =>
            {
                listenOptions.UseHttps("Cert.pfx", "NotReal123");
            });
        })
        .UseUrls(hostUrl)
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseShutdownTimeout(TimeSpan.FromSeconds(60))
        .Build();
    host.Start();

My best guess is that Chrome is actively keeping that port open, is there a way to close the connection when the application crashes/closes?

EDIT:

The way I can replicate it (but still can't reliably replicate it) is I will do an action that requires talking across a WebSocket or ajax call and in the middle of the transaction, I kill the server but leave the client open.

Ran netstat -a after Visual Studio Debugger was killed but Chrome was left open and found these following entries:

  TCP    127.0.0.1:1985         PCNAME:0                LISTENING
  TCP    127.0.0.1:1985         PROJ:50733            CLOSE_WAIT
  TCP    127.0.0.1:1985         PROJ:65330            CLOSE_WAIT
  TCP    127.0.0.1:1985         PROJ:65331            CLOSE_WAIT
  TCP    127.0.0.1:1985         PROJ:65332            CLOSE_WAIT
.
.
.
  TCP    127.0.0.1:50733        PROJ:1985             FIN_WAIT_2
.
.
.
  TCP    127.0.0.1:65330        PROJ:1985             FIN_WAIT_2
  TCP    127.0.0.1:65331        PROJ:1985             FIN_WAIT_2
  TCP    127.0.0.1:65332        PROJ:1985             FIN_WAIT_2

PCNAME is the name of my laptop and PROJ is the 'subdomain' I made in the host file. Closing Chrome and running netstat again, the lines above no longer exist in the list.

Paul_LayLow
  • 133
  • 1
  • 13
  • Shouldn't be, use `netstat -a` to see what's holding the port open. – davidfowl May 05 '20 at 07:35
  • @davidfowl, ran it and pasted my results in my edit. In short, it seems to happen when the front end is awaiting a response from the server but the server is stopped during the middle of it. – Paul_LayLow May 07 '20 at 15:47

0 Answers0