Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Security and hardening · Troubleshooting walkthrough

.NET SslStream reports that the server closed the connection with 0 bytes

Short answer

Received an unexpected EOF or 0 bytes from the transport stream means the peer closed the connection while SslStream still expected TLS data. During a Linux handshake, .NET 5 and later offer only ECDHE suites unless openssl.cnf sets others, so servers limited to RSA key exchange hang up. Mid-download, the server or a proxy cut the response short.

  • Applies to .NET 11.0.0 and .NET 10.0.15 on Linux, OpenSSL 3 s_client
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Received an unexpected EOF or 0 bytes from the transport stream means the other end closed the connection while SslStream was still waiting for TLS data. The message does not say why. What matters is when it happened, because the same exception has two unrelated meanings: the server hung up during the handshake, or it hung up in the middle of the response.

Both start with the same probe. Open a TLS session to the server from the same machine or container with OpenSSL, and see what it chooses.

The text you see

  • System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
  • System.Net.Http.HttpRequestException: Error while copying content to a stream.

Stage 1

Probe the server and note the phase

Protocol and cipher suite the server picks

openssl s_client -connect api.example.com:443 -brief </dev/null
  • The exception appears while connecting, before any HTTP response: handshake case.
  • The outer exception is Error while copying content to a stream and headers arrived: mid-response case.

Stage 2

Handshake: .NET on Linux offers only ECDHE suites

When openssl.cnf sets no cipher list, .NET on Linux falls back to its own short list: TLS 1.3 suites plus ECDHE suites with AES-GCM or AES-CBC. A legacy server that supports only RSA key exchange finds nothing it can pick and closes the socket, which SslStream reports as EOF. One dotnet/runtime report describes exactly this on an ASP.NET 8.0 container image, while the 7.0 image, whose openssl.cnf set a cipher policy, worked. OpenSSL's own tool still connects, because .NET ignores OpenSSL's built-in defaults.

Compare the two key-exchange families against the server.

Does the server accept ECDHE, or only RSA

openssl s_client -connect api.example.com:443 -tls1_2 -cipher kECDHE -brief </dev/null
openssl s_client -connect api.example.com:443 -tls1_2 -cipher kRSA -brief </dev/null

Stage 3

Fixing the handshake case

Ask the server owner to enable ECDHE suites. That is the real fix. Until then, give only this client a CipherSuitesPolicy that adds the RSA suite the server accepts next to the ECDHE ones. CipherSuitesPolicy is not supported on Windows or Android, and a system-wide alternative is a CipherString in openssl.cnf (crypto-policies on RHEL and Fedora).

A per-client cipher policy

using System.Net.Security;

var handler = new SocketsHttpHandler
{
    SslOptions =
    {
        CipherSuitesPolicy = new CipherSuitesPolicy(new[]
        {
            TlsCipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
            TlsCipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
            TlsCipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384,
        }),
    },
};
using var client = new HttpClient(handler);

Stage 4

Mid-response: the connection closed early

Here the handshake worked and headers arrived. Then the server, or a load balancer or proxy in between, closed the connection before the whole body was sent. In a dotnet/runtime issue on exactly this stack trace, the maintainers found no fault in how HttpClient parses the response and pointed at the server side.

Retry idempotent requests when the inner exception is an IOException; in that issue, retries stopped the failures. For a lasting fix, capture traffic on the client to see which hop sends the close, then look at that hop's timeouts and limits.

A bounded retry for GET

for (var attempt = 1; ; attempt++)
{
    try
    {
        using var response = await client.GetAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
    catch (HttpRequestException ex) when (ex.InnerException is IOException && attempt < 3)
    {
        await Task.Delay(TimeSpan.FromSeconds(attempt));
    }
}

Stage 5

If even OpenSSL cannot connect

When openssl s_client cannot connect with -tls1_2, the server may speak only TLS 1.0 or 1.1. The .NET fallback list on Linux has no suite for those versions at all, so the lasting fix is TLS 1.2 on the server. Probe every third-party endpoint with -cipher kECDHE from your production image before go-live.

If you only keep one thing

  • The same message means a handshake refusal or an early close; note the phase.
  • On Linux, .NET's default suites are ECDHE only unless openssl.cnf says otherwise.
  • Mid-response closes come from the server or a proxy; retry GETs and capture traffic.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.