Skip to content

Commit 637e764

Browse files
committed
Fix accessing PID in TestHostControllersTestHost
1 parent 0044362 commit 637e764

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControllersTestHost.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,25 @@ protected override async Task<int> InternalRunAsync()
205205
await _logger.LogDebugAsync($"Starting test host process");
206206
using IProcess testHostProcess = process.Start(processStartInfo);
207207

208-
testHostProcess.Exited += (sender, e) =>
209-
{
210-
var processExited = sender as Process;
211-
_logger.LogDebug($"Test host process exited, PID: '{processExited?.Id}'");
212-
};
213-
208+
int? testHostProcessId = null;
214209
try
215210
{
216-
await _logger.LogDebugAsync($"Started test host process '{testHostProcess.Id}' HasExited: {testHostProcess.HasExited}");
211+
testHostProcessId = testHostProcess.Id;
217212
}
218213
catch (InvalidOperationException) when (testHostProcess.HasExited)
219214
{
220215
// Access PID can throw InvalidOperationException if the process has already exited:
221216
// System.InvalidOperationException: No process is associated with this object.
222217
}
223218

219+
testHostProcess.Exited += (sender, e) =>
220+
{
221+
var processExited = sender as Process;
222+
_logger.LogDebug($"Test host process exited, PID: '{testHostProcessId}'");
223+
};
224+
225+
await _logger.LogDebugAsync($"Started test host process '{testHostProcessId}' HasExited: {testHostProcess.HasExited}");
226+
224227
string? seconds = configuration[PlatformConfigurationConstants.PlatformTestHostControllersManagerSingleConnectionNamedPipeServerWaitConnectionTimeoutSeconds];
225228
int timeoutSeconds = seconds is null ? TimeoutHelper.DefaultHangTimeoutSeconds : int.Parse(seconds, CultureInfo.InvariantCulture);
226229
await _logger.LogDebugAsync($"Setting PlatformTestHostControllersManagerSingleConnectionNamedPipeServerWaitConnectionTimeoutSeconds '{timeoutSeconds}'");

0 commit comments

Comments
 (0)