Skip to content

HTTP2: Unhelpful exceptions with HttpClient and HTTP/2 when not supported by server #29187

Closed
dotnet/corefx
#37050

Description

@JamesNK
  1. Server that only supports HTTP1
  2. Enable HTTP/2: AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
  3. Create HttpRequestMessage with Version = 2.0
  4. Call server

HttpClient.SendAsync throws this exception the first call:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SslStream'.
   at System.Net.Security.SslStream.ThrowIfExceptional()
   at System.Net.Security.SslStream.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)
   at System.Net.Security.SslStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)
   at System.Net.Http.Http2Connection.FlushOutgoingBytesAsync()
   at System.Net.Http.Http2Connection.SendHeadersAsync(HttpRequestMessage request)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at BenchmarkClient.Program.<>c__DisplayClass5_1.<<Main>b__1>d.MoveNext() in C:\Development\Source\grpc-dotnet\perf\benchmarkapps\BenchmarkClient\Program.cs:line 179

Repeating the call with the HttpClient throws a different exception:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'CreditManager'.
   at System.Net.Http.CreditManager.RequestCreditAsync(Int32 amount)
   at System.Net.Http.Http2Connection.SendHeadersAsync(HttpRequestMessage request)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at BenchmarkClient.Program.<>c__DisplayClass5_1.<<Main>b__1>d.MoveNext() in C:\Development\Source\grpc-dotnet\perf\benchmarkapps\BenchmarkClient\Program.cs:line 179

I would expect a friendly error message that the server does not support HTTP/2.

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100-preview4-011108
 Commit:    7cc4cca27b

Activity

  1. changed the title [-]Odd exceptions with HttpClient and HTTP/2[/-] [+]Unhelpful exceptions with HttpClient and HTTP/2[/+] on Apr 6, 2019
  2. stephentoub commented on Apr 8, 2019

    @stephentoub
    Member

    I would expect a friendly error message

    The server supports HTTP/1.1 with SSL? If so, I wouldn't expect an error here at all, rather simply downgrading to 1.1.

    Do you have a repro you can share? What server?

  3. JamesNK commented on Apr 8, 2019

    @JamesNK
    MemberAuthor

    Server is Kestrel.

    https://github.com/JamesNK/grpc-dotnet/tree/jamesnk/httpclient-error-repo

    1. perf\benchmarkapps\BenchmarkServer - dotnet run -c Release
    2. perf\benchmarkapps\BenchmarkClient - dotnet run -c Release
  4. wfurt commented on Apr 8, 2019

    @wfurt
    Member

    Do you have packet capture? I would be interested to se ALPN negotiation. As @stephentoub mentioned, we should downgrade to HTTP/1.1 if H/2 is not negotiated.

  5. JamesNK commented on Apr 8, 2019

    @JamesNK
    MemberAuthor

    Wireshark - http2-http1-error.zip

    If you want the packets unencrypted then you'll need to run the app yourself. I couldn't get Wireshark to do it.

  6. wfurt commented on Apr 8, 2019

    @wfurt
    Member

    The server negotiated H/2 from the list so it is expected that it can talk HTTP/2. That does not mean the current exception is useful. It is also little bit strange that the exception is not consistent.

    image

  7. self-assigned this
    on Apr 20, 2019
  8. changed the title [-]Unhelpful exceptions with HttpClient and HTTP/2[/-] [+]HTTP2: Unhelpful exceptions with HttpClient and HTTP/2 when not supported by server[/+] on Apr 25, 2019
  9. transferred this issue fromdotnet/corefxon Feb 1, 2020
  10. added this to the 3.0 milestone on Feb 1, 2020
  11. ghost locked as resolved and limited conversation to collaborators on Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions