From 76c9e7d8de0f59cf7efe18c7045672e186e4cbfa Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Mon, 22 Jul 2024 18:48:12 +0200 Subject: [PATCH] Fix possible deadlock inside MSBuild task (#3307) --- .../Tasks/InvokeTestingPlatformTask.cs | 8 ++++++-- .../Microsoft.Testing.Platform/IPC/NamedPipeServer.cs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs b/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs index 1422779089..39ed30692f 100644 --- a/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs +++ b/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs @@ -238,7 +238,7 @@ protected override void LogEventsFromTextOutput(string singleLine, MessageImport } protected override void ProcessStarted() - => _connectionLoopTask = Task.Run(() => + => _connectionLoopTask = Task.Run(async () => { try { @@ -249,7 +249,7 @@ protected override void ProcessStarted() pipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); pipeServer.RegisterSerializer(new FailedTestInfoRequestSerializer(), typeof(FailedTestInfoRequest)); pipeServer.RegisterSerializer(new RunSummaryInfoRequestSerializer(), typeof(RunSummaryInfoRequest)); - pipeServer.WaitConnectionAsync(_waitForConnections.Token).GetAwaiter().GetResult(); + await pipeServer.WaitConnectionAsync(_waitForConnections.Token); _connections.Add(pipeServer); Log.LogMessage(MessageImportance.Low, $"Client connected to '{_pipeNameDescription.Name}'"); } @@ -258,6 +258,10 @@ protected override void ProcessStarted() { // Do nothing we're cancelling } + catch (Exception ex) + { + Log.LogError(ex.ToString()); + } }); public override bool Execute() diff --git a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs index 1eb2eb56f4..5625a972d8 100644 --- a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs +++ b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs @@ -63,7 +63,7 @@ public NamedPipeServer( CancellationToken cancellationToken) { ArgumentGuard.IsNotNull(pipeNameDescription); - _namedPipeServerStream = new((PipeName = pipeNameDescription).Name, PipeDirection.InOut, maxNumberOfServerInstances); + _namedPipeServerStream = new((PipeName = pipeNameDescription).Name, PipeDirection.InOut, maxNumberOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); _callback = callback; _environment = environment; _logger = logger;