diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index cf9d6f225..e3efcd296 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -1,33 +1,27 @@ -using Accessibility; -using Flow.Launcher.Core.Resource; +using Flow.Launcher.Core.Resource; using Flow.Launcher.Infrastructure; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; -using System.Reflection; +using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; -using ICSharpCode.SharpZipLib.Zip; -using JetBrains.Annotations; using Microsoft.IO; -using System.Text.Json.Serialization; using System.Windows; using System.Windows.Controls; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; using CheckBox = System.Windows.Controls.CheckBox; using Control = System.Windows.Controls.Control; -using Label = System.Windows.Controls.Label; using Orientation = System.Windows.Controls.Orientation; using TextBox = System.Windows.Controls.TextBox; using UserControl = System.Windows.Controls.UserControl; -using System.Windows.Data; namespace Flow.Launcher.Core.Plugin { @@ -92,12 +86,15 @@ namespace Flow.Launcher.Core.Plugin private async Task> DeserializedResultAsync(Stream output) { - if (output == Stream.Null) return null; + await using (output) + { + if (output == Stream.Null) return null; - var queryResponseModel = - await JsonSerializer.DeserializeAsync(output, options); + var queryResponseModel = + await JsonSerializer.DeserializeAsync(output, options); - return ParseResults(queryResponseModel); + return ParseResults(queryResponseModel); + } } private List DeserializedResult(string output) @@ -139,7 +136,7 @@ namespace Flow.Launcher.Core.Plugin } else { - var actionResponse = await RequestAsync(result.JsonRPCAction); + await using var actionResponse = await RequestAsync(result.JsonRPCAction); if (actionResponse.Length == 0) { @@ -254,24 +251,17 @@ namespace Flow.Launcher.Core.Plugin return Stream.Null; } - - var sourceBuffer = BufferManager.GetStream(); - var errorBuffer = BufferManager.GetStream(); + using var errorBuffer = BufferManager.GetStream(); var sourceCopyTask = process.StandardOutput.BaseStream.CopyToAsync(sourceBuffer, token); var errorCopyTask = process.StandardError.BaseStream.CopyToAsync(errorBuffer, token); - - token.Register(() => + + await using var registeredEvent = token.Register(() => { - try - { - if (!process.HasExited) - process.Kill(); - } - catch (InvalidOperationException) - { - } + if (!process.HasExited) + process.Kill(); + sourceBuffer.Dispose(); }); try @@ -287,31 +277,18 @@ namespace Flow.Launcher.Core.Plugin return Stream.Null; } + switch (sourceBuffer.Length, errorBuffer.Length) + { + case (0, 0): + const string errorMessage = "Empty JSON-RPC Response."; + Log.Warn($"|{nameof(JsonRPCPlugin)}.{nameof(ExecuteAsync)}|{errorMessage}"); + break; + case (_, not 0): + throw new InvalidDataException(Encoding.UTF8.GetString(errorBuffer.ToArray())); // The process has exited with an error message + } + sourceBuffer.Seek(0, SeekOrigin.Begin); - - token.ThrowIfCancellationRequested(); - - if (sourceBuffer.Length == 0) - { - var errorMessage = errorBuffer.Length == 0 ? - "Empty JSONRPC Response" : - await process.StandardError.ReadToEndAsync(); - - Log.Error($"{context.CurrentPluginMetadata.Name}|{errorMessage}"); - } - - if (errorBuffer.Length != 0) - { - using var error = new StreamReader(errorBuffer); - - var errorMessage = await error.ReadToEndAsync(); - - if (!string.IsNullOrEmpty(errorMessage)) - { - Log.Error($"|{context.CurrentPluginMetadata.Name}.{nameof(ExecuteAsync)}|{errorMessage}"); - } - } - + return sourceBuffer; }