In the previous commit, we only polled the user-provided CancellationToken prior to waiting on the synchronization primitive, but the sync primitive wait was only cancelable by shutting down the stream. This commit updates that logic to support also canceling the asynchronous wait via the user-provided CancellationToken.
Commit migrated from https://github.com/dotnet/corefx/commit/80d2b157525211b655c3f81dde0ace6be9617720
Today, CurlHandler provides a response content stream that doesn't override ReadAsync. As such, any consumers reading data using ReadAsync will actually end up blocking a thread pool thread doing a synchronous Read.
This commit fixes that by implementing ReadAsync to be truly asynchronous. If data is already available, a task is returned immediately. Otherwise, the reader asynchronously waits for the writer to publish data and then consumes that data.
The overall design of the response stream remains the same as it was. However, there are some notable changes:
- A SemaphoreSlim is now used instead of a ManualResetEventSlim for the _writerPublishedDataEvent. This was done because SemaphoreSlim supports both synchronous and asynchronous waiting.
- The _readerRequestingDataEvent was removed, as it was no longer necessary.
- We no longer allocate an UnmanagedMemoryStream per write operation, instead simply storing the IntPtr buffer and long length on the response stream.
- Additional reorganization was done, primarily to share logic between Read and ReadAsync.
Commit migrated from https://github.com/dotnet/corefx/commit/c2af3efe7356efa34887c144940c2c1e6f60271a
This change overhauls how CurlHandler manages its threading and resources.
CurlHandler is now split into three types:
- CurlHandler, which represents the shell of the handler, contains some helper functions, and contains a "MultiAgent". It submits "EasyRequest"s to the "MultiAgent".
- CurlHandler.EasyRequest, which represents all of the state associated with a SendAsync request on the HttpClient.
- CurlHandler.MultiAgent, which represents the owner of a libcurl multi handle and all of the processing associated with it. The MultiAgent is responsible for actually processing EasyRequests, which CurlHandler queues to it.
MultiAgent is designed to not require any cleanup. When the first EasyRequest arrives, it spins up a worker that creates a multi handle to process the request. Any additional requests to arrive while that worker is active will be processed by that worker and multi handle. When no work remains, the multi handle and the worker goes away. If/when another request arrives, a new multi handle / worker are spun up. Etc.
Almost all of the locking previously there was removed, as the worker thread is now solely responsible for doing all work on the multi handle. The only locking now is when the CurlHandler hands off an EasyRequest to the MultiAgent, it needs to lock to store the request into the agent's queue, and the MultiAgent needs to lock to access it.
This design avoids threading issues we faced with regards to accessing state managed by the multi handle without holding the lock (e.g. polling file descriptors) and avoids having multiple threads blocked waiting on the lock to do processing associated with the multi handle. It also makes resource cleanup easy: all state associated with the easy handle is cleaned up when that thread is done processing the easy handle, and all state associated with the multi handle is cleaned up when the worker thread shuts down.
The change also ensures that we always complete the response message, and that any exceptions that occur are propagated as needed to Read on the response stream.
And the design will make it trivial in the future to change the relationship between a CurlHandler and a MultiAgent:
- If we want one MultiAgent shared across all CurlHandlers, we just change the _agent field to be static instead of instance.
- If we want a pool of MultiAgents, we can simply create a collection of them and round-robin between them from one or more CurlHandlers.
- Etc.
All of the configuration code remains, e.g. what options are set on the easy and multi handles, but the code has been refactored. I also include CURLOPT_NOSIGNAL to avoid multithreading problems per the docs.
Along the way, I cleaned up some stale interop that's gotten left behind through various iterations, and I fixed a few small issues related to behavioral differences from Windows, e.g. CookieContainer never returning null, UseCookies defaulting to true, etc.
Commit migrated from https://github.com/dotnet/corefx/commit/b7e7847677236ca0afd0b400bd1fcc138d960902
HttpContent.ReadAsStringAsync() has a bunch of calls to
Encoding.GetPreamble() as part of its encoding detection.
GetPreamble() creates a new byte[] each time it is called.
These byte[] allocations can be avoided. Also, as part of this,
cleaned up and improved the performance of the encoding detection.
Commit migrated from https://github.com/dotnet/corefx/commit/de05c8642746842f61cb063c3d43976f7ee00540
- If a timeout happens, the socket fd is not removed from _fdSet. When
SafeCurlMultiHandle is finalized, it checks if _fdSet is empty and
this assertion was failing. So ensuring that all codepaths of
EndRequest result in cleanup from _fdSet
Commit migrated from https://github.com/dotnet/corefx/commit/bf40c3f442a59616f03871057633344d9230d4b6
- On OSX, the read end of the pipe sometimes wakes up the poll
with a POLLOUT. Since we are interested in only reading on it,
scoping down to POLLIN prevents this from happening. Else we
attempt a read on it which fails
- CheckForCompletedTransfers was being unnecessarily called from the
socket callback. We are only interested in MSG_DONE after the socket
callback indicates removal. This will anyway wake up PollFunction which
calls CheckForCompletedTransfers. This race is also causing a seg fault
sometimes because curl_multi_socket_action has been called on an fd
that was getting cleaned up via curl_easy_cleanup (call sequence is
CurlSocketCallback --> CheckForCompletedTransfers --> EndRequest)
- Also removed some unnecessary CharSet attributes
cc: @stephentoub @davidsh @CIPop @SidharthNabar @pgavlin
Commit migrated from https://github.com/dotnet/corefx/commit/58178ce07fdf0e478467a77698c58fb6d1b4e866
In earlier version, a curl_multi_socket_action was performed everytime
the callback was invoked irrespective of whether there was activity on
the socket. This in turn causes more callbacks when libcurl tries to read
the socket but finds no data. This can be made more efficient by waiting
for activity on the socket and informing libcurl accordingly
- Fixed implementation of timer callback using an actual timer and handling
all possible values of timeout
- Fixed a bug in CheckForCompletedTransfers where a mismatch could occur
between the easy handle specified in the DONE msg
- Fixed the SEGV which was happening in earlier code due to recursive calls
to curl_multi_socket_action in the socket callback which was being invoked
synchronously in curl_multi_socket_action. This was causing a stack overflow
leading to a segfault
Commit migrated from https://github.com/dotnet/corefx/commit/d52dcd394fc174db629acd771d8529714e4dc4f4
This change updates the assembly versions of all of the corefx assemblies
as needed after we shipped stable versions. Assemblies with API differences
get a minor version bump. Assemblies with only bugfixes get a build version
bump.
In order to facilitate this I had to update the reference assemblies, so I took
the opportunity to port them all to the open.
[tfs-changeset: 1514419]
Commit migrated from https://github.com/dotnet/corefx/commit/40723b795917f2e98b64d45c17effb2546dd0cae
This bug revolves around a design limitations of the System.Runtime.WindowsRuntime System.IO.NetFxToWinRtStreamAdapter. Due to the way that the adapter works, the Windows.Web.Http layer is unable to "rewind" data in order to re-send it to the server for POST requests. Resending request entity-body data to the server is frequently needed due to authentication challenges.
The nature of this bug has resulted in a few common scenarios being broken for UWP (Windows 10 Store) apps that use System.Net.HttpClient or System.Net.HttpWebRequest. Even for simple content like StringContent (which is already buffered and not a stream, per se), we are unable to POST the data since the code paths were always using the limited NetFxToWinRtStreamAdapter.
The fix for this bug is multi-layered. In general, we try to avoid using the NetFxToWinRtStreamAdapter for the POST scenarios. For content that is ByteArrayContent (or derived classes), the data is already "buffered". So, we don't need to send it as a stream. We also check if the data has been buffered via the developer calling LoadIntoBufferAsync() explicitly and send it as a buffer. Then, if the data is not buffered, we check the stream itself. If it already supports the WinRT Stream interfaces, then we don't need to use the adapter. So, third-party devs can create compatible, multi-interface (.NET and WinRT) stream content to be POSTed.
[tfs-changeset: 1511754]
Commit migrated from https://github.com/dotnet/corefx/commit/e298b31e1da3dea3c3fd78f5f713b4659136484e