Skip to content

Commit

Permalink
Don't log a stack trace for AddressInUseException (#30154)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Ross <[email protected]>
  • Loading branch information
BerserkerDotNet and Tratcher authored Feb 23, 2021
1 parent 695ca4b commit 465a219
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Servers/Kestrel/Core/src/Internal/KestrelServerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ async Task OnBind(ListenOptions options)

await BindAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
catch
{
Trace.LogCritical(0, ex, "Unable to start Kestrel.");
// Don't log the error https://github.com/dotnet/aspnetcore/issues/29801
Dispose();
throw;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Servers/Kestrel/Core/test/KestrelServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void StartWithInvalidAddressThrows()
var exception = Assert.Throws<FormatException>(() => StartDummyApplication(server));

Assert.Contains("Invalid url", exception.Message);
Assert.Equal(1, testLogger.CriticalErrorsLogged);
Assert.Equal(0, testLogger.CriticalErrorsLogged);
}
}

Expand Down Expand Up @@ -123,7 +123,7 @@ public void StartWithPathBaseInAddressThrows()
Assert.Equal(
$"A path base can only be configured using {nameof(IApplicationBuilder)}.UsePathBase().",
exception.Message);
Assert.Equal(1, testLogger.CriticalErrorsLogged);
Assert.Equal(0, testLogger.CriticalErrorsLogged);
}
}

Expand Down Expand Up @@ -171,7 +171,7 @@ public void StartWithMaxRequestBufferSizeLessThanMaxRequestLineSizeThrows(long m
Assert.Equal(
CoreStrings.FormatMaxRequestBufferSmallerThanRequestLineBuffer(maxRequestBufferSize, maxRequestLineSize),
exception.Message);
Assert.Equal(1, testLogger.CriticalErrorsLogged);
Assert.Equal(0, testLogger.CriticalErrorsLogged);
}
}

Expand All @@ -198,7 +198,7 @@ public void StartWithMaxRequestBufferSizeLessThanMaxRequestHeadersTotalSizeThrow
Assert.Equal(
CoreStrings.FormatMaxRequestBufferSmallerThanRequestHeaderBuffer(maxRequestBufferSize, maxRequestHeadersTotalSize),
exception.Message);
Assert.Equal(1, testLogger.CriticalErrorsLogged);
Assert.Equal(0, testLogger.CriticalErrorsLogged);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public class TestApplicationErrorLoggerLoggedTest : LoggedTest

public ConcurrentQueue<LogMessage> LogMessages => TestApplicationErrorLogger.Messages;

public bool ThrowOnCriticalErrors
{
get => TestApplicationErrorLogger.ThrowOnCriticalErrors;
set => TestApplicationErrorLogger.ThrowOnCriticalErrors = value;
}

public bool ThrowOnUngracefulShutdown
{
get => TestApplicationErrorLogger.ThrowOnUngracefulShutdown;
Expand Down
25 changes: 19 additions & 6 deletions src/Servers/Kestrel/test/BindTests/AddressRegistrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ private async Task RegisterDefaultServerAddresses_Success(IEnumerable<string> ad
[Fact]
public async Task ThrowsWhenBindingToIPv4AddressInUse()
{
ThrowOnCriticalErrors = false;

using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
Expand All @@ -562,13 +564,17 @@ public async Task ThrowsWhenBindingToIPv4AddressInUse()
.UseKestrel()
.UseUrls($"http://127.0.0.1:{port}")
.Configure(ConfigureEchoAddress);
});
})
.ConfigureServices(AddTestLogging);

using (var host = hostBuilder.Build())
{
var exception = Assert.Throws<IOException>(() => host.Start());
Assert.Equal(CoreStrings.FormatEndpointAlreadyInUse($"http://127.0.0.1:{port}"), exception.Message);

var expectedMessage = CoreStrings.FormatEndpointAlreadyInUse($"http://127.0.0.1:{port}");
Assert.Equal(expectedMessage, exception.Message);
Assert.Equal(0, LogMessages.Count(log => log.LogLevel == LogLevel.Critical &&
log.Exception is null &&
log.Message.EndsWith(expectedMessage, StringComparison.Ordinal)));
await host.StopAsync();
}
}
Expand All @@ -578,7 +584,7 @@ public async Task ThrowsWhenBindingToIPv4AddressInUse()
[IPv6SupportedCondition]
public async Task ThrowsWhenBindingToIPv6AddressInUse()
{
IgnoredCriticalLogExceptions.Add(typeof(IOException));
ThrowOnCriticalErrors = false;

using (var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
{
Expand All @@ -599,7 +605,11 @@ public async Task ThrowsWhenBindingToIPv6AddressInUse()
using (var host = hostBuilder.Build())
{
var exception = Assert.Throws<IOException>(() => host.Start());
Assert.Equal(CoreStrings.FormatEndpointAlreadyInUse($"http://[::1]:{port}"), exception.Message);
var expectedMessage = CoreStrings.FormatEndpointAlreadyInUse($"http://[::1]:{port}");
Assert.Equal(expectedMessage, exception.Message);
Assert.Equal(0, LogMessages.Count(log => log.LogLevel == LogLevel.Critical &&
log.Exception is null &&
log.Message.EndsWith(expectedMessage, StringComparison.Ordinal)));

await host.StopAsync();
}
Expand Down Expand Up @@ -931,7 +941,7 @@ public async Task EndpointDefaultsConfig_CanSetProtocolForUrlsConfig(string inpu

private void ThrowsWhenBindingLocalhostToAddressInUse(AddressFamily addressFamily)
{
IgnoredCriticalLogExceptions.Add(typeof(IOException));
ThrowOnCriticalErrors = false;

var addressInUseCount = 0;
var wrongMessageCount = 0;
Expand Down Expand Up @@ -990,6 +1000,9 @@ private void ThrowsWhenBindingLocalhostToAddressInUse(AddressFamily addressFamil
}

Assert.Equal(CoreStrings.FormatEndpointAlreadyInUse(thisAddressString), exception.Message);
Assert.Equal(0, LogMessages.Count(log => log.LogLevel == LogLevel.Critical &&
log.Exception is null &&
log.Message.EndsWith(CoreStrings.FormatEndpointAlreadyInUse(thisAddressString), StringComparison.Ordinal)));
break;
}
}
Expand Down

0 comments on commit 465a219

Please sign in to comment.