Skip to content

Commit d7c037d

Browse files
committed
fix dispose issue by manually dispose
1 parent 220db44 commit d7c037d

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Windows.Forms;
1212
using Flow.Launcher.Infrastructure.Logger;
1313
using Flow.Launcher.Plugin;
14+
using ICSharpCode.SharpZipLib.Zip;
1415
using JetBrains.Annotations;
1516
using Microsoft.IO;
1617

@@ -234,9 +235,11 @@ protected string Execute(ProcessStartInfo startInfo)
234235

235236
protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, CancellationToken token = default)
236237
{
238+
Process process = null;
239+
bool disposed = false;
237240
try
238241
{
239-
using var process = Process.Start(startInfo);
242+
process = Process.Start(startInfo);
240243
if (process == null)
241244
{
242245
Log.Error("|JsonRPCPlugin.ExecuteAsync|Can't start new process");
@@ -246,18 +249,21 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
246249
await using var source = process.StandardOutput.BaseStream;
247250

248251
var buffer = BufferManager.GetStream();
249-
bool disposed = false;
250-
process.Disposed += (_, _) => { disposed = true; };
252+
251253
token.Register(() =>
252254
{
253-
if (!disposed)
255+
// ReSharper disable once AccessToDisposedClosure
256+
// ReSharper disable once AccessToModifiedClosure
257+
// Manually Check whether disposed
258+
if (!disposed && !process.HasExited)
254259
// ReSharper disable once AccessToDisposedClosure
255-
// Manually Check whether disposed
256260
process.Kill();
257261
});
258262

259263
try
260264
{
265+
// token expire won't instantly trigger the exception,
266+
// manually kill process at before
261267
await source.CopyToAsync(buffer, token);
262268
}
263269
catch (OperationCanceledException)
@@ -269,7 +275,7 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
269275
buffer.Seek(0, SeekOrigin.Begin);
270276

271277
token.ThrowIfCancellationRequested();
272-
278+
273279
if (!process.StandardError.EndOfStream)
274280
{
275281
using var standardError = process.StandardError;
@@ -294,6 +300,11 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
294300
e);
295301
return Stream.Null;
296302
}
303+
finally
304+
{
305+
process?.Dispose();
306+
disposed = true;
307+
}
297308
}
298309

299310
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)

0 commit comments

Comments
 (0)