runtime: System.Net.Http.HttpClient always fails on first attempted connection
Description
On the first attempt to connect to a web site, the connection always fails with a “Received an unexpected EOF or 0 bytes from the transport stream.” exception. On the second and subsequent attempts it works as expected.
This was first apparent to me because the behavior appeared suddenly in PowerShell 7.2.x+. A simple Invoke-WebRequest fails the first time, then works fine for the rest of the session. Meanwhile, PowerShell 5 works as expected on the same platform.
Discussion over in the PowerShell repo in topic 20813 led to trying a purely C# code test (below) that suggests this is not a PowerShell issue, but some weird edge case that is surfaced in .NET
I can reproduce the behavior on multiple systems in my organization, on the LAN or not, even completely disconnected from the network using a loopback web server. I can also reproduce it under Windows Subsystem for Linux (kali) using Mono (6.8.0.105).
Reproduction Steps
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
namespace HTTP_Test
{
class program
{
static void Main()
{
Task t = new Task(HTTP_GET);
t.Start();
Console.ReadLine();
}
static async void HTTP_GET()
{
var TARGETURL = "https://www.google.com/";
HttpClientHandler handler = new HttpClientHandler();
Console.WriteLine("GET: + " + TARGETURL);
// ... Use HttpClient.
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync(TARGETURL);
HttpContent content = response.Content;
// ... Check Status Code
Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);
}
}
}
Expected behavior
Network connection should work the first time.
Actual behavior
Connection fails on first attempt every time.
Regression?
No response
Known Workarounds
Use PowerShell 5.
Configuration
My system is .NET release 533325, Windows 10 22H2 (19045.3570) on an x64.
Has been reproduced on multiple versions of Windows 10 on various hardware platforms, including a VDI. This has also been reproduced under Windows Subsystem for Linux on my laptop.
Other information
No response
About this issue
- Original URL
- State: open
- Created 7 months ago
- Comments: 28 (17 by maintainers)
TL;DR: Here’s a modified test case based on yours, please run it (verify that the issue happens) and share the output with us. HttpTest-5.0-p8.zip
Hi @mkellen1, sorry for the delay. Can I please ask you to also collect the extended telemetry while the issue happens? It is very strange that there are no packages for the first connection attempt, but we should at least see it in the telemetry.
Official docs for networking telemetry are here, but we also need internal diagnostic event sources which are not listed on this page; also, as a funny coincidence, the names of these internal event sources changed right between 5.0-p8 and 5.0-rc1 😄 (https://github.com/dotnet/runtime/commit/ba7c518a494ef7aa87159816e07576eefe14290f) – so I went ahead and wrote for you an event listener class to collect everything:
NetEventListener
Then you need to create an instance at the start of your test app. Here’s the modified test loosely based on what you’ve sent above:
Program
As a reference, this is my output from it:
5.0 Preview 8
5.0 RC 1
The server is also a default Apache listening on localhost.
The whole project for the preview 8 version is in HttpTest-5.0-p8.zip. For rc1, you’d need to change SDK in global.json:
I don’t know whether you already leveraging local .NET installations, but just in case, here’s how to add it (example is for Windows, but there are also docs for Linux in https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script):
To verify, you can call
dotnet --infoFor RC 1, it would be
-Version 5.0.100-rc.1.20452.10insteadI went back and downloaded Powershell versions until I found exactly where it broke. The last version that works for me is 7.1.0-preview.7. The next version (7.1.0-rc.1) fails. Each had a different release of .NET 5.
Sure enough, the code works in dotnet-5.0.100-preview.8 and fails in dotnet-5.0.100-rc.1, matching the powershell breakage.
Many thanks for the detailed response. It may take me a bit to implement it all, but I will get back to you as soon as I can with the results.
TestCase.zip
The loopback server is just a default Apache listening on 127.0.0.1
(Hopefully I’m not too far off of the correct way to do this, I’m not a .NET developer, just a guy who is trying to get his PowerShell scripts to work again.)
Disabling each in turn made no difference (nor did turning them both off together).