From fe8428b8b05e34054a1b1eaed6a0ca14ca0b14e8 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 13 Jul 2018 19:56:19 +0900 Subject: [PATCH] make sure the LndInvoiceClientSession get disposed, even if it fails at initialization --- .../Lightning/Lnd/LndInvoiceClient.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/BTCPayServer/Payments/Lightning/Lnd/LndInvoiceClient.cs b/BTCPayServer/Payments/Lightning/Lnd/LndInvoiceClient.cs index 9e3f4c626..b7bf26907 100644 --- a/BTCPayServer/Payments/Lightning/Lnd/LndInvoiceClient.cs +++ b/BTCPayServer/Payments/Lightning/Lnd/LndInvoiceClient.cs @@ -41,16 +41,24 @@ namespace BTCPayServer.Payments.Lightning.Lnd public async Task StartListening() { - _Client = _Parent.CreateHttpClient(); - _Client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite); - var request = new HttpRequestMessage(HttpMethod.Get, _Parent.BaseUrl.WithTrailingSlash() + "v1/invoices/subscribe"); - _Response = await _Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, _Cts.Token); - _Body = await _Response.Content.ReadAsStreamAsync(); - _Reader = new StreamReader(_Body); + try + { + _Client = _Parent.CreateHttpClient(); + _Client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite); + var request = new HttpRequestMessage(HttpMethod.Get, _Parent.BaseUrl.WithTrailingSlash() + "v1/invoices/subscribe"); + _Response = await _Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, _Cts.Token); + _Body = await _Response.Content.ReadAsStreamAsync(); + _Reader = new StreamReader(_Body); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed - ListenLoop(); + ListenLoop(); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + } + catch + { + _Stopped.Set(); + Dispose(); + } } private async Task ListenLoop()