From dbcbdb7092bb457d7644096a6a62c243ead5a864 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 1 May 2024 23:38:28 +0000 Subject: [PATCH] Move Cancel call outside of lock and add additional error handling (#4055) Co-authored-by: Reuben Bond --- .../ServiceEndpointWatcher.cs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Extensions.ServiceDiscovery/ServiceEndpointWatcher.cs b/src/Microsoft.Extensions.ServiceDiscovery/ServiceEndpointWatcher.cs index d361202a69..a94b7b7a3c 100644 --- a/src/Microsoft.Extensions.ServiceDiscovery/ServiceEndpointWatcher.cs +++ b/src/Microsoft.Extensions.ServiceDiscovery/ServiceEndpointWatcher.cs @@ -77,8 +77,10 @@ public ValueTask GetEndpointsAsync(CancellationToken canc async ValueTask GetEndpointsInternal(CancellationToken cancellationToken) { ServiceEndpointSource? result; + var disposalToken = _disposalCancellation.Token; do { + disposalToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested(); await RefreshAsync(force: false).WaitAsync(cancellationToken).ConfigureAwait(false); result = _cachedEndpoints; @@ -194,7 +196,14 @@ private async Task RefreshAsyncInternal() if (OnEndpointsUpdated is { } callback) { - callback(new(newEndpoints, error)); + try + { + callback(new(newEndpoints, error)); + } + catch (Exception exception) + { + _logger.LogError(exception, "Error notifying observers of updated endpoints."); + } } lock (_lock) @@ -244,10 +253,17 @@ private void SchedulePollingTimer() /// public async ValueTask DisposeAsync() { - lock (_lock) + try { _disposalCancellation.Cancel(); + } + catch (Exception exception) + { + _logger.LogError(exception, "Error cancelling disposal cancellation token."); + } + lock (_lock) + { _changeTokenRegistration?.Dispose(); _changeTokenRegistration = null;