Guarding against leaking GCHandles in read/write operations

See #19
This commit is contained in:
Louis DeJardin 2014-07-08 18:11:09 -07:00
parent 1406ec94c3
commit de6c32dc4b
3 changed files with 97 additions and 39 deletions

View File

@ -53,11 +53,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
SocketOutput _self;
ArraySegment<byte> _buffer;
Action<Exception> _drained;
UvStreamHandle _socket;
Action<Exception, object> _callback;
object _state;
GCHandle _pin;
internal void Contextualize(
SocketOutput socketOutput,
@ -87,8 +85,14 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{
KestrelTrace.Log.ConnectionWriteCallback(0, status);
//NOTE: pool this?
var callback = _callback;
_callback = null;
var state = _state;
_state = null;
Dispose();
_callback(error, _state);
callback(error, state);
}
}

View File

@ -36,12 +36,29 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
}
public void Listen(int backlog, Action<UvStreamHandle, int, Exception, object> callback, object state)
{
if (_listenVitality.IsAllocated)
{
throw new InvalidOperationException("TODO: Listen may not be called more than once");
}
try
{
_listenCallback = callback;
_listenState = state;
_listenVitality = GCHandle.Alloc(this, GCHandleType.Normal);
_uv.listen(this, 10, _uv_connection_cb);
}
catch
{
_listenCallback = null;
_listenState = null;
if (_listenVitality.IsAllocated)
{
_listenVitality.Free();
}
throw;
}
}
public void Accept(UvStreamHandle handle)
{
@ -52,6 +69,12 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
Func<UvStreamHandle, int, object, Libuv.uv_buf_t> allocCallback,
Action<UvStreamHandle, int, Exception, object> readCallback,
object state)
{
if (_readVitality.IsAllocated)
{
throw new InvalidOperationException("TODO: ReadStop must be called before ReadStart may be called again");
}
try
{
_allocCallback = allocCallback;
_readCallback = readCallback;
@ -59,9 +82,25 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
_readVitality = GCHandle.Alloc(this, GCHandleType.Normal);
_uv.read_start(this, _uv_alloc_cb, _uv_read_cb);
}
catch
{
_allocCallback = null;
_readCallback = null;
_readState = null;
if (_readVitality.IsAllocated)
{
_readVitality.Free();
}
throw;
}
}
public void ReadStop()
{
if (!_readVitality.IsAllocated)
{
throw new InvalidOperationException("TODO: ReadStart must be called before ReadStop may be called");
}
_allocCallback = null;
_readCallback = null;
_readState = null;

View File

@ -39,6 +39,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
ArraySegment<ArraySegment<byte>> bufs,
Action<UvWriteReq, int, Exception, object> callback,
object state)
{
try
{
// add GCHandle to keeps this SafeHandle alive while request processing
_pins.Add(GCHandle.Alloc(this, GCHandleType.Normal));
@ -70,15 +72,28 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
_state = state;
_uv.write(this, handle, pBuffers, nBuffers, _uv_write_cb);
}
private static void UvWriteCb(IntPtr ptr, int status)
catch
{
_callback = null;
_state = null;
Unpin(this);
throw;
}
}
private static void Unpin(UvWriteReq req)
{
var req = FromIntPtr<UvWriteReq>(ptr);
foreach (var pin in req._pins)
{
pin.Free();
}
req._pins.Clear();
}
private static void UvWriteCb(IntPtr ptr, int status)
{
var req = FromIntPtr<UvWriteReq>(ptr);
Unpin(req);
var callback = req._callback;
req._callback = null;