From 5c91e072a624cabc7f518e0befd1924ac71b585c Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Wed, 26 Apr 2023 18:09:56 +0900 Subject: [PATCH] Prevent payout processors from stalling restart --- BTCPayServer/HostedServices/BaseAsyncService.cs | 6 ++---- .../DelayedTransactionBroadcasterHostedService.cs | 4 ++-- BTCPayServer/HostedServices/DynamicDnsHostedService.cs | 6 +++--- .../HostedServices/NewVersionCheckerHostedService.cs | 4 ++-- BTCPayServer/HostedServices/RatesHostedService.cs | 6 +++--- .../Payments/Lightning/LightningPendingPayoutListener.cs | 6 +++--- .../PayoutProcessors/BaseAutomatedPayoutProcessor.cs | 2 +- BTCPayServer/Services/TorServices.cs | 2 +- 8 files changed, 17 insertions(+), 19 deletions(-) diff --git a/BTCPayServer/HostedServices/BaseAsyncService.cs b/BTCPayServer/HostedServices/BaseAsyncService.cs index bbfb6d0aa..26cb51af2 100644 --- a/BTCPayServer/HostedServices/BaseAsyncService.cs +++ b/BTCPayServer/HostedServices/BaseAsyncService.cs @@ -32,7 +32,7 @@ namespace BTCPayServer.HostedServices foreach (var t in _Tasks) t.ContinueWith(t => { - if (t.IsFaulted) + if (t.IsFaulted && !CancellationToken.IsCancellationRequested) Logs.PayServer.LogWarning(t.Exception, $"Unhanded exception in {this.GetType().Name}"); }, TaskScheduler.Default); return Task.CompletedTask; @@ -40,7 +40,7 @@ namespace BTCPayServer.HostedServices internal abstract Task[] InitializeTasks(); - protected CancellationToken Cancellation + protected CancellationToken CancellationToken { get { return _Cts.Token; } } @@ -69,8 +69,6 @@ namespace BTCPayServer.HostedServices } } - public CancellationToken CancellationToken => _Cts.Token; - public virtual async Task StopAsync(CancellationToken cancellationToken) { if (_Cts != null) diff --git a/BTCPayServer/HostedServices/DelayedTransactionBroadcasterHostedService.cs b/BTCPayServer/HostedServices/DelayedTransactionBroadcasterHostedService.cs index ae1654817..81147687e 100644 --- a/BTCPayServer/HostedServices/DelayedTransactionBroadcasterHostedService.cs +++ b/BTCPayServer/HostedServices/DelayedTransactionBroadcasterHostedService.cs @@ -28,8 +28,8 @@ namespace BTCPayServer.HostedServices { while (true) { - await _transactionBroadcaster.ProcessAll(Cancellation); - await Task.Delay(PollInternal, Cancellation); + await _transactionBroadcaster.ProcessAll(CancellationToken); + await Task.Delay(PollInternal, CancellationToken); } } } diff --git a/BTCPayServer/HostedServices/DynamicDnsHostedService.cs b/BTCPayServer/HostedServices/DynamicDnsHostedService.cs index da78d91eb..34aa3c169 100644 --- a/BTCPayServer/HostedServices/DynamicDnsHostedService.cs +++ b/BTCPayServer/HostedServices/DynamicDnsHostedService.cs @@ -30,7 +30,7 @@ namespace BTCPayServer.HostedServices readonly TimeSpan Period = TimeSpan.FromMinutes(60); async Task UpdateRecord() { - using (var timeout = CancellationTokenSource.CreateLinkedTokenSource(Cancellation)) + using (var timeout = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken)) { var settings = await SettingsRepository.GetSettingAsync() ?? new DynamicDnsSettings(); foreach (var service in settings.Services) @@ -59,9 +59,9 @@ namespace BTCPayServer.HostedServices } } } - using var delayCancel = CancellationTokenSource.CreateLinkedTokenSource(Cancellation); + using var delayCancel = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken); var delay = Task.Delay(Period, delayCancel.Token); - var changed = SettingsRepository.WaitSettingsChanged(Cancellation); + var changed = SettingsRepository.WaitSettingsChanged(CancellationToken); await Task.WhenAny(delay, changed); delayCancel.Cancel(); } diff --git a/BTCPayServer/HostedServices/NewVersionCheckerHostedService.cs b/BTCPayServer/HostedServices/NewVersionCheckerHostedService.cs index 2e88c0e66..875d4203a 100644 --- a/BTCPayServer/HostedServices/NewVersionCheckerHostedService.cs +++ b/BTCPayServer/HostedServices/NewVersionCheckerHostedService.cs @@ -44,7 +44,7 @@ namespace BTCPayServer.HostedServices { Logs.Events.LogError(ex, "Error while performing new version check"); } - await Task.Delay(TimeSpan.FromDays(1), Cancellation); + await Task.Delay(TimeSpan.FromDays(1), CancellationToken); } public async Task ProcessVersionCheck() @@ -52,7 +52,7 @@ namespace BTCPayServer.HostedServices var policies = await _settingsRepository.GetSettingAsync() ?? new PoliciesSettings(); if (policies.CheckForNewVersions) { - var tag = await _versionFetcher.Fetch(Cancellation); + var tag = await _versionFetcher.Fetch(CancellationToken); if (tag != null && tag != _env.Version) { var dh = await _settingsRepository.GetSettingAsync() ?? new NewVersionCheckerDataHolder(); diff --git a/BTCPayServer/HostedServices/RatesHostedService.cs b/BTCPayServer/HostedServices/RatesHostedService.cs index 7d65797a5..70ad24e5a 100644 --- a/BTCPayServer/HostedServices/RatesHostedService.cs +++ b/BTCPayServer/HostedServices/RatesHostedService.cs @@ -64,10 +64,10 @@ namespace BTCPayServer.HostedServices var usedProviders = GetStillUsedProviders().ToArray(); if (usedProviders.Length == 0) { - await Task.Delay(TimeSpan.FromSeconds(30), Cancellation); + await Task.Delay(TimeSpan.FromSeconds(30), CancellationToken); return; } - using (var timeout = CancellationTokenSource.CreateLinkedTokenSource(Cancellation)) + using (var timeout = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken)) { timeout.CancelAfter(TimeSpan.FromSeconds(20.0)); try @@ -97,7 +97,7 @@ namespace BTCPayServer.HostedServices await SaveRateCache(); } } - await Task.Delay(TimeSpan.FromSeconds(30), Cancellation); + await Task.Delay(TimeSpan.FromSeconds(30), CancellationToken); } public override async Task StartAsync(CancellationToken cancellationToken) diff --git a/BTCPayServer/Payments/Lightning/LightningPendingPayoutListener.cs b/BTCPayServer/Payments/Lightning/LightningPendingPayoutListener.cs index 50e641765..f1871aa28 100644 --- a/BTCPayServer/Payments/Lightning/LightningPendingPayoutListener.cs +++ b/BTCPayServer/Payments/Lightning/LightningPendingPayoutListener.cs @@ -101,7 +101,7 @@ public class LightningPendingPayoutListener : BaseAsyncService break; case PayoutLightningBlob payoutLightningBlob: { - var payment = await client.GetPayment(payoutLightningBlob.Id, Cancellation); + var payment = await client.GetPayment(payoutLightningBlob.Id, CancellationToken); if (payment is null) { continue; @@ -126,8 +126,8 @@ public class LightningPendingPayoutListener : BaseAsyncService } } - await context.SaveChangesAsync(Cancellation); - await Task.Delay(TimeSpan.FromSeconds(SecondsDelay), Cancellation); + await context.SaveChangesAsync(CancellationToken); + await Task.Delay(TimeSpan.FromSeconds(SecondsDelay), CancellationToken); } internal override Task[] InitializeTasks() diff --git a/BTCPayServer/PayoutProcessors/BaseAutomatedPayoutProcessor.cs b/BTCPayServer/PayoutProcessors/BaseAutomatedPayoutProcessor.cs index deb2e2a84..3eac03b41 100644 --- a/BTCPayServer/PayoutProcessors/BaseAutomatedPayoutProcessor.cs +++ b/BTCPayServer/PayoutProcessors/BaseAutomatedPayoutProcessor.cs @@ -74,7 +74,7 @@ public abstract class BaseAutomatedPayoutProcessor : BaseAsyncService where T States = new[] { PayoutState.AwaitingPayment }, PaymentMethods = new[] { _PayoutProcesserSettings.PaymentMethod }, Stores = new[] { _PayoutProcesserSettings.StoreId } - }, context); + }, context, CancellationToken); if (payouts.Any()) { Logs.PayServer.LogInformation($"{payouts.Count} found to process. Starting (and after will sleep for {blob.Interval})"); diff --git a/BTCPayServer/Services/TorServices.cs b/BTCPayServer/Services/TorServices.cs index ab8218c3a..59b882c9d 100644 --- a/BTCPayServer/Services/TorServices.cs +++ b/BTCPayServer/Services/TorServices.cs @@ -35,7 +35,7 @@ namespace BTCPayServer.Services } else { - await Task.Delay(TimeSpan.FromSeconds(120), Cancellation); + await Task.Delay(TimeSpan.FromSeconds(120), CancellationToken); } List result = new List(); try