Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public HangDumpActivityIndicator(
string pipeNameEnvironmentVariable = $"{HangDumpConfiguration.PipeName}_{FNV_1aHashHelper.ComputeStringHash(testApplicationModuleInfo.GetCurrentTestApplicationFullPath())}_{namedPipeSuffix}";
string namedPipeName = _environment.GetEnvironmentVariable(pipeNameEnvironmentVariable)
?? throw new InvalidOperationException($"Expected {pipeNameEnvironmentVariable} environment variable set.");
_namedPipeClient = new NamedPipeClient(namedPipeName);
_namedPipeClient = new NamedPipeClient(namedPipeName, _environment);
_namedPipeClient.RegisterSerializer(new ActivityIndicatorMutexNameRequestSerializer(), typeof(ActivityIndicatorMutexNameRequest));
_namedPipeClient.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse));
_namedPipeClient.RegisterSerializer(new SessionEndSerializerRequestSerializer(), typeof(SessionEndSerializerRequest));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private async Task<IResponse> CallbackAsync(IRequest request)
else if (request is ConsumerPipeNameRequest consumerPipeNameRequest)
{
await _logger.LogDebugAsync($"Consumer pipe name received '{consumerPipeNameRequest.PipeName}'").ConfigureAwait(false);
_namedPipeClient = new NamedPipeClient(consumerPipeNameRequest.PipeName);
_namedPipeClient = new NamedPipeClient(consumerPipeNameRequest.PipeName, _environment);
_namedPipeClient.RegisterSerializer(new GetInProgressTestsResponseSerializer(), typeof(GetInProgressTestsResponse));
_namedPipeClient.RegisterSerializer(new GetInProgressTestsRequestSerializer(), typeof(GetInProgressTestsRequest));
_namedPipeClient.RegisterSerializer(new ExitSignalActivityIndicatorTaskRequestSerializer(), typeof(ExitSignalActivityIndicatorTaskRequest));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task BeforeRunAsync(CancellationToken cancellationToken)
{
string namedPipeName = _environment.GetEnvironmentVariable(TrxEnvironmentVariableProvider.TRXNAMEDPIPENAME)
?? throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ExtensionResources.TrxReportGeneratorMissingTrxNamedPipeEnvironmentVariable, TrxEnvironmentVariableProvider.TRXNAMEDPIPENAME));
NamedPipeClient = new NamedPipeClient(namedPipeName);
NamedPipeClient = new NamedPipeClient(namedPipeName, _environment);
NamedPipeClient.RegisterSerializer(new ReportFileNameRequestSerializer(), typeof(ReportFileNameRequest));
NamedPipeClient.RegisterSerializer(new TestAdapterInformationRequestSerializer(), typeof(TestAdapterInformationRequest));
NamedPipeClient.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ await LogTestHostCreatedAsync(
environment.SetEnvironmentVariable(pipeEnvironmentVariable, string.Empty);

// Create client to connect to the monitor
NamedPipeClient client = new(pipeName);
NamedPipeClient client = new(pipeName, environment);
client.RegisterAllSerializers();

// Connect to the monitor
Expand Down
21 changes: 19 additions & 2 deletions src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#if NETCOREAPP
using System.Buffers;

using Microsoft.Testing.Platform.Helpers;
#endif

using System.IO.Pipes;

using Microsoft.Testing.Platform.Helpers;

#if NET
using Microsoft.Testing.Platform.Resources;
#endif
Expand All @@ -24,14 +24,21 @@ internal sealed class NamedPipeClient : NamedPipeBase, IClient
private readonly MemoryStream _serializationBuffer = new();
private readonly MemoryStream _messageBuffer = new();
private readonly byte[] _readBuffer = new byte[250000];
private readonly IEnvironment _environment;

private bool _disposed;

public NamedPipeClient(string name)
: this(name, new SystemEnvironment())
{
}

public NamedPipeClient(string name, IEnvironment environment)
{
Guard.NotNull(name);
_namedPipeClientStream = new(".", name, PipeDirection.InOut);
PipeName = name;
_environment = environment;
}

public string PipeName { get; }
Expand Down Expand Up @@ -144,6 +151,16 @@ public async Task<TResponse> RequestReplyAsync<TRequest, TResponse>(TRequest req
int currentReadBytes = await _namedPipeClientStream.ReadAsync(_readBuffer, currentReadIndex, _readBuffer.Length, cancellationToken).ConfigureAwait(false);
#endif

if (currentReadBytes == 0)
{
// We are reading a message response.
// If we cannot get a response, there is no way we can recover and continue executing.
// This can happen if the other processes gets killed or crashes while while it's sending the response.
// This is especially important for 'dotnet test', where the user can simply kill the dotnet.exe process themselves.
// In that case, we want the MTP process to also die.
_environment.FailFast("[NamedPipeClient] Connection lost with the other side.");
}

// Reset the current chunk size
int missingBytesToReadOfCurrentChunk = currentReadBytes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task AfterCommonServiceSetupAsync()
_environment.SetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_DOTNETTEST_EXECUTIONID, Guid.NewGuid().ToString("N"));
}

_dotnetTestPipeClient = new(arguments[0]);
_dotnetTestPipeClient = new(arguments[0], _environment);
_dotnetTestPipeClient.RegisterAllSerializers();

await _dotnetTestPipeClient.ConnectAsync(_cancellationTokenSource.CancellationToken).ConfigureAwait(false);
Expand Down