Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix DuplicateAndClose_TcpServerHandler flaky test #103161

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/libraries/Common/tests/System/Net/RemoteExecutorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ namespace Microsoft.DotNet.RemoteExecutor;

internal static class RemoteExecutorExtensions
{
/// <summary>
/// Dispose the RemoteInvokeHandle synchronously can take considerable time, and cause other unrelated tests to fail on timeout
/// because of depletion of xUnit synchronization context threads.
/// Running dispose in a separate task on the thread pool can help alleviate this issue.
/// </summary>
/// <example>
/// Executes the ServerCode in separate process and awaits its completion in a separate task outside of current synchronization context.
/// <code>
/// await RemoteExecutor.Invoke(ServerCode).DisposeAsync();
/// </code>
/// </example>
public static async ValueTask DisposeAsync(this RemoteInvokeHandle handle)
{
await Task.Run(handle.Dispose);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Pipes;
using System.Runtime.Serialization.Formatters.Binary;
Expand Down Expand Up @@ -265,7 +266,6 @@ public void SocketCtr_SocketInformation_WhenProtocolInformationTooShort_Throws()
public abstract class PolymorphicTests<T> where T : SocketHelperBase, new()
{
private static readonly T Helper = new T();
private readonly string _ipcPipeName = Path.GetRandomFileName();

private static void WriteSocketInfo(Stream stream, SocketInformation socketInfo)
{
Expand Down Expand Up @@ -317,44 +317,58 @@ public async Task DuplicateAndClose_TcpServerHandler(AddressFamily addressFamily
// Async is allowed on the listener:
using Socket handlerOriginal = await listener.AcceptAsync();

// pipe used to exchange socket info
await using NamedPipeServerStream pipeServerStream =
new NamedPipeServerStream(_ipcPipeName, PipeDirection.Out);
// socket used to exchange duplicated socket info with server code
using Socket ipcServerListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ipcServerListener.BindToAnonymousPort(IPAddress.Loopback);
ipcServerListener.Listen(1);
string ipcPortString = ((IPEndPoint)ipcServerListener.LocalEndPoint).Port.ToString(CultureInfo.InvariantCulture);

if (sameProcess)
{
Task handlerCode = Task.Run(() => HandlerServerCode(_ipcPipeName));
await RunCommonHostLogic(Environment.ProcessId);
Task handlerCode = Task.Run(() => HandlerServerCode(ipcPortString));
RunCommonHostLogic(Environment.ProcessId);
await handlerCode;
}
else
{
RemoteInvokeHandle hServerProc = RemoteExecutor.Invoke(HandlerServerCode, _ipcPipeName);
await RunCommonHostLogic(hServerProc.Process.Id);
await hServerProc.DisposeAsync();
RemoteInvokeHandle hServerProc = RemoteExecutor.Invoke(HandlerServerCode, ipcPortString);

// Since RunCommonHostLogic can throw, we need to make sure the server process is disposed
try
{
RunCommonHostLogic(hServerProc.Process.Id);
}
finally
{
await hServerProc.DisposeAsync();
}
}

async Task RunCommonHostLogic(int processId)
void RunCommonHostLogic(int processId)
{
await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding);

pipeServerStream.WaitForConnection();
using Socket ipcServer = ipcServerListener.Accept();
ipcServer.NoDelay = true;

// Duplicate the socket:
SocketInformation socketInfo = handlerOriginal.DuplicateAndClose(processId);
WriteSocketInfo(pipeServerStream, socketInfo);
// Send socketInfo to server code
using var ipcStream = new NetworkStream(ipcServer, false);
WriteSocketInfo(new NetworkStream(ipcServer), socketInfo);

// Send client data:
client.Send(TestBytes);
}

static async Task<int> HandlerServerCode(string ipcPipeName)
static async Task<int> HandlerServerCode(string ipcPortString)
rokonec marked this conversation as resolved.
Show resolved Hide resolved
{
await using NamedPipeClientStream pipeClientStream =
new NamedPipeClientStream(".", ipcPipeName, PipeDirection.In);
pipeClientStream.Connect();
int ipcPort = int.Parse(ipcPortString, CultureInfo.InvariantCulture);
using Socket ipcClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
rokonec marked this conversation as resolved.
Show resolved Hide resolved
ipcClient.NoDelay = true;
await ipcClient.ConnectAsync(IPAddress.Loopback, ipcPort);

await using var ipcStream = new NetworkStream(ipcClient, true);
SocketInformation socketInfo = ReadSocketInfo(ipcStream);

SocketInformation socketInfo = ReadSocketInfo(pipeClientStream);
using Socket handler = new Socket(socketInfo);

Assert.True(handler.IsBound);
Expand Down
Loading