Connect to a closed loopback port takes 2 seconds per address on Windows; libuv, curl and Chromium avoid it with SIO_TCP_INITIAL_RTO

未关闭
#138,036 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
csharp
领域
networking

调研方向

Start by reading the Windows socket connection code in System.Net.Sockets and the related tests for loopback connections. Check how the proposed SIO_TCP_INITIAL_RTO ioctl is supported across Windows versions and where failures can be safely ignored. Done means refused loopback connections fail faster while successful connections and non-loopback connections remain unaffected.

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

描述

area-System.Net.Sockets untriaged
Description

On Windows, connecting to a loopback port where nothing listens takes about 2 seconds before SocketException ConnectionRefused. For localhost it's about 4 seconds, because ::1 and then 127.0.0.1 each wait. On Linux the same call fails in about a millisecond.

Repro with .NET 10.0.14 on Windows 11 26100.

var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(IPAddress.Loopback, 59321); // ~2,030 ms, then ConnectionRefused

HttpClient.GetAsync("http://localhost:59321/") takes about 4,080 ms. On WSL2 on the same machine, both take 1 to 3 ms.

Configuration

.NET 10.0.14, Windows 11 26100 x64 (Intel Core i9-10900K). Linux numbers are from WSL2 on the same machine with the same .NET.

Regression?

No. #65375 and #31085 reported the localhost part and were closed as OS behavior.

Data

From .NET the same thing works today with socket.IOControl(unchecked((int)0x98000011), new byte[] { 0, 0, 254, 0 }, null) before Connect. With it the refused connect takes 2.6 ms for 127.0.0.1 and 1.1 ms for ::1, and an HttpClient with a ConnectCallback that does this gets http://localhost down from 4,083 ms to 9.5 ms. Connects that succeed are unchanged.

Analysis

The time is Windows retransmitting the SYN after the RST. It can be turned off per socket, and libuv (Node), curl (curl/curl#22494) and Chromium do that for loopback destinations.

TCP_INITIAL_RTO_PARAMETERS p = { TCP_INITIAL_RTO_DEFAULT_RTT, TCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONS };
WSAIoctl(sock, SIO_TCP_INITIAL_RTO, &p, sizeof(p), NULL, 0, &bytes, NULL, NULL);

There's no system-wide way to get this. Set-NetTCPSetting accepts MaxSynRetransmissions from 2 to 8 only.

Would you take a change that sets this ioctl in System.Net.Sockets on Windows when the remote address is loopback, ignoring failures (older Windows)? It only affects loopback, where there's no link to lose a SYN on. It matters for tools and test suites that poll for a local server to come up. PowerShell's Invoke-WebRequest pays 4 s per miss on localhost today.

主要语言
C#
星标
18.5k
派生
5.8k
平均合并
2 天 18 小时
30 天内合并 PR
588

环境准备

在 Codespaces 中打开

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

从这里开始

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

dotnet/runtime 的其他 Issue

查看 dotnet/runtime 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

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