[Kestrel] Getting System.IO.IOException: Failed to bind to address https://10.0.0.x:443: address already in use.
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
After stopping and starting again a process (for an update) it fails with this exception. After inspection with the **tcpview** program I **cannot find any program or process actively listening to the port that the exception describes**.
### Expected Behavior
beginning the kestrel process without any port binding errors since no one is listening to this port.
### Steps To Reproduce
I am really not so quite sure how to reproduce this issue. This is my kestrel configuration.
```csharp
builder.Host.UseWindowsService();
builder.Host.ConfigureHostOptions(options =>
{
options.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
options.ShutdownTimeout = TimeSpan.FromSeconds(2);
});
builder.WebHost.UseKestrel(serverOptions =>
{
serverOptions.Listen(internalIpv4, 80, portOptions =>
{
portOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
});
serverOptions.Listen(internalIpv6, 80, portOptions =>
{
portOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
});
serverOptions.Listen(internalIpv4, 443, portOptions =>
{
portOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
portOptions.DisableAltSvcHeader = false;
portOptions.UseHttps(httpsOptions =>
{
httpsOptions.ServerCertificateSelector = (connectionContext, name) =>
{
if (name != null && CertificateDictionary.TryGetValue(name, out var cert))
{
return cert;
}
logger.LogError($"Certificate for {name} not found.");
return null;
};
httpsOptions.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
httpsOptions.HandshakeTimeout = TimeSpan.FromSeconds(10);
});
});
serverOptions.Listen(internalIpv6, 443, portOptions =>
{
portOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
portOptions.DisableAltSvcHeader = false;
portOptions.UseHttps(httpsOptions =>
{
httpsOptions.ServerCertificateSelector = (connectionContext, name) =>
{
if (name != null && CertificateDictionary.TryGetValue(name, out var cert))
{
return cert;
}
logger.LogError($"Certificate for {name} not found.");
return null;
};
httpsOptions.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
httpsOptions.HandshakeTimeout = TimeSpan.FromSeconds(10);
});
});
serverOptions.AddServerHeader = false;
serverOptions.Limits.MaxConcurrentConnections = long.MaxValue;
serverOptions.Limits.MaxResponseBufferSize = 5120;
serverOptions.Limits.MaxRequestBufferSize = 4_196_352;
serverOptions.Limits.MaxRequestHeadersTotalSize = 65_536;
serverOptions.Limits.MaxRequestHeaderCount = 500;
serverOptions.Limits.MaxRequestBodySize = 104_857_600;
serverOptions.Limits.Http2.MaxStreamsPerConnection = 2_048;
serverOptions.Limits.MaxRequestLineSize = 16_384;
serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(1);
serverOptions.AllowSynchronousIO = false;
serverOptions.ResponseHeaderEncodingSelector = (encoding) =>
{
return Encoding.UTF8;
};
});
```
Also I have added YARP Reverse Proxy in the service and middleware tree
```csharp
builder.Services.AddReverseProxy().LoadFromMemory(routes, clusters);
```
```csharp
app.MapReverseProxy();
```
I don't know if this can affect the entire process.
### Exceptions (if any)
```
Application: MyApplication.exe
CoreCLR Version: 7.0.923.32018
.NET Version: 7.0.9
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException: Failed to bind to address https://10.0.0.x:443: address already in use.
---> Microsoft.AspNetCore.Connections.AddressInUseException: The local address is already in use.
---> System.Net.Quic.QuicException: The local address is already in use.
at System.Net.Quic.ThrowHelper.ThrowIfMsQuicError(Int32 status, String message)
at System.Net.Quic.QuicListener..ctor(QuicListenerOptions options)
at System.Net.Quic.QuicListener.ListenAsync(QuicListenerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Internal.QuicConnectionListener.CreateListenerAsync()
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Internal.QuicConnectionListener.CreateListenerAsync()
at Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.QuicTransportFactory.BindAsync(EndPoint endpoint, IFeatureCollection features, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, MultiplexedConnectionDelegate multiplexedConnectionDelegate, ListenOptions listenOptions, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass30_0`1.<g__OnBind|0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.EndpointsStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IEnumerable`1 listenOptions, AddressBindContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at MyApplication.Program.Main(String[] args)
```
### .NET Version
.NET Version: 7.0.9
### Anything else?
OS: Windows Server 2022 Datacenter Azure Edition
Version: 21H2
OS Build: 20348.1850
Contributor guide
Assessment
This issue has not been assessed yet.