From 6940249de2623e0135b62aabbdc6c8386515d4ec Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 16 Jul 2020 15:13:24 -0700 Subject: [PATCH] Handle exception thrown by ProcessName during ProcessEx timeout (#23990) * Handle exception thrown by ProcessName during ProcessEx timeout * Update src/Shared/Process/ProcessEx.cs Co-authored-by: Brennan * PR feedback Co-authored-by: Brennan --- src/Shared/Process/ProcessEx.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Shared/Process/ProcessEx.cs b/src/Shared/Process/ProcessEx.cs index c4f0002d5c..ad7d06b1ed 100644 --- a/src/Shared/Process/ProcessEx.cs +++ b/src/Shared/Process/ProcessEx.cs @@ -49,10 +49,15 @@ namespace Microsoft.AspNetCore.Internal _exited = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + // We greedily create a timeout exception message even though a timeout is unlikely to happen for two reasons: + // 1. To make it less likely for Process getters to throw exceptions like "System.InvalidOperationException: Process has exited, ..." + // 2. To ensure if/when exceptions are thrown from Process getters, these exceptions can easily be observed. + var timeoutExMessage = $"Process proc {proc.ProcessName} {proc.StartInfo.Arguments} timed out after {DefaultProcessTimeout}."; + _processTimeoutCts = new CancellationTokenSource(timeout); _processTimeoutCts.Token.Register(() => { - _exited.TrySetException(new TimeoutException($"Process proc {proc.ProcessName} {proc.StartInfo.Arguments} timed out after {DefaultProcessTimeout}.")); + _exited.TrySetException(new TimeoutException(timeoutExMessage)); }); }