Better report Kestrel crashing errors

This commit is contained in:
Stephen Halter 2017-02-02 17:07:14 -08:00
parent 8cbe54a182
commit 5124adf450
4 changed files with 34 additions and 10 deletions

View File

@ -142,17 +142,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal
Post(t => t.OnStopImmediate());
if (!await WaitAsync(_threadTcs.Task, stepTimeout).ConfigureAwait(false))
{
_log.LogError(0, null, "KestrelThread.StopAsync failed to terminate libuv thread.");
_log.LogCritical("KestrelThread.StopAsync failed to terminate libuv thread.");
}
}
}
catch (ObjectDisposedException)
{
// REVIEW: Should we log something here?
// Until we rework this logic, ODEs are bound to happen sometimes.
if (!await WaitAsync(_threadTcs.Task, stepTimeout).ConfigureAwait(false))
{
_log.LogError(0, null, "KestrelThread.StopAsync failed to terminate libuv thread.");
_log.LogCritical("KestrelThread.StopAsync failed to terminate libuv thread.");
}
}
}
@ -343,8 +342,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal
}
finally
{
thisHandle.Free();
_threadTcs.SetResult(null);
thisHandle.Free();
}
}

View File

@ -51,7 +51,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal
public void Dispose()
{
Task.WaitAll(Threads.Select(thread => thread.StopAsync(TimeSpan.FromSeconds(2.5))).ToArray());
try
{
Task.WaitAll(Threads.Select(thread => thread.StopAsync(TimeSpan.FromSeconds(2.5))).ToArray());
}
catch (AggregateException aggEx)
{
// An uncaught exception was likely thrown from the libuv event loop.
// The original error that crashed one loop may have caused secondary errors in others.
// Make sure that the stack trace of the original error is logged.
foreach (var ex in aggEx.InnerExceptions)
{
Log.LogCritical("Failed to gracefully close Kestrel.", ex);
}
throw;
}
Threads.Clear();
#if DEBUG

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[InlineData(-1337)]
public void StartWithNonPositiveThreadCountThrows(int threadCount)
{
var testLogger = new TestApplicationErrorLogger();
var testLogger = new TestApplicationErrorLogger { ThrowOnCriticalErrors = false };
var server = CreateServer(new KestrelServerOptions() { ThreadCount = threadCount }, testLogger);
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => StartDummyApplication(server));
@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[Fact]
public void StartWithInvalidAddressThrows()
{
var testLogger = new TestApplicationErrorLogger();
var testLogger = new TestApplicationErrorLogger { ThrowOnCriticalErrors = false };
var server = CreateServer(new KestrelServerOptions(), testLogger);
server.Features.Get<IServerAddressesFeature>().Addresses.Add("http:/asdf");
@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[InlineData(int.MaxValue - 1, int.MaxValue)]
public void StartWithMaxRequestBufferSizeLessThanMaxRequestLineSizeThrows(long maxRequestBufferSize, int maxRequestLineSize)
{
var testLogger = new TestApplicationErrorLogger();
var testLogger = new TestApplicationErrorLogger { ThrowOnCriticalErrors = false };
var options = new KestrelServerOptions();
options.Limits.MaxRequestBufferSize = maxRequestBufferSize;
options.Limits.MaxRequestLineSize = maxRequestLineSize;

View File

@ -14,6 +14,8 @@ namespace Microsoft.AspNetCore.Testing
// Application errors are logged using 13 as the eventId.
private const int ApplicationErrorEventId = 13;
public bool ThrowOnCriticalErrors { get; set; } = true;
public ConcurrentBag<LogMessage> Messages { get; } = new ConcurrentBag<LogMessage>();
public int TotalErrorsLogged => Messages.Count(message => message.LogLevel == LogLevel.Error);
@ -34,9 +36,17 @@ namespace Microsoft.AspNetCore.Testing
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
#if false
Console.WriteLine($"Log {logLevel}[{eventId}]: {formatter(state, exception)} {exception?.Message}");
#if true
if (logLevel == LogLevel.Critical && ThrowOnCriticalErrors)
#endif
{
Console.WriteLine($"Log {logLevel}[{eventId}]: {formatter(state, exception)} {exception?.Message}");
if (logLevel == LogLevel.Critical && ThrowOnCriticalErrors)
{
throw new Exception("Unexpected critical error.", exception);
}
}
Messages.Add(new LogMessage
{