We've been plagued for the last few weeks by CI jobs on Windows that hang in System.Net.Http.Functional.Tests, causing the whole job to fail after it eventually times out after two hours. I managed to find a CI server that had a process still alive after having experienced the hang and was able to debug it. The Dispose_DisposingHandlerCancelsActiveOperationsWithoutResponses test is creating three loopback servers and using HttpClient.GetAsync to access each of them. It's then Disposing of the HttpClient to verify that requests which should have been canceled are canceled. The problem is that there's a race condition. We may end up canceling one of the requests before it even gets a chance to connect to the server. As a result, the server remains stuck in an asynchronous accept on it socket, and at the end of the test, we're waiting for all of the servers to shutdown... deadlock. The fix is simple: don't wait for the servers... we don't care about their results, and they'll all be torn down when we exit the inner delegate.
Commit migrated from https://github.com/dotnet/corefx/commit/99b7b5a9b9f23e8b94bd8c75ecf86bb71d24b6c4
libcurl wants to be able to call back to CurlHandler at arbitrary times to ask it to seek in the request content stream. This can happen for redirects, for auth challenges, etc. Handling this has been a challenge due to the way HttpContent is designed.
HttpContent provides two modes for accessing the data via Streams:
- Push: You call CopyToAsync(Stream, TransportContext) to have the data from the HttpContent copied into the target stream.
- Pull: You call ReadAsStreamAsync to get a stream that represents the data in the HttpContent, and you can read from that.
The pull model is ideal for libcurl. It allows us to get a stream which we can jump around in if it's seekable, and we can request a new stream if it's not seekable. The push model is challenging, as we're forced to put in plcae an intermediary, a push-pull adapter that let's us effectively read from a type that's trying to write to us.
There is however an issue with the pull model. With the push model, StreamContent's CopyToAsync tracks whether the stream has already been accessed, and if it has and if it's non-seekable, trying to call CopyToAsync again throws to alert you that rewinding simply isn't possible. The pull StreamContent's ReadAsStreamAsync, however, does not. This means that if you get a non-seekable stream and you call ReadAsStreamAsync again, you get back the same stream (or, rather, a read-only wrapper around the same stream) that is at the existing position rather than the original position. Then resending that content will end up sending less data than it should have, resulting in failure, just not abruptly.
That issue caused us to go down the path of the more challenging push model and trying to use CopyToAsync. However, recently that's been shown to have even worse effects. We don't have the ability to cancel CopyToAsync, which means that if the first copy is still in-flight when we try to seek, we have no choice but to fail, even if the stream is actually seekable, as most are (e.g. memory streams, file streams, etc.) This can happen deterministically in cases where libcurl decides to redirect before it's fully sent the request content, but it's also prone to race conditions even for cases where the request content is fully sent but the copy hasn't been officially marked as complete by the time libcurl tries to redirect. ASP.NET recently began hitting this frequently on some machines in their lab runs.
While there's no perfect solution, the best solution is to go with the one that's the simplest code, the fastest code, and the deterministic outcome: the pull model. This commit deletes a whole bunch of code that was there in support of the push model, adds just a bit of code to enable the pull model, and fixes up a couple of tests that were written to expect the push model.
Commit migrated from https://github.com/dotnet/corefx/commit/74cd3d619b3d3b1e963ca8bd33749af3aec99c05
Be a bit more lenient in what we allow in the formatting of response headers on Unix. The RFC states that "Each header field consists of a case-insensitive field name followed by a colon (":"), optional leading whitespace, the field value, and optional trailing whitespace." However, whitespace between the header name and the colon is accepted by WinHttpHandler, by the .NET Framework, and by all browsers on which I tried it, and servers can easily include such whitespace; if they do and we're not tolerant of it, requests to such servers will fail.
Commit migrated from https://github.com/dotnet/corefx/commit/888a01cbcb1a98377ebf6e87e89a2551aac1654d
In HttpClient on Unix when a client certificate is used, we add the intermediate certificates for the client authentication cert's chain so that the server can chain-verify. When we add the extra chain certificate to the SSL handle, we're transferring ownership to that handle, which we subequently dispose... but we don't mark the original SafeHandle as invalid, so it eventually gets finalized and we end up double-free'ing, leading to seg faults and other badness. This commit simply cleans up the handle appropriately.
Commit migrated from https://github.com/dotnet/corefx/commit/0b8dfc5774f66b8538e07813113d6c962a8d5775
When using a timeout and/or a cancellation token with a buffered HttpClient operation, the cancellation/timeout doesn't flow through to the actual read/write operations on the underlying streams. This means that on Windows, doing a basic operation like GetStringAsync with a server that pauses for arbitrarily long periods of time while sending the response body won't cancel or timeout until it sends more data. (We don't need to do this on Unix, as on Unix we already handle this case differently, with a single registration for the duration of the SendAsync operation.)
Commit migrated from https://github.com/dotnet/corefx/commit/cc8b48c8ea41b9cee888055483d16072f3f3a1c5
* Fix CurlHandler handling of redirects
- Relative urls in Location headers were not being handled correctly. We now allow any url that TryCreate will parse using the current url as a base.
- We were only handling a subset of redirect status codes as redirects, trying to second-guess which status codes libcurl would redirect for, e.g. 300 wasn't being treated as a redirect. We now let libcurl do all the work.
- We should have been playing it safer with regards to clearing out credentials if we weren't sure a Uri redirect location was valid. Now we do.
- We were tracking state unnecessarily that we should have instead been
asking libcurl for, e.g. the last url used in a redirect. This also caused us to store the wrong RequestMessage.Uri in some cases where a location header came in but we didn't actually follow it.
Commit migrated from https://github.com/dotnet/corefx/commit/7bd18d682f0e201275c988e6263239ab84f6389b
- Verify that request content isn't disposed until after response content is buffered
- Verify that request content isn't disposed until after request content is sent and response headers are received
- Verify that verbs which shouldn't send request content don't
Commit migrated from https://github.com/dotnet/corefx/commit/9c0eaa1b7a4812f50ac37f4adde516971d9b333a
We have several innerloop tests that make HTTP calls during System.Net.Http.Functional.Tests out to www.ssllabs.com, which has SSL endpoints for different SSL protocols. This commit avoids using that server during innerloop tests. The tests are moved to outerloop, and some replacements using a localhost are added to innerloop.
Commit migrated from https://github.com/dotnet/corefx/commit/8a9402b33d46166d1a3129a7c2cae0334b00c4b6