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

Open
#138,036 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
networking

Research direction

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.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C#
Stars
18.5k
Forks
5.8k
Avg merge
2d 18h
Merged PRs (30d)
588

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.