Avoid unobserved exceptions

- Don't throw from AdaptedPipeline.ReadInputAsync
- Watch for unobserved exceptions in SampleApp
This commit is contained in:
Stephen Halter 2017-02-07 03:44:00 -08:00
parent ba549502e1
commit 7d3bcd2bf8
4 changed files with 12 additions and 9 deletions

View File

@ -4,6 +4,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -34,6 +35,11 @@ namespace SampleApp
public static void Main(string[] args) public static void Main(string[] args)
{ {
TaskScheduler.UnobservedTaskException += (sender, e) =>
{
Console.WriteLine("Unobserved exception: {0}", e.Exception);
};
var host = new WebHostBuilder() var host = new WebHostBuilder()
.UseKestrel(options => .UseKestrel(options =>
{ {

View File

@ -63,7 +63,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Adapter.Internal
catch (Exception ex) catch (Exception ex)
{ {
Input.Writer.Complete(ex); Input.Writer.Complete(ex);
throw;
// Don't rethrow the exception. It should be handled by the Pipeline consumer.
return;
} }
} while (bytesRead != 0); } while (bytesRead != 0);

View File

@ -119,10 +119,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
public Task StopAsync() public Task StopAsync()
{ {
_frame.StopAsync(); return Task.WhenAll(_frame.StopAsync(), _socketClosedTcs.Task);
_frame.Input.Reader.CancelPendingRead();
return _socketClosedTcs.Task;
} }
public virtual Task AbortAsync(Exception error = null) public virtual Task AbortAsync(Exception error = null)

View File

@ -402,10 +402,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
/// </summary> /// </summary>
public Task StopAsync() public Task StopAsync()
{ {
if (!_requestProcessingStopping) _requestProcessingStopping = true;
{ Input.Reader.CancelPendingRead();
_requestProcessingStopping = true;
}
return _requestProcessingTask ?? TaskCache.CompletedTask; return _requestProcessingTask ?? TaskCache.CompletedTask;
} }