Merge pull request #1005 from Flow-Launcher/KillProcess

Use Cancellation Token to avoid potential race tracing issue
This commit is contained in:
Jeremy Wu 2022-07-21 20:25:11 +10:00 committed by GitHub
commit 4c4ba92b22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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