Use Cancellation Token to avoid potential race tracing issue

This commit is contained in:
Hongtao Zhang 2022-02-01 15:32:32 -06:00
parent eb5e9dd4be
commit 0ceefea2dd

View file

@ -241,7 +241,7 @@ namespace Flow.Launcher.Core.Plugin
protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, CancellationToken token = default) protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, CancellationToken token = default)
{ {
Process process = null; Process process = null;
bool disposed = false; using var exitTokenSource = new CancellationTokenSource();
try try
{ {
process = Process.Start(startInfo); process = Process.Start(startInfo);
@ -251,6 +251,7 @@ namespace Flow.Launcher.Core.Plugin
return Stream.Null; return Stream.Null;
} }
await using var source = process.StandardOutput.BaseStream; await using var source = process.StandardOutput.BaseStream;
var buffer = BufferManager.GetStream(); var buffer = BufferManager.GetStream();
@ -259,7 +260,7 @@ namespace Flow.Launcher.Core.Plugin
{ {
// ReSharper disable once AccessToModifiedClosure // ReSharper disable once AccessToModifiedClosure
// Manually Check whether disposed // Manually Check whether disposed
if (!disposed && !process.HasExited) if (!exitTokenSource.IsCancellationRequested && !process.HasExited)
process.Kill(); process.Kill();
}); });
@ -302,8 +303,8 @@ namespace Flow.Launcher.Core.Plugin
} }
finally finally
{ {
exitTokenSource.Cancel();
process?.Dispose(); process?.Dispose();
disposed = true;
} }
} }