Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Created attachment 13773 [details]
Example project that reproduces the problem.
[Cross-posted from https://forums.xamarin.com/discussion/55223/shared-httpclient-instance-fails-after-cancellation]
Although the thread safety of HttpClient was recently improved, I've found a scenario where the state of a HttpClient instance can become irrevocably corrupt. After more than a year of suffering from this obscure problem, I think I finally have a simple project in which it is reliably reproducible (attached with code below for easy reference).
The flow is as follows:
1. Start 10 HTTP requests for the same resource on individual background threads
2. Start 10 more HTTP requests for the same resource on individual background threads
3. Cancel the first 10 requests
4. Wait for all the threads to finish
The expectation is that the first set of requests terminate quickly with a TaskCancelledException, then the second set run to completion. The request logic only actually waits for the header to be returned and doesn't bother downloading the content, so under favourable network conditions we would expect this to all complete within 10 seconds or so.
However, in the sample project the second set of requests never return and in fact the HttpClient timeout of 30 seconds eventually kicks in to kill the tasks off.
There are four adjustments to the sample project that stop this problem happening (the first three have NOTE comments in the code):
1. Increasing the DefaultConnectionLimit to 100
2. Removing the task cancellation step
3. Using an instance of HttpClient per request rather than using a single shared instance
4. Targeting a different domain for the second set of requests
Change any one of these things and the code will run as expected.
Based on this evidence, and tinkering with the target URL, I think the likely culprit has something to do with DNS resolution. Perhaps cancelling a task when a DNS query is being processed somehow corrupts the state of the HttpClient object? It is a natural area for lock protection, but perhaps the cancellation logic forgets the unlock?
The sample code is adapted from a @MihaMarkic bug report about a related (or possibly identical) problem: https://bugzilla.xamarin.com/show_bug.cgi?id=20974
This issue is fixed in the current Xamarin.Android stable release (6.0.0-34) and it appears to be a different issue than the one reported in bug #20974
I can confirm that the example now runs as expected in Xamarin.Android 22.214.171.124. Thank you!