Skip to content

WaitForExitAsync: Handle timeout exception when timeout parameter set #505

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -116,5 +116,34 @@ public async Task ProcessProxyExitTimesAreNotAffectedByTheProcessHavingBeenDispo
DateTime exitTime = process.ExitTime;
Assert.IsTrue(exitTime != DateTime.MinValue);
}

[Test]
public async Task ProcessProxyWaitForExitAsyncHandlesTimeoutExceptionAsExpected()
{
IProcessProxy process = null;
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "ping",
Arguments = "localhost -n 2", // This will run for about 2 seconds
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};

using (process = new ProcessProxy(new Process { StartInfo = startInfo }))
{
// Test Case 1: When timeout is null, no TimeoutException should be thrown
// The process will complete normally
await process.StartAndWaitAsync(CancellationToken.None);
Assert.IsTrue(process.HasExited);

// Test Case 2: When timeout is specified and process takes longer, TimeoutException should be caught
process = new ProcessProxy(new Process { StartInfo = startInfo });
await process.StartAndWaitAsync(CancellationToken.None, TimeSpan.FromMilliseconds(100));
// If we get here, the TimeoutException was caught as expected
Assert.IsTrue(true);
}
}
}
}
4 changes: 4 additions & 0 deletions src/VirtualClient/VirtualClient.Common/ProcessProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ public virtual async Task WaitForExitAsync(CancellationToken cancellationToken,
{
// Expected whenever the CancellationToken receives a cancellation request.
}
catch (TimeoutException) when (timeout != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should NOT be swallowed here. There are upstream cases where the caller of this logic needs to know if the process did not actually complete within the timeout allotted. TimeoutException should be handled in the calling locations, not the core method.

{
// Expected timeout when timeout was specified.
}
finally
{
this.exitTime = DateTime.UtcNow;
Expand Down
Loading