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 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
588

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/runtime のほかの issue

dotnet/runtime の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。