runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Address feedback on xplat implementation of HttpClient

Open vijaykota opened this issue 11 years ago • 6 comments

vijaykota avatar Jul 29 '15 03:07 vijaykota

  1. From https://github.com/dotnet/corefx/pull/2473

1.1 https://github.com/dotnet/corefx/pull/2473#discussion_r35539699 This should ideally be: state.TrySetCanceled(((OperationCanceledException)ex).Token); though with the cast only done once via an as rather than an is.

1.2 https://github.com/dotnet/corefx/pull/2473#discussion_r35543390 nit: provide constructor and make setters private

var state = new RequestCompletionSource(cancellationToken, request, this);

or just use object initializer

1.3 https://github.com/dotnet/corefx/pull/2473#discussion_r35552624 Same comment here about paying in the right token to TrySetCanceled. Separately, is this really the right behavior? The returned task can now be completed while there's still asynchronous operations in progress. Seems like it should actually try to abort the operation rather than just transitioning the task to canceled. If that's going to be done and is just a todo for later, please as a comment to that affect.

vijaykota avatar Jul 29 '15 03:07 vijaykota

1.4 https://github.com/dotnet/corefx/pull/2473#discussion_r35599095 RequestUri.AbsoluteUri CIPop added a note 3 hours ago
+1 on AbsoluteUri - we need to ensure that the Uri object is actually the expected CURL format. Also see above comment on limiting http and https schemes.

1.5 https://github.com/dotnet/corefx/pull/2473#discussion_r35713281 +1 for an async/high performance libcurl-multi implementation Task.StartNew proved to be slower in practice compared to the HttpWebRequest implementation for WebSockets. The current implementation should be only a starting point for a server-side HttpClient implementation. [vijaykota] What is the timeline for WinHttpHandler changes in this regard?

1.6 https://github.com/dotnet/corefx/pull/2473#discussion_r35600036 There are three places in this try block that you throw an exception just to transfer flow to the catch block. Call HandleAsyncException directly from this if block and then return. Similarly for ThrowIfCancellationRequested, check IsCancellationRequested instead and call HandleAsycException directly

1.7 https://github.com/dotnet/corefx/pull/2473#discussion_r35600347 set response.Request

1.8 https://github.com/dotnet/corefx/pull/2473#discussion_r35713418 Nit: please be consistent regarding the libcurl/LibCurl casing. [vijaykota] The mixed case is what is being followed by libc, libcrypto etc. so won't change this

1.9 https://github.com/dotnet/corefx/pull/2473#discussion_r35718272 SendAsync should not be callable multiple times for the same request. You may be missing a request message check

1.10 https://github.com/dotnet/corefx/pull/2473#discussion_r35718465 According to the documentation calling easy_init without calling global_init is "strongly" discouraged. http://curl.haxx.se/libcurl/c/curl_easy_init.html [vijaykota] Sorry.. I had meant to add a TODO in the code but forgot this

1.11 https://github.com/dotnet/corefx/pull/2473#discussion_r35718607 According to http://curl.haxx.se/libcurl/c/CURLOPT_URL.html there are a lot of protocols supported by libcurl. You should enforce either UriSchemeHttp or UriSchemeHttps and reject anything else for HttpClient.

1.12. Comment on GetCurlException in https://github.com/dotnet/corefx/pull/2473 [stephentoub] Is this the right exception type to throw? There's no networking or web-specific exception type that should be used instead? [davidsh] There will be a better architecture for this later on. We will be revising the System.Net.Http contract and specifically the HttpRequestException type to include a property that is a enum with more granular and platform agnostic errors. It will be similar to WebExceptionStatus enum (from .NET Desktop).

vijaykota avatar Jul 29 '15 03:07 vijaykota

cc: @CIPop @SidharthNabar @pgavlin

davidsh avatar Jul 29 '15 05:07 davidsh

2. Pending comments as of Aug 10, 2015

2.1 See 1.9 2.2 See 1.12

2.3 https://github.com/dotnet/corefx/pull/2597#discussion-diff-36043352 [stephentoub] Do we want this to throw an exception like this if the status code can't be successfully parsed?

2.4 https://github.com/dotnet/corefx/pull/2597#discussion-diff-36573023 [stephentoub] Could you add a TODO and open an issue to track investigating ways of getting rid of such blocking? We don't have to hold up the PR for it, but it still really makes me nervous

2.5 https://github.com/dotnet/corefx/pull/2597#discussion_r36573904 [stephentoub] That said, this pattern makes me question whether a SafeHandle is really appropriate here. If for the majority of the lifetime of the handle we've marked it as DangerousAddRef, that's a very long window where something could go wrong and we drop the safe handle such that the handle will never get closed and never be finalized. At that point, the safe handle isn't buying us much over just storing an IntPtr. It's fine for now, and maybe there are other benefits I'm not seeing clearly at the moment, but something to think about.

2.6 https://github.com/dotnet/corefx/pull/2597#discussion-diff-36574022 [stephentoub] I'm probably being overly cautious here, but consider asserting here that _innerStream is in fact an UnmanagedMemoryStream, with a comment highlighting how disastrous it would be if in the future someone changed _innerStream to be something else where Read could block.

2.7 https://github.com/dotnet/corefx/pull/2597#discussion-diff-36574245 [stephentoub] I understand your argument for why the writer needs to block until the data is consumed, because of how libcurl works with regards to handing out a pointer to the data that's only good for the duration of the synchronous call. But what about the reader... could we override ReadAsync, and implement it to use asynchronous waiting when data isn't available yet? e.g. awaiting on a TaskCompletionSource instead of blocking on a ManualResetEventSlim?

2.8 https://github.com/dotnet/corefx/pull/2597#discussion-diff-36592315 [stephentoub] What happens at this point if TrySetResult returns false because the task is already completed, and it was completed as Canceled or Faulted, such that there will never be a reader. Is the subsequent code that proceeds to call WaitAndSignalReaders still ok?

2.9 https://github.com/dotnet/corefx/pull/2597#discussion-diff-36592359 [stephentoub] What if responseHeader.Length == prefixLength such that prefixLength + 1 (or +2 as used below) is out of bounds?

2.10 https://github.com/dotnet/corefx/pull/2597#discussion-diff-36592527 [stephentoub] I was curious about this, so I looked at the libcurl docs, which includes in its discussion about the timeout argument: A timeout_ms value of -1 means you should delete your timer. so this doesn't appear to be doing the right thing...?

2.11 https://github.com/dotnet/corefx/pull/2597#discussion-diff-36592581 [stephentoub] Do you need to dispose of requestContentStream here?

2.12 https://github.com/dotnet/corefx/pull/2597#discussion-diff-36592592 [stephentoub] I'm not clear on why this cancellation check is necessary either. The one below makes sense, because we will have just performed a potentially long operation and we want to poll cancellation again. But this one, we just checked IsCancellationRequested prior to calling BeginRequest.

vijaykota avatar Aug 10 '15 12:08 vijaykota

Pending comments as of Aug 19, 2015 3.1 See 1.9 3.2 See 1.12 3.3 See 2.3 3.4 See 2.4 3.5 See 2.5 3.6 See 2.7

3.7 https://github.com/vijaykota/corefx/commit/2d6ef7d2b3f10600168231d98b750dbc5770c714#commitcomment-12760473 [stephentoub] CurlSocketCallback calls CheckForCompletedTransfers, and CheckForCompletedTransfers calls EndRequest. EndRequest calls TrySet* on the Task that's returned to the caller, and the caller could have hooked up synchronous continuations such that those continuations (arbitrary code) runs during the call to TrySet*, which means arbitrary code can be running under the lock.

3.8 https://github.com/dotnet/corefx/pull/2720#discussion_r37188185 [stephentoub] Could we end up in some kind of infinite loop here where multiHandle.FdCount > 0 but we get an error every time we call PollFds, so we end up in a tight spin loop?

3.9 https://github.com/dotnet/corefx/pull/2944#issuecomment-134712233 [stephentoub] Another thing to investigate/fix (may or may not be related): doesn't look like we're following all of the required steps for using libcurl in a multithreaded environment: http://curl.haxx.se/libcurl/c/threadsafe.html

3.10 https://github.com/dotnet/corefx/pull/2944#discussion-diff-37865246 The freeing of _multiHandlePtr is commented out and the leak needs to be fixed

vijaykota avatar Aug 19 '15 05:08 vijaykota

Created the following issues/tracking items

  • dotnet/corefx#3268 to track 3.1 which is 1.9 dotnet/corefx#2473 (comment)

SendAsync should not be callable multiple times for the same request. You may be missing a request message check

  • dotnet/runtime#14857 is tracking item 3.2 , which is 1.12 Comment on GetCurlException in dotnet/corefx#2473

    [stephentoub] Is this the right exception type to throw? There's no networking or web-specific exception type that should be used instead?

[davidsh] There will be a better architecture for this later on. We will be revising the System.Net.Http contract and specifically the HttpRequestException type to include a property that is a enum with more granular and platform agnostic errors. It will be similar to WebExceptionStatus enum (from .NET Desktop).

  • A new issue dotnet/corefx#3269 is now tracking the negative scenarios of status code in response messages. That is, 3.3 , which is 2.3 dotnet/corefx#2597 (diff)

    [stephentoub] Do we want this to throw an exception like this if the status code can't be successfully parsed?

  • A new issue dotnet/corefx#3270 is created to track 3.9 dotnet/corefx#2944 (comment)

    [stephentoub] Another thing to investigate/fix (may or may not be related): doesn't look like we're following all of the required steps for using libcurl in a multithreaded environment: http://curl.haxx.se/libcurl/c/threadsafe.html

The rest of the items are now closed via dotnet/corefx#3050 and dotnet/corefx#3068.

Hence closing this CR.

kapilash avatar Sep 16 '15 11:09 kapilash