From 3aaae6964f8eba30ff78b426fe0569a99eacd4f1 Mon Sep 17 00:00:00 2001 From: Peter Hsu Date: Tue, 14 Jul 2015 10:36:34 -0700 Subject: [PATCH] Fix NPE issue where UvShutdownReq being garbage collected before the shutdown callback executed --- .../Networking/UvShutdownReq.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Networking/UvShutdownReq.cs b/src/Microsoft.AspNet.Server.Kestrel/Networking/UvShutdownReq.cs index 815350d3cc..53073f0e2a 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Networking/UvShutdownReq.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Networking/UvShutdownReq.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Runtime.InteropServices; namespace Microsoft.AspNet.Server.Kestrel.Networking { @@ -14,6 +15,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking Action _callback; object _state; + GCHandle _pin; public void Init(UvLoopHandle loop) { @@ -27,12 +29,14 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking { _callback = callback; _state = state; + _pin = GCHandle.Alloc(this, GCHandleType.Normal); _uv.shutdown(this, handle, _uv_shutdown_cb); } private static void UvShutdownCb(IntPtr ptr, int status) { var req = FromIntPtr(ptr); + req._pin.Free(); req._callback(req, status, req._state); req._callback = null; req._state = null;