Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

improve error handling on failed Http/2 handshake - #37050

Merged
wfurt merged 16 commits into
dotnet:masterfrom
wfurt:h2_handshake_36665
May 3, 2019
Merged

wfurt merged 16 commits into
dotnet:masterfrom
wfurt:h2_handshake_36665

Conversation

@wfurt

@wfurt wfurt commented Apr 20, 2019

Copy link
Copy Markdown
Member

The issue described in #36665 was misconfiguration. However getting ObjectDisposedException on internal objects is not very helpful.

The RFC says:

The server connection preface consists of a potentially empty SETTINGS frame (Section 6.5) that MUST be the first frame the server sends in the HTTP/2 connection.

This change modifies SetupAsync() to check for server preface and throw error immediately instead of aborting connection in background task. It will also log few bytes from response if tracing is enable so it is easier to diagnose this in field.

with this called would get following exception instead of ObjectDisposedException:

System.Net.Http.HttpRequestException: The server returned an invalid or unrecognized response.
   at System.Net.Http.Http2Connection.SetupAsync()
   at System.Net.Http.HttpConnectionPool.GetHttp2ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   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.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at proxy_test.Program.fetch2(String uri) in /home/furt/proxy-test/Program.cs:line 58

closes #36665

@wfurt
wfurt requested a review from a team April 20, 2019 02:04
@wfurt wfurt self-assigned this Apr 20, 2019
@davidsh davidsh added this to the 3.0 milestone Apr 20, 2019
@davidsh

davidsh commented Apr 22, 2019

Copy link
Copy Markdown
Contributor

There are no test changes/additions in this PR. Is this difficult to have automated tests? How did you test these changes?

@wfurt

wfurt commented Apr 22, 2019

Copy link
Copy Markdown
Member Author

I set up HTTP 1.1 server and I used ssl proxy in front of it advertising HTTP2 via ALPN. So HttpClient would fall to HTTP2 processing while server does not understand it.

After second thought I modified old Loopback server and added test for this.

@wfurt

wfurt commented Apr 23, 2019

Copy link
Copy Markdown
Member Author

/azp run corefx-outerloop-linux

@wfurt

wfurt commented Apr 23, 2019

Copy link
Copy Markdown
Member Author

/azp run corefx-outerloop-osx

@azure-pipelines

Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@wfurt

wfurt commented Apr 23, 2019

Copy link
Copy Markdown
Member Author

/azp run corefx-outerloop-windows

@azure-pipelines

Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

1 similar comment
@azure-pipelines

Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@wfurt

wfurt commented Apr 23, 2019

Copy link
Copy Markdown
Member Author

outerloop failures seems unrelated.

@geoffkizer

Copy link
Copy Markdown

If I understand correctly, this change means we will wait to receive the initial SETTINGS from the server before trying to actually use the connection to send requests. Correct?

Per spec, we are allowed to start sending requests before we receive the initial SETTINGS from the server. Forcing us to wait for the server SETTINGS may introduce a perf issue here.

I'm not sure why we can't keep the SETTINGS handling on the background task, and just handle the exception better?

@wfurt

wfurt commented Apr 24, 2019

Copy link
Copy Markdown
Member Author

We do send frames out before this in SetupAsync() so we are not waiting. Do you have specific recommendation how to get exceptions back? There was recent discussion about sending request in parallel with receiving response for gRPC and it will be useful there as well.

@geoffkizer

Copy link
Copy Markdown

We do send frames out before this in SetupAsync() so we are not waiting.

These are just the connection establishment frames though, right? I'm talking about sending actual requests.

Do you have specific recommendation how to get exceptions back?

I'm not sure what you are asking. What exactly are we trying to fix here? There are lots of ways the connection can fail once it's established, why is this particular case any different?

@wfurt

wfurt commented Apr 25, 2019

Copy link
Copy Markdown
Member Author

I have two goals:

  1. collect better (more detailed?) diagnostic on failure on first frame as that is likely may be protocol mismatch
  2. throw better exaction to caller (like The server returned an invalid or unrecognized response.) instead of ObjectDisposed on internal objects.

Aside from the CreditManager, I have also seen disposed exceptions on SslStream.

@wfurt

wfurt commented Apr 25, 2019

Copy link
Copy Markdown
Member Author

I updated code to avoid delay. Now we will throw:

        System.Net.Http.HttpRequestException : The server returned an invalid or unrecognized response.
        ---- System.Net.Http.Http2Connection+Http2ProtocolException : Http2 Protocol Error, errorCode = ProtocolError
        Stack Trace:
          /home/furt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs(1381,0): at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
          /home/furt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs(525,0): at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
          /home/furt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs(33,0): at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
          /home/furt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs(507,0): at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
          /home/furt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs(135,0): at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
          /home/furt/github/wfurt-corefx/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Http2.cs(1300,0): at System.Net.Http.Functional.Tests.HttpClientHandlerTest_Http2.<>c__DisplayClass47_0.<<Http2_ProtocolMismatch_Throws>b__0>d.MoveNext()
          --- End of stack trace from previous location where exception was thrown ---
          /home/furt/github/wfurt-corefx/src/Common/tests/System/Net/Http/LoopbackServer.cs(66,0): at System.Net.Test.Common.LoopbackServer.CreateServerAsync(Func`2 funcAsync, Options options)
          /home/furt/github/wfurt-corefx/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Http2.cs(1282,0): at System.Net.Http.Functional.Tests.HttpClientHandlerTest_Http2.Http2_ProtocolMismatch_Throws()
          --- End of stack trace from previous location where exception was thrown ---

@wfurt

wfurt commented Apr 29, 2019

Copy link
Copy Markdown
Member Author

I sync up with #37223. However even with that change, _abortException did not bubble up and we were still getting ObjectDisposedException on CreditManager. I made some updates as well as I updated new test to check for type of InnerException as everything now gets wrapped in HttpRequestException.

@stephentoub

Copy link
Copy Markdown
Member

_abortException did not bubble up

Where didn't it bubble up?

@wfurt

wfurt commented Apr 29, 2019

Copy link
Copy Markdown
Member Author

to caller of client.GetStringAsync() e.g. test in this case:

  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, CancellationToken cancellationToken) in /mnt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/CreditManager.cs:line 50
     at System.Net.Http.Http2Connection.SendHeadersAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /mnt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs:line 965
     at System.Threading.Tasks.ValueTask`1.get_Result() in /__w/1/s/src/System.Private.CoreLib/shared/System/Threading/Tasks/ValueTask.cs:line 759
     at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /mnt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs:line 1353
     --- End of inner exception stack trace ---
     at System.Net.Http.Http2Connection.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /mnt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs:line 1393
     at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) in /mnt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:line 525
     at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /mnt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs:line 33
     at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) in /mnt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:line 507
     at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask) in /mnt/github/wfurt-corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:line 135
     at System.Net.Http.Functional.Tests.HttpClientHandlerTest_Http2.<>c__DisplayClass49_0.<<Http2_ProtocolMismatch_Throws>b__0>d.MoveNext() in /mnt/github/wfurt-corefx/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Http2.cs:line 1349
      System.Net.Http.Functional.Tests.SocketsHttpHandlerTest_Http2.Http2_ProtocolMismatch_Throws [FAIL]

This is because the inner exception does not come through Http2Stream.

@stephentoub

Copy link
Copy Markdown
Member

to caller of client.GetStringAsync() e.g. test in this case:

Ah. My PR only changed call sites to throw _abortException when they were checking for the aborted state; this doesn't appear to be doing that.

@wfurt

wfurt commented Apr 29, 2019

Copy link
Copy Markdown
Member Author

The stream it self should be in aborted state but the exception comes directly from Http2Connection.SendHeadersAsync(). Since the _abortException is same for all streams, could we store it just in Http2Connection and use _connection. _abortException in Http2Stream code?
When I saw your PR I would not sure if there is benefit of putting it each stream.

@stephentoub

Copy link
Copy Markdown
Member

Since the _abortException is same for all streams

It's not; different streams could have different abort exceptions due to an RST_STREAM frame specific to that stream.

@wfurt

wfurt commented Apr 30, 2019

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown

Azure Pipelines successfully started running 4 pipeline(s).

@wfurt

wfurt commented Apr 30, 2019

Copy link
Copy Markdown
Member Author

That make sense @stephentoub . I look at it again and proposed change should work as intended as it brings connection-level errors from Abort()

outerloop failures are not related to this PR.

@wfurt

wfurt commented May 3, 2019

Copy link
Copy Markdown
Member Author

/azp run corefx-outerloop-windows

@wfurt

wfurt commented May 3, 2019

Copy link
Copy Markdown
Member Author

/azp run corefx-outerloop-linux

@azure-pipelines

Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@wfurt

wfurt commented May 3, 2019

Copy link
Copy Markdown
Member Author

/azp run corefx-outerloop-osx

@azure-pipelines

Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

1 similar comment
@azure-pipelines

Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@wfurt

wfurt commented May 3, 2019

Copy link
Copy Markdown
Member Author

outerloop failures seems unrelated.

@wfurt
wfurt merged commit c412fa7 into dotnet:master May 3, 2019
@wfurt
wfurt deleted the h2_handshake_36665 branch May 3, 2019 06:31
@ViktorHofer

Copy link
Copy Markdown
Member

@wfurt why did you merge this PR when all legs were failing? because of a build error?

Build FAILED.

HttpClientHandlerTest.Http2.cs(1352,10): error CS1513: } expected [F:\workspace\_work\1\s\src\System.Net.Http\tests\FunctionalTests\System.Net.Http.Functional.Tests.csproj]
    0 Warning(s)
    1 Error(s)

https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Http2.cs#L1351 is missing a bracket:

@ViktorHofer

Copy link
Copy Markdown
Member

I commited directly into master to fix the build break: 3075793

@wfurt

wfurt commented May 3, 2019

Copy link
Copy Markdown
Member Author

sorry @ViktorHofer There was merge conflict I tried to resolve using github UI. and obviously I did not do it quite right.

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* improve error handling on failed Http/2 handshake

* remove extra space

* add test for HTTP2 client talking to HTTP1 server.

* fix broken tests

* improve exception handling

* update exception handling

* ws update

* fix bad merge

* update test and changes from dotnet/corefx#37223

* use _abortException only if the stream is not aborted already

* fix IsAborted check

* updates to syncup with recent changes


Commit migrated from dotnet/corefx@c412fa7
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants