Fix NPE issue where UvShutdownReq being garbage collected before the shutdown callback executed

This commit is contained in:
Peter Hsu 2015-07-14 10:36:34 -07:00
parent 94dba8ff0e
commit 3aaae6964f
1 changed files with 4 additions and 0 deletions

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Runtime.InteropServices;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
@ -14,6 +15,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
Action<UvShutdownReq, int, object> _callback; Action<UvShutdownReq, int, object> _callback;
object _state; object _state;
GCHandle _pin;
public void Init(UvLoopHandle loop) public void Init(UvLoopHandle loop)
{ {
@ -27,12 +29,14 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
_callback = callback; _callback = callback;
_state = state; _state = state;
_pin = GCHandle.Alloc(this, GCHandleType.Normal);
_uv.shutdown(this, handle, _uv_shutdown_cb); _uv.shutdown(this, handle, _uv_shutdown_cb);
} }
private static void UvShutdownCb(IntPtr ptr, int status) private static void UvShutdownCb(IntPtr ptr, int status)
{ {
var req = FromIntPtr<UvShutdownReq>(ptr); var req = FromIntPtr<UvShutdownReq>(ptr);
req._pin.Free();
req._callback(req, status, req._state); req._callback(req, status, req._state);
req._callback = null; req._callback = null;
req._state = null; req._state = null;