From ddb25a253a88d07c932e5faef9a7af69e591cb01 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sun, 21 Aug 2022 20:58:17 -0400 Subject: [PATCH 1/5] Unregister Cancellation Event by disposing the delegate --- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index cf9d6f225..66f2bc952 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -3,6 +3,7 @@ using Flow.Launcher.Core.Resource; using Flow.Launcher.Infrastructure; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; @@ -254,24 +255,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 @@ -303,7 +297,7 @@ namespace Flow.Launcher.Core.Plugin if (errorBuffer.Length != 0) { using var error = new StreamReader(errorBuffer); - + var errorMessage = await error.ReadToEndAsync(); if (!string.IsNullOrEmpty(errorMessage)) From 2b60422a6c378baeaba4913ecbe85cfc46e8819e Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sun, 21 Aug 2022 20:59:31 -0400 Subject: [PATCH 2/5] Cleanup Using --- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index 66f2bc952..bc9c38670 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -1,34 +1,26 @@ -using Accessibility; -using Flow.Launcher.Core.Resource; +using Flow.Launcher.Core.Resource; using Flow.Launcher.Infrastructure; using System; using System.Collections.Generic; -using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; -using System.Reflection; 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 { From 74d929094481cbc269dcb3dea4b97d9772d61e43 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sun, 21 Aug 2022 22:00:12 -0400 Subject: [PATCH 3/5] Redesign exception stream handling and dispose stream to recycle --- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 49 +++++++++------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index bc9c38670..0f32137f1 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -85,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) @@ -132,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) { @@ -273,31 +277,18 @@ namespace Flow.Launcher.Core.Plugin return Stream.Null; } + switch (sourceBuffer.Length, errorBuffer.Position) + { + case (0, 0): + var errorMessage = Encoding.UTF8.GetString(errorBuffer.GetBuffer(), 0, (int)errorBuffer.Position); + 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; } From 793b432d61f2ddc37dc0b8f760d1d6a1441a6ec4 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sun, 21 Aug 2022 22:02:46 -0400 Subject: [PATCH 4/5] Use Length instead of Position to keep thing clear --- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index 0f32137f1..7003804b8 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -277,7 +277,7 @@ namespace Flow.Launcher.Core.Plugin return Stream.Null; } - switch (sourceBuffer.Length, errorBuffer.Position) + switch (sourceBuffer.Length, errorBuffer.Length) { case (0, 0): var errorMessage = Encoding.UTF8.GetString(errorBuffer.GetBuffer(), 0, (int)errorBuffer.Position); From f6e6cd5a4261fe985c3f611b9f727d85eb11a921 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sun, 21 Aug 2022 22:09:56 -0400 Subject: [PATCH 5/5] Fix Error Message for empty response --- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index 7003804b8..e3efcd296 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -280,7 +280,7 @@ namespace Flow.Launcher.Core.Plugin switch (sourceBuffer.Length, errorBuffer.Length) { case (0, 0): - var errorMessage = Encoding.UTF8.GetString(errorBuffer.GetBuffer(), 0, (int)errorBuffer.Position); + const string errorMessage = "Empty JSON-RPC Response."; Log.Warn($"|{nameof(JsonRPCPlugin)}.{nameof(ExecuteAsync)}|{errorMessage}"); break; case (_, not 0):