diff --git a/src/Servers/Kestrel/Transport.Sockets/src/Internal/NativeMethods.cs b/src/Servers/Kestrel/Transport.Sockets/src/Internal/NativeMethods.cs deleted file mode 100644 index a77efc2c35..0000000000 --- a/src/Servers/Kestrel/Transport.Sockets/src/Internal/NativeMethods.cs +++ /dev/null @@ -1,31 +0,0 @@ -// 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 System.Net.Sockets; -using System.Runtime.InteropServices; - -namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal -{ - internal static class NativeMethods - { - [DllImport("kernel32.dll", SetLastError = true)] - private static extern bool SetHandleInformation(IntPtr hObject, HANDLE_FLAGS dwMask, HANDLE_FLAGS dwFlags); - - [Flags] - private enum HANDLE_FLAGS : uint - { - None = 0, - INHERIT = 1, - PROTECT_FROM_CLOSE = 2 - } - - internal static void DisableHandleInheritance(Socket socket) - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - SetHandleInformation(socket.Handle, HANDLE_FLAGS.INHERIT, 0); - } - } - } -} diff --git a/src/Servers/Kestrel/Transport.Sockets/src/SocketTransport.cs b/src/Servers/Kestrel/Transport.Sockets/src/SocketTransport.cs index bc61f1a97f..82599f20b7 100644 --- a/src/Servers/Kestrel/Transport.Sockets/src/SocketTransport.cs +++ b/src/Servers/Kestrel/Transport.Sockets/src/SocketTransport.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -80,7 +80,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets IPEndPoint endPoint = _endPointInformation.IPEndPoint; var listenSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); - NativeMethods.DisableHandleInheritance(listenSocket); // Kestrel expects IPv6Any to bind to both IPv6 and IPv4 if (endPoint.Address == IPAddress.IPv6Any) diff --git a/src/Servers/Kestrel/test/FunctionalTests/HandleInheritanceTests.cs b/src/Servers/Kestrel/test/FunctionalTests/HandleInheritanceTests.cs deleted file mode 100644 index cd75141197..0000000000 --- a/src/Servers/Kestrel/test/FunctionalTests/HandleInheritanceTests.cs +++ /dev/null @@ -1,61 +0,0 @@ -// 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.Diagnostics; -using System.Net.Sockets; -using System.Runtime.InteropServices; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Testing; -using Microsoft.AspNetCore.Testing.xunit; -using Xunit; - -namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests -{ - public class HandleInheritanceTests : TestApplicationErrorLoggerLoggedTest - { - [ConditionalFact] - [OSSkipCondition(OperatingSystems.Linux, SkipReason = "Fixed in 3.0 https://github.com/aspnet/KestrelHttpServer/issues/3040")] - [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Fixed in 3.0 https://github.com/aspnet/KestrelHttpServer/issues/3040")] - public async Task SpawnChildProcess_DoesNotInheritListenHandle() - { - var hostBuilder = TransportSelector.GetWebHostBuilder() - .UseKestrel() - .ConfigureServices(AddTestLogging) - .UseUrls("http://127.0.0.1:0") - .Configure(app => - { - app.Run(context => - { - return context.Response.WriteAsync("Hello World"); - }); - }); - - using (var host = hostBuilder.Build()) - { - await host.StartAsync(); - - var processInfo = new ProcessStartInfo - { - FileName = "cmd.exe", - CreateNoWindow = true, - }; - using (var process = Process.Start(processInfo)) - { - var port = host.GetPort(); - await host.StopAsync(); - - // We should not be able to connect if the handle was correctly closed and not inherited by the child process. - using (var client = new TcpClient()) - { - await Assert.ThrowsAnyAsync(() => client.ConnectAsync("127.0.0.1", port)); - } - - process.Kill(); - } - } - } - } -}