ServiceEndpointWatcher logs normal cancellation as an error

未关闭 适合新手
#7,940 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@seragAhmed 已经在做这个了。

开始于 2026年11月12日。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp

调研方向

从 src/Libraries/Microsoft.Extensions.ServiceDiscovery/ServiceEndpointWatcher.cs 开始,重点检查 DisposeAsync 和 RefreshAsyncInternal,issue 将它们指出为取消和日志记录路径。检查现有的 ServiceEndpointWatcher 测试是否覆盖了后台刷新期间的释放情况。完成标准是这种正常取消不再记录解析错误,并且有一个测试覆盖该复现问题。

由索引模型根据 Issue 内容生成。

描述

bug untriaged
Description

Hi!

I'm getting Error resolving endpoints for service ... when a watcher is disposed during a background refresh. The refresh is cancelled by the watcher itself, but that cancellation ends up in the error log.

I ran into this with DNS discovery. The first lookup worked, then a background refresh was left waiting for a DNS response. About 20 seconds after the lookup, idle-watcher cleanup cancelled it and logged TaskCanceledException. The next lookup worked too.

Reproduction Steps

Here is a smaller repro with a custom provider, so there is no DNS server to set up.

Create a .NET 10 console app and add Microsoft.Extensions.ServiceDiscovery 10.11.0 and a framework reference to Microsoft.AspNetCore.App. Replace Program.cs with:

using System.Diagnostics.CodeAnalysis;
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ServiceDiscovery;

var factory = new SlowRefreshProvider();
var services = new ServiceCollection();
services.AddLogging(logging => logging.AddSimpleConsole());
services.AddServiceDiscoveryCore(options => options.RefreshPeriod = TimeSpan.FromMilliseconds(100));
services.AddSingleton<IServiceEndpointProviderFactory>(factory);

await using var provider = services.BuildServiceProvider();
var resolver = provider.GetRequiredService<ServiceEndpointResolver>();
await resolver.GetEndpointsAsync("http://sample", CancellationToken.None);
await factory.RefreshStarted.Task.WaitAsync(TimeSpan.FromSeconds(5));
Console.WriteLine("Disposing the resolver during a background refresh");
await resolver.DisposeAsync();
Console.WriteLine("Disposed successfully");

sealed class SlowRefreshProvider : IServiceEndpointProviderFactory, IServiceEndpointProvider
{
    private int calls;
    public TaskCompletionSource RefreshStarted { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);

    public bool TryCreateProvider(ServiceEndpointQuery query, [NotNullWhen(true)] out IServiceEndpointProvider? provider)
    {
        provider = this;
        return true;
    }

    public async ValueTask PopulateAsync(IServiceEndpointBuilder endpoints, CancellationToken cancellationToken)
    {
        if (Interlocked.Increment(ref calls) == 1)
        {
            endpoints.Endpoints.Add(ServiceEndpoint.Create(new DnsEndPoint("localhost", 8080)));
            return;
        }

        RefreshStarted.SetResult();
        await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken);
    }

    public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}

Run with dotnet run. The first lookup succeeds. Once the background refresh starts, the example disposes the resolver. The provider uses the supplied cancellation token and doesn't throw anything on its own.

Expected behavior

I'd expect cancellation during disposal to be handled without logging a resolution error.

Actual behavior
Disposing the resolver during a background refresh
fail: Microsoft.Extensions.ServiceDiscovery.ServiceEndpointWatcher[4]
      Error resolving endpoints for service 'http://sample'.
      System.Threading.Tasks.TaskCanceledException: A task was canceled.
         at SlowRefreshProvider.PopulateAsync(...)
         at Microsoft.Extensions.ServiceDiscovery.ServiceEndpointWatcher.RefreshAsyncInternal()
Disposed successfully
Regression?

No response

Known Workarounds

No response

Configuration

Tested with package 10.11.0, .NET SDK 10.0.403, macOS arm64.

Other information

From the watcher source for 10.11.0, this seems to happen because DisposeAsync cancels _disposalCancellation, and RefreshAsyncInternal logs the resulting exception as ResolutionFailed. Disposal suppresses the exception afterwards, but the error has already been logged.

The same catch/log path is still in main. I haven't tested an 11.x package.

主要语言
C#
星标
3.3k
派生
919
平均合并
2 天 2 小时
30 天内合并 PR
35

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/extensions 的其他 Issue

查看 dotnet/extensions 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。