From 036897ecab279de89954824882fff290877e17f0 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Fri, 27 Sep 2024 08:51:55 +0200 Subject: [PATCH] Remove request from requests dict after it was handled. This was a memory leak! --- package.json | 2 +- templates/CSharp/JRpcClient.cs | 38 +++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 2f9b4d6..ac46179 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/jrpcgen", - "version": "1.2.19", + "version": "1.2.20", "main": "lib/index.js", "license": "MIT", "packageManager": "yarn@3.1.1", diff --git a/templates/CSharp/JRpcClient.cs b/templates/CSharp/JRpcClient.cs index d994699..d58d23f 100644 --- a/templates/CSharp/JRpcClient.cs +++ b/templates/CSharp/JRpcClient.cs @@ -89,27 +89,41 @@ public class JRpcClient var task = new TaskCompletionSource(); this.Requests.Add(id, task); - _ = Task.Run(async () => - { - await this.Transport.Write(request.ToJsonString()); - }); - if (this.Timeout.HasValue) + try { - if (await Task.WhenAny(task.Task, Task.Delay(this.Timeout.Value)) == task.Task) + _ = Task.Run(async () => { - return await task.Task; + try + { + await this.Transport.Write(request.ToJsonString()); + } + catch (Exception e) + { + task.SetException(e); + } + }); + + if (this.Timeout.HasValue) + { + if (await Task.WhenAny(task.Task, Task.Delay(this.Timeout.Value)) == task.Task) + { + return await task.Task; + } + else + { + throw new JRpcTimeoutException(); + } + } else { - this.Requests.Remove(id); - throw new JRpcTimeoutException(); + return await task.Task; } - } - else + finally { - return await task.Task; + this.Requests.Remove(id); } }