From d7c037d1562237757f293637731a089bd174bd4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BC=98=E9=9F=AC?= Date: Tue, 27 Jul 2021 14:11:48 +0800 Subject: [PATCH] fix dispose issue by manually dispose --- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 23 ++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index 59980a611..08836b56f 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using System.Windows.Forms; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Plugin; +using ICSharpCode.SharpZipLib.Zip; using JetBrains.Annotations; using Microsoft.IO; @@ -234,9 +235,11 @@ namespace Flow.Launcher.Core.Plugin protected async Task ExecuteAsync(ProcessStartInfo startInfo, CancellationToken token = default) { + Process process = null; + bool disposed = false; try { - using var process = Process.Start(startInfo); + process = Process.Start(startInfo); if (process == null) { Log.Error("|JsonRPCPlugin.ExecuteAsync|Can't start new process"); @@ -246,18 +249,21 @@ namespace Flow.Launcher.Core.Plugin await using var source = process.StandardOutput.BaseStream; var buffer = BufferManager.GetStream(); - bool disposed = false; - process.Disposed += (_, _) => { disposed = true; }; + token.Register(() => { - if (!disposed) + // ReSharper disable once AccessToDisposedClosure + // ReSharper disable once AccessToModifiedClosure + // Manually Check whether disposed + if (!disposed && !process.HasExited) // ReSharper disable once AccessToDisposedClosure - // Manually Check whether disposed process.Kill(); }); try { + // token expire won't instantly trigger the exception, + // manually kill process at before await source.CopyToAsync(buffer, token); } catch (OperationCanceledException) @@ -269,7 +275,7 @@ namespace Flow.Launcher.Core.Plugin buffer.Seek(0, SeekOrigin.Begin); token.ThrowIfCancellationRequested(); - + if (!process.StandardError.EndOfStream) { using var standardError = process.StandardError; @@ -294,6 +300,11 @@ namespace Flow.Launcher.Core.Plugin e); return Stream.Null; } + finally + { + process?.Dispose(); + disposed = true; + } } public async Task> QueryAsync(Query query, CancellationToken token)