From a631f55a37176b37e1e826749dbbc90c871907e8 Mon Sep 17 00:00:00 2001 From: Noi Date: Sat, 24 Oct 2020 22:46:53 -0700 Subject: [PATCH] Change exception type thrown by semaphore --- BackgroundServices/DataRetention.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/BackgroundServices/DataRetention.cs b/BackgroundServices/DataRetention.cs index dfefc0a..38ecf4a 100644 --- a/BackgroundServices/DataRetention.cs +++ b/BackgroundServices/DataRetention.cs @@ -1,5 +1,6 @@ using BirthdayBot.Data; using NpgsqlTypes; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -28,9 +29,17 @@ namespace BirthdayBot.BackgroundServices return; } - // A semaphore is used to restrict this work being done concurrently on other shards - // to avoid putting pressure on the SQL connection pool. Updating this is a low priority. - await _updateLock.WaitAsync(token).ConfigureAwait(false); + try + { + // A semaphore is used to restrict this work being done concurrently on other shards + // to avoid putting pressure on the SQL connection pool. Updating this is a low priority. + await _updateLock.WaitAsync(token).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + // Calling thread does not expect the exception that SemaphoreSlim throws... + throw new TaskCanceledException(); + } try { // Build a list of all values to update