Prevent access to closed socket in in Connection.End

This commit is contained in:
Stephen Halter 2015-08-06 15:14:50 -07:00
parent b162202519
commit c0cc511b5b
1 changed files with 22 additions and 0 deletions

View File

@ -4,6 +4,7 @@
using System;
using Microsoft.AspNet.Server.Kestrel.Networking;
using System.Diagnostics;
using System.Threading;
namespace Microsoft.AspNet.Server.Kestrel.Http
{
@ -15,6 +16,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
private static readonly Action<UvStreamHandle, int, Exception, object> _readCallback = ReadCallback;
private static readonly Func<UvStreamHandle, int, object, Libuv.uv_buf_t> _allocCallback = AllocCallback;
private int _connectionState;
private static Libuv.uv_buf_t AllocCallback(UvStreamHandle handle, int suggestedSize, object state)
{
return ((Connection)state).OnAlloc(handle, suggestedSize);
@ -104,6 +107,12 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
switch (endType)
{
case ProduceEndType.SocketShutdownSend:
if (Interlocked.CompareExchange(ref _connectionState, ConnectionState.Shutdown, ConnectionState.Open)
!= ConnectionState.Open)
{
return;
}
KestrelTrace.Log.ConnectionWriteFin(_connectionId, 0);
Thread.Post(
x =>
@ -128,6 +137,12 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
_frame);
break;
case ProduceEndType.SocketDisconnect:
if (Interlocked.Exchange(ref _connectionState, ConnectionState.Disconnected)
== ConnectionState.Disconnected)
{
return;
}
KestrelTrace.Log.ConnectionDisconnect(_connectionId);
Thread.Post(
x =>
@ -139,5 +154,12 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
break;
}
}
private static class ConnectionState
{
public const int Open = 0;
public const int Shutdown = 1;
public const int Disconnected = 2;
}
}
}