Remove handle inheritance workaround Kestrel/#3040 (#5000)
This commit is contained in:
parent
3c09d644cc
commit
6046ad27f6
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
@ -80,7 +80,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets
|
||||||
IPEndPoint endPoint = _endPointInformation.IPEndPoint;
|
IPEndPoint endPoint = _endPointInformation.IPEndPoint;
|
||||||
|
|
||||||
var listenSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
var listenSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||||
NativeMethods.DisableHandleInheritance(listenSocket);
|
|
||||||
|
|
||||||
// Kestrel expects IPv6Any to bind to both IPv6 and IPv4
|
// Kestrel expects IPv6Any to bind to both IPv6 and IPv4
|
||||||
if (endPoint.Address == IPAddress.IPv6Any)
|
if (endPoint.Address == IPAddress.IPv6Any)
|
||||||
|
|
|
||||||
|
|
@ -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<SocketException>(() => client.ConnectAsync("127.0.0.1", port));
|
|
||||||
}
|
|
||||||
|
|
||||||
process.Kill();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue