diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Listener.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Listener.cs index abd9c46216..a40515d66f 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Listener.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Listener.cs @@ -3,6 +3,7 @@ using System; using System.Threading.Tasks; +using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; using Microsoft.Extensions.Logging; @@ -38,6 +39,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { var listener = ((Listener)tcs2.Task.AsyncState); listener.ListenSocket = listener.CreateListenSocket(); + ListenSocket.Listen(Constants.ListenBacklog, ConnectionCallback, this); tcs2.SetResult(0); } catch (Exception ex) @@ -54,7 +56,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http /// protected abstract UvStreamHandle CreateListenSocket(); - protected static void ConnectionCallback(UvStreamHandle stream, int status, Exception error, object state) + private static void ConnectionCallback(UvStreamHandle stream, int status, Exception error, object state) { var listener = (Listener)state; diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerPrimary.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerPrimary.cs index a9b938d158..9e40e6b549 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerPrimary.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/ListenerPrimary.cs @@ -70,10 +70,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http } var dispatchPipe = new UvPipeHandle(Log); - dispatchPipe.Init(Thread.Loop, Thread.QueueCloseHandle, true); try { + dispatchPipe.Init(Thread.Loop, Thread.QueueCloseHandle, true); pipe.Accept(dispatchPipe); } catch (UvException ex) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/PipeListener.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/PipeListener.cs index 0b01438870..8ef9e38683 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/PipeListener.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/PipeListener.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; using Microsoft.Extensions.Logging; @@ -22,9 +21,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http protected override UvStreamHandle CreateListenSocket() { var socket = new UvPipeHandle(Log); - socket.Init(Thread.Loop, Thread.QueueCloseHandle, false); - socket.Bind(ServerAddress.UnixPipePath); - socket.Listen(Constants.ListenBacklog, (stream, status, error, state) => ConnectionCallback(stream, status, error, state), this); + + try + { + socket.Init(Thread.Loop, Thread.QueueCloseHandle, false); + socket.Bind(ServerAddress.UnixPipePath); + } + catch + { + socket.Dispose(); + throw; + } + return socket; } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/PipeListenerPrimary.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/PipeListenerPrimary.cs index 3011a26616..ebc59cfadb 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/PipeListenerPrimary.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/PipeListenerPrimary.cs @@ -22,9 +22,18 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http protected override UvStreamHandle CreateListenSocket() { var socket = new UvPipeHandle(Log); - socket.Init(Thread.Loop, Thread.QueueCloseHandle, false); - socket.Bind(ServerAddress.UnixPipePath); - socket.Listen(Constants.ListenBacklog, (stream, status, error, state) => ConnectionCallback(stream, status, error, state), this); + + try + { + socket.Init(Thread.Loop, Thread.QueueCloseHandle, false); + socket.Bind(ServerAddress.UnixPipePath); + } + catch + { + socket.Dispose(); + throw; + } + return socket; } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/SocketOutput.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/SocketOutput.cs index daed0694e5..0b02da9748 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/SocketOutput.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/SocketOutput.cs @@ -583,7 +583,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http _writeReq = Self._writeReqPool.Allocate(); - _writeReq.Write(Self._socket, _lockedStart, _lockedEnd, _bufferCount, (_writeReq, status, error, state) => + _writeReq.Write(Self._socket, _lockedStart, _lockedEnd, _bufferCount, (req, status, error, state) => { var writeContext = (WriteContext)state; writeContext.PoolWriteReq(writeContext._writeReq); diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/TcpListener.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/TcpListener.cs index 8f3b664105..fd69130fb8 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/TcpListener.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/TcpListener.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; using Microsoft.Extensions.Logging; @@ -22,14 +23,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http protected override UvStreamHandle CreateListenSocket() { var socket = new UvTcpHandle(Log); - socket.Init(Thread.Loop, Thread.QueueCloseHandle); - socket.NoDelay(ServerOptions.NoDelay); - socket.Bind(ServerAddress); - // If requested port was "0", replace with assigned dynamic port. - ServerAddress.Port = socket.GetSockIPEndPoint().Port; + try + { + socket.Init(Thread.Loop, Thread.QueueCloseHandle); + socket.NoDelay(ServerOptions.NoDelay); + socket.Bind(ServerAddress); + + // If requested port was "0", replace with assigned dynamic port. + ServerAddress.Port = socket.GetSockIPEndPoint().Port; + } + catch + { + socket.Dispose(); + throw; + } - socket.Listen(Constants.ListenBacklog, (stream, status, error, state) => ConnectionCallback(stream, status, error, state), this); return socket; } diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/TcpListenerPrimary.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/TcpListenerPrimary.cs index b442deb42e..09b856438a 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/TcpListenerPrimary.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/TcpListenerPrimary.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; using Microsoft.Extensions.Logging; @@ -22,14 +23,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http protected override UvStreamHandle CreateListenSocket() { var socket = new UvTcpHandle(Log); - socket.Init(Thread.Loop, Thread.QueueCloseHandle); - socket.NoDelay(ServerOptions.NoDelay); - socket.Bind(ServerAddress); - // If requested port was "0", replace with assigned dynamic port. - ServerAddress.Port = socket.GetSockIPEndPoint().Port; + try + { + socket.Init(Thread.Loop, Thread.QueueCloseHandle); + socket.NoDelay(ServerOptions.NoDelay); + socket.Bind(ServerAddress); + + // If requested port was "0", replace with assigned dynamic port. + ServerAddress.Port = socket.GetSockIPEndPoint().Port; + } + catch + { + socket.Dispose(); + throw; + } - socket.Listen(Constants.ListenBacklog, (stream, status, error, state) => ConnectionCallback(stream, status, error, state), this); return socket; }