Skip to content

Commit

Permalink
use WaitForExitAsync instead of Exited event to ensure process output…
Browse files Browse the repository at this point in the history
… is read (#3275)

* use WaitForExitAsync instead of Exited event to ensure process output is read
  • Loading branch information
tskimmett committed Apr 8, 2024
1 parent e5605b8 commit 3d82b0f
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/Aspire.Hosting/Dcp/Process/ProcessUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,6 @@ public static (Task<ProcessResult>, IAsyncDisposable) Run(ProcessSpec processSpe

var processLifetimeTcs = new TaskCompletionSource<ProcessResult>();

process.Exited += (_, e) =>
{
startupComplete.Wait();
if (processSpec.ThrowOnNonZeroReturnCode && process.ExitCode != 0)
{
processLifetimeTcs.TrySetException(new InvalidOperationException(
$"Command {processSpec.ExecutablePath} {processSpec.Arguments} returned non-zero exit code {process.ExitCode}"));
}
else
{
processLifetimeTcs.TrySetResult(new ProcessResult(process.ExitCode));
}
};

try
{
#if ASPIRE_EVENTSOURCE
Expand All @@ -108,6 +93,21 @@ public static (Task<ProcessResult>, IAsyncDisposable) Run(ProcessSpec processSpe
process.BeginOutputReadLine();
process.BeginErrorReadLine();
processSpec.OnStart?.Invoke(process.Id);

process.WaitForExitAsync().ContinueWith(t =>
{
startupComplete.Wait();

This comment has been minimized.

Copy link
@davidfowl

davidfowl Apr 8, 2024

Member

We could make this async now no?

if (processSpec.ThrowOnNonZeroReturnCode && process.ExitCode != 0)
{
processLifetimeTcs.TrySetException(new InvalidOperationException(
$"Command {processSpec.ExecutablePath} {processSpec.Arguments} returned non-zero exit code {process.ExitCode}"));
}
else
{
processLifetimeTcs.TrySetResult(new ProcessResult(process.ExitCode));
}
}, TaskScheduler.Default);
}
finally
{
Expand Down

0 comments on commit 3d82b0f

Please sign in to comment.