Skip to content

Commit 8e01add

Browse files
authored
Move Cancel call outside of lock and add additional error handling (dotnet#4052)
1 parent 3dff5fb commit 8e01add

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Microsoft.Extensions.ServiceDiscovery/ServiceEndpointWatcher.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ public ValueTask<ServiceEndpointSource> GetEndpointsAsync(CancellationToken canc
7777
async ValueTask<ServiceEndpointSource> GetEndpointsInternal(CancellationToken cancellationToken)
7878
{
7979
ServiceEndpointSource? result;
80+
var disposalToken = _disposalCancellation.Token;
8081
do
8182
{
83+
disposalToken.ThrowIfCancellationRequested();
8284
cancellationToken.ThrowIfCancellationRequested();
8385
await RefreshAsync(force: false).WaitAsync(cancellationToken).ConfigureAwait(false);
8486
result = _cachedEndpoints;
@@ -194,7 +196,14 @@ private async Task RefreshAsyncInternal()
194196

195197
if (OnEndpointsUpdated is { } callback)
196198
{
197-
callback(new(newEndpoints, error));
199+
try
200+
{
201+
callback(new(newEndpoints, error));
202+
}
203+
catch (Exception exception)
204+
{
205+
_logger.LogError(exception, "Error notifying observers of updated endpoints.");
206+
}
198207
}
199208

200209
lock (_lock)
@@ -244,10 +253,17 @@ private void SchedulePollingTimer()
244253
/// <inheritdoc/>
245254
public async ValueTask DisposeAsync()
246255
{
247-
lock (_lock)
256+
try
248257
{
249258
_disposalCancellation.Cancel();
259+
}
260+
catch (Exception exception)
261+
{
262+
_logger.LogError(exception, "Error cancelling disposal cancellation token.");
263+
}
250264

265+
lock (_lock)
266+
{
251267
_changeTokenRegistration?.Dispose();
252268
_changeTokenRegistration = null;
253269

0 commit comments

Comments
 (0)