11
11
using System . Windows . Forms ;
12
12
using Flow . Launcher . Infrastructure . Logger ;
13
13
using Flow . Launcher . Plugin ;
14
+ using ICSharpCode . SharpZipLib . Zip ;
14
15
using JetBrains . Annotations ;
15
16
using Microsoft . IO ;
16
17
@@ -234,9 +235,11 @@ protected string Execute(ProcessStartInfo startInfo)
234
235
235
236
protected async Task < Stream > ExecuteAsync ( ProcessStartInfo startInfo , CancellationToken token = default )
236
237
{
238
+ Process process = null ;
239
+ bool disposed = false ;
237
240
try
238
241
{
239
- using var process = Process . Start ( startInfo ) ;
242
+ process = Process . Start ( startInfo ) ;
240
243
if ( process == null )
241
244
{
242
245
Log . Error ( "|JsonRPCPlugin.ExecuteAsync|Can't start new process" ) ;
@@ -246,18 +249,21 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
246
249
await using var source = process . StandardOutput . BaseStream ;
247
250
248
251
var buffer = BufferManager . GetStream ( ) ;
249
- bool disposed = false ;
250
- process . Disposed += ( _ , _ ) => { disposed = true ; } ;
252
+
251
253
token . Register ( ( ) =>
252
254
{
253
- if ( ! disposed )
255
+ // ReSharper disable once AccessToDisposedClosure
256
+ // ReSharper disable once AccessToModifiedClosure
257
+ // Manually Check whether disposed
258
+ if ( ! disposed && ! process . HasExited )
254
259
// ReSharper disable once AccessToDisposedClosure
255
- // Manually Check whether disposed
256
260
process . Kill ( ) ;
257
261
} ) ;
258
262
259
263
try
260
264
{
265
+ // token expire won't instantly trigger the exception,
266
+ // manually kill process at before
261
267
await source . CopyToAsync ( buffer , token ) ;
262
268
}
263
269
catch ( OperationCanceledException )
@@ -269,7 +275,7 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
269
275
buffer . Seek ( 0 , SeekOrigin . Begin ) ;
270
276
271
277
token . ThrowIfCancellationRequested ( ) ;
272
-
278
+
273
279
if ( ! process . StandardError . EndOfStream )
274
280
{
275
281
using var standardError = process . StandardError ;
@@ -294,6 +300,11 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
294
300
e ) ;
295
301
return Stream . Null ;
296
302
}
303
+ finally
304
+ {
305
+ process ? . Dispose ( ) ;
306
+ disposed = true ;
307
+ }
297
308
}
298
309
299
310
public async Task < List < Result > > QueryAsync ( Query query , CancellationToken token )
0 commit comments