From cc84097016276da3acf5e7089e57a98ce9a39377 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sun, 1 Nov 2015 13:31:45 +0000 Subject: [PATCH] Listener - remove closure allocation --- .../Http/Listener.cs | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/Listener.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/Listener.cs index a7b72acc01..d1a80b7043 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/Listener.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/Listener.cs @@ -30,19 +30,22 @@ namespace Microsoft.AspNet.Server.Kestrel.Http Thread = thread; Application = application; - var tcs = new TaskCompletionSource(); - Thread.Post(_ => + var tcs = new TaskCompletionSource(this); + + Thread.Post(tcs2 => { try { - ListenSocket = CreateListenSocket(); - tcs.SetResult(0); + var listener = ((Listener)tcs2.Task.AsyncState); + listener.ListenSocket = listener.CreateListenSocket(); + tcs2.SetResult(0); } catch (Exception ex) { - tcs.SetException(ex); + tcs2.SetException(ex); } - }, null); + }, tcs); + return tcs.Task; } @@ -85,21 +88,22 @@ namespace Microsoft.AspNet.Server.Kestrel.Http // the exception that stopped the event loop will never be surfaced. if (Thread.FatalError == null && ListenSocket != null) { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(this); Thread.Post( - _ => + tcs2 => { try { - ListenSocket.Dispose(); - tcs.SetResult(0); + var socket = (Listener)tcs2.Task.AsyncState; + socket.ListenSocket.Dispose(); + tcs2.SetResult(0); } catch (Exception ex) { - tcs.SetException(ex); + tcs2.SetException(ex); } }, - null); + tcs); // REVIEW: Should we add a timeout here to be safe? tcs.Task.Wait();