From 150e4b842cfe27ebdb11089c82e157b82969215d Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Wed, 6 Oct 2021 13:22:55 +0900 Subject: [PATCH] Make sure the process doesn't crash if exception raised in Subscribe --- BTCPayServer/Controllers/InvoiceController.UI.cs | 6 +++--- BTCPayServer/Controllers/NotificationsController.cs | 2 +- BTCPayServer/EventAggregator.cs | 4 ++++ BTCPayServer/HostedServices/InvoiceNotificationManager.cs | 2 +- BTCPayServer/HostedServices/InvoiceWatcher.cs | 2 +- BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs | 2 +- BTCPayServer/Payments/Lightning/LightningListener.cs | 4 ++-- 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 1d41b4455..cd2ed90cd 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -665,9 +665,9 @@ namespace BTCPayServer.Controllers CompositeDisposable leases = new CompositeDisposable(); try { - leases.Add(_EventAggregator.Subscribe(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId))); - leases.Add(_EventAggregator.Subscribe(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId))); - leases.Add(_EventAggregator.Subscribe(async o => await NotifySocket(webSocket, o.Invoice.Id, invoiceId))); + leases.Add(_EventAggregator.SubscribeAsync(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId))); + leases.Add(_EventAggregator.SubscribeAsync(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId))); + leases.Add(_EventAggregator.SubscribeAsync(async o => await NotifySocket(webSocket, o.Invoice.Id, invoiceId))); while (true) { var message = await webSocket.ReceiveAndPingAsync(DummyBuffer, default(CancellationToken)); diff --git a/BTCPayServer/Controllers/NotificationsController.cs b/BTCPayServer/Controllers/NotificationsController.cs index 40e697327..c768b69e1 100644 --- a/BTCPayServer/Controllers/NotificationsController.cs +++ b/BTCPayServer/Controllers/NotificationsController.cs @@ -62,7 +62,7 @@ namespace BTCPayServer.Controllers IEventAggregatorSubscription subscription = null; try { - subscription = _eventAggregator.Subscribe(async evt => + subscription = _eventAggregator.SubscribeAsync(async evt => { if (evt.UserId == userId) { diff --git a/BTCPayServer/EventAggregator.cs b/BTCPayServer/EventAggregator.cs index fb2385e3a..d15f7cbb3 100644 --- a/BTCPayServer/EventAggregator.cs +++ b/BTCPayServer/EventAggregator.cs @@ -145,6 +145,10 @@ namespace BTCPayServer return Subscribe(new Action((sub, t) => subscription(sub, t))); } + public IEventAggregatorSubscription SubscribeAsync(Func subscription) + { + return Subscribe(new Action((sub, t) => _ = subscription(t))); + } public IEventAggregatorSubscription Subscribe(Action subscription) { return Subscribe(new Action((sub, t) => subscription(t))); diff --git a/BTCPayServer/HostedServices/InvoiceNotificationManager.cs b/BTCPayServer/HostedServices/InvoiceNotificationManager.cs index a13913f9f..d8390a1c8 100644 --- a/BTCPayServer/HostedServices/InvoiceNotificationManager.cs +++ b/BTCPayServer/HostedServices/InvoiceNotificationManager.cs @@ -309,7 +309,7 @@ namespace BTCPayServer.HostedServices readonly CompositeDisposable leases = new CompositeDisposable(); public Task StartAsync(CancellationToken cancellationToken) { - leases.Add(_EventAggregator.Subscribe(async e => + leases.Add(_EventAggregator.SubscribeAsync(async e => { if (e.EventCode == InvoiceEventCode.PaymentSettled) { diff --git a/BTCPayServer/HostedServices/InvoiceWatcher.cs b/BTCPayServer/HostedServices/InvoiceWatcher.cs index e993637c1..9d923db47 100644 --- a/BTCPayServer/HostedServices/InvoiceWatcher.cs +++ b/BTCPayServer/HostedServices/InvoiceWatcher.cs @@ -281,7 +281,7 @@ namespace BTCPayServer.HostedServices { Watch(b.InvoiceId); })); - leases.Add(_eventAggregator.Subscribe(async b => + leases.Add(_eventAggregator.SubscribeAsync(async b => { if (InvoiceEventNotification.HandlesEvent(b.Name)) { diff --git a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs index e10a91d2a..07aa86e93 100644 --- a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs +++ b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs @@ -75,7 +75,7 @@ namespace BTCPayServer.Payments.Bitcoin { _RunningTask = new TaskCompletionSource(); _Cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - leases.Add(_Aggregator.Subscribe(async nbxplorerEvent => + leases.Add(_Aggregator.SubscribeAsync(async nbxplorerEvent => { if (nbxplorerEvent.NewState == NBXplorerState.Ready) { diff --git a/BTCPayServer/Payments/Lightning/LightningListener.cs b/BTCPayServer/Payments/Lightning/LightningListener.cs index 98480c95c..720284934 100644 --- a/BTCPayServer/Payments/Lightning/LightningListener.cs +++ b/BTCPayServer/Payments/Lightning/LightningListener.cs @@ -143,7 +143,7 @@ namespace BTCPayServer.Payments.Lightning readonly CompositeDisposable leases = new CompositeDisposable(); public Task StartAsync(CancellationToken cancellationToken) { - leases.Add(_Aggregator.Subscribe(async inv => + leases.Add(_Aggregator.SubscribeAsync(async inv => { if (inv.Name == InvoiceEvent.Created) { @@ -159,7 +159,7 @@ namespace BTCPayServer.Payments.Lightning } } })); - leases.Add(_Aggregator.Subscribe(async inv => + leases.Add(_Aggregator.SubscribeAsync(async inv => { if (inv.State.Status == InvoiceStatusLegacy.New && inv.State.ExceptionStatus == InvoiceExceptionStatus.PaidPartial)