Skip to content

Commit 2c790bf

Browse files
authored
Merge pull request #1341 from Flow-Launcher/jsonrpc_empty_response
[Dev] Unregister Cancellation Event by disposing the delegate
2 parents 7686f61 + 0c1aabb commit 2c790bf

File tree

1 file changed

+25
-48
lines changed

1 file changed

+25
-48
lines changed

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
1-
using Accessibility;
2-
using Flow.Launcher.Core.Resource;
1+
using Flow.Launcher.Core.Resource;
32
using Flow.Launcher.Infrastructure;
43
using System;
54
using System.Collections.Generic;
65
using System.Diagnostics;
76
using System.IO;
87
using System.Linq;
9-
using System.Reflection;
8+
using System.Text;
109
using System.Text.Json;
1110
using System.Threading;
1211
using System.Threading.Tasks;
1312
using Flow.Launcher.Infrastructure.Logger;
1413
using Flow.Launcher.Infrastructure.UserSettings;
1514
using Flow.Launcher.Plugin;
16-
using ICSharpCode.SharpZipLib.Zip;
17-
using JetBrains.Annotations;
1815
using Microsoft.IO;
19-
using System.Text.Json.Serialization;
2016
using System.Windows;
2117
using System.Windows.Controls;
2218
using YamlDotNet.Serialization;
2319
using YamlDotNet.Serialization.NamingConventions;
2420
using CheckBox = System.Windows.Controls.CheckBox;
2521
using Control = System.Windows.Controls.Control;
26-
using Label = System.Windows.Controls.Label;
2722
using Orientation = System.Windows.Controls.Orientation;
2823
using TextBox = System.Windows.Controls.TextBox;
2924
using UserControl = System.Windows.Controls.UserControl;
30-
using System.Windows.Data;
3125

3226
namespace Flow.Launcher.Core.Plugin
3327
{
@@ -92,12 +86,15 @@ public List<Result> LoadContextMenus(Result selectedResult)
9286

9387
private async Task<List<Result>> DeserializedResultAsync(Stream output)
9488
{
95-
if (output == Stream.Null) return null;
89+
await using (output)
90+
{
91+
if (output == Stream.Null) return null;
9692

97-
var queryResponseModel =
98-
await JsonSerializer.DeserializeAsync<JsonRPCQueryResponseModel>(output, options);
93+
var queryResponseModel =
94+
await JsonSerializer.DeserializeAsync<JsonRPCQueryResponseModel>(output, options);
9995

100-
return ParseResults(queryResponseModel);
96+
return ParseResults(queryResponseModel);
97+
}
10198
}
10299

103100
private List<Result> DeserializedResult(string output)
@@ -139,7 +136,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
139136
}
140137
else
141138
{
142-
var actionResponse = await RequestAsync(result.JsonRPCAction);
139+
await using var actionResponse = await RequestAsync(result.JsonRPCAction);
143140

144141
if (actionResponse.Length == 0)
145142
{
@@ -254,24 +251,17 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
254251
return Stream.Null;
255252
}
256253

257-
258-
259254
var sourceBuffer = BufferManager.GetStream();
260-
var errorBuffer = BufferManager.GetStream();
255+
using var errorBuffer = BufferManager.GetStream();
261256

262257
var sourceCopyTask = process.StandardOutput.BaseStream.CopyToAsync(sourceBuffer, token);
263258
var errorCopyTask = process.StandardError.BaseStream.CopyToAsync(errorBuffer, token);
264-
265-
token.Register(() =>
259+
260+
await using var registeredEvent = token.Register(() =>
266261
{
267-
try
268-
{
269-
if (!process.HasExited)
270-
process.Kill();
271-
}
272-
catch (InvalidOperationException)
273-
{
274-
}
262+
if (!process.HasExited)
263+
process.Kill();
264+
sourceBuffer.Dispose();
275265
});
276266

277267
try
@@ -287,31 +277,18 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
287277
return Stream.Null;
288278
}
289279

290-
sourceBuffer.Seek(0, SeekOrigin.Begin);
291-
292-
token.ThrowIfCancellationRequested();
293-
294-
if (sourceBuffer.Length == 0)
295-
{
296-
var errorMessage = errorBuffer.Length == 0 ?
297-
"Empty JSONRPC Response" :
298-
await process.StandardError.ReadToEndAsync();
299-
300-
Log.Error($"{context.CurrentPluginMetadata.Name}|{errorMessage}");
301-
}
302-
303-
if (errorBuffer.Length != 0)
280+
switch (sourceBuffer.Length, errorBuffer.Length)
304281
{
305-
using var error = new StreamReader(errorBuffer);
306-
307-
var errorMessage = await error.ReadToEndAsync();
308-
309-
if (!string.IsNullOrEmpty(errorMessage))
310-
{
311-
Log.Error($"|{context.CurrentPluginMetadata.Name}.{nameof(ExecuteAsync)}|{errorMessage}");
312-
}
282+
case (0, 0):
283+
const string errorMessage = "Empty JSON-RPC Response.";
284+
Log.Warn($"|{nameof(JsonRPCPlugin)}.{nameof(ExecuteAsync)}|{errorMessage}");
285+
break;
286+
case (_, not 0):
287+
throw new InvalidDataException(Encoding.UTF8.GetString(errorBuffer.ToArray())); // The process has exited with an error message
313288
}
314289

290+
sourceBuffer.Seek(0, SeekOrigin.Begin);
291+
315292
return sourceBuffer;
316293
}
317294

0 commit comments

Comments
 (0)