Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

HttpWebRequest caches HttpClient in simple cases #41462

Conversation

alnikola
Copy link

@alnikola alnikola commented Oct 1, 2019

HttpWebRequest caches and tries to reuse a single static HttpClient instance when it's safe to share the same instance among concurrent requests with the given parameters.
Fixes #15460

@davidsh davidsh added this to the 5.0 milestone Oct 1, 2019
@davidsh davidsh requested review from stephentoub and a team October 1, 2019 14:46
if (rcvc != null)
{
RemoteCertificateValidationCallback localRcvc = rcvc;
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => localRcvc(request, cert, chain, errors);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not understanding the logic around request being passed in to CreateHttpClient. null is passed in when creating the cached instance, and the instance is passed in otherwise. That means then that for the cached instance, this request will be null. And that means that anyone using the cached HttpClient instance will have a null request object passed into their RemoteCertificateValidationCallback Seems like a bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, there's a subtle change here that's going to result in the closure being created even when the ServerCertificateValidationCallback is null. Previously the only state being closed over were locals, this, or confined to the immediate containing scope. This change now also closes over request, which has method scope. That means the closure for this lambda would previously have only been allocated with rcvc != null, but now it's going to be allocated every time this method is called.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Passing null is intentional because client caching is only allowed when ServiceCertificateValidationCallback == null, thus it will never be caught in the lambda. Additionally, even if somebody accidentally change caching conditions to allow validation callback caching, passing null will still protect from leaking of "this" instance in the static cache.

  2. Valid point. I will fix it.

@scalablecory
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@davidsh
Copy link
Contributor

davidsh commented Oct 4, 2019

Most of the Outerloop failures are not related to the PR.

However, the UWP failures are related to PR:
https://dev.azure.com/dnceng/public/_build/results?buildId=377193&view=ms.vss-test-web.build-test-results-tab&runId=11578792&resultId=166807&paneView=debug

image

The UWP tests need to probably be adjusted accordingly.

@alnikola alnikola force-pushed the alnikola/15460-reuse-http-client-in-web-request branch from fccee22 to 14bec02 Compare October 7, 2019 18:16
@alnikola
Copy link
Author

alnikola commented Oct 7, 2019

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@alnikola alnikola merged commit 95e35e1 into dotnet:master Oct 8, 2019
@alnikola alnikola deleted the alnikola/15460-reuse-http-client-in-web-request branch October 8, 2019 09:24
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
HttpWebRequest caches and tries to reuse a single static HttpClient instance when it's safe to share the same instance among concurrent requests with the given parameters.
Fixes dotnet/corefx#15460


Commit migrated from dotnet/corefx@95e35e1
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants