Created attachment 19066 [details]
We can deadlock Mono launching a few HttpWebRequests. We can consistently reproduce in our app, but only sometimes happens in the attached program.
We initially detected this on a server pulling data from Azure. It deadlocks and it is unable to handle a single new request (since we rely on Socket.BeginReceive for that, and the callback is never invoked once the deadlock happens).
We suspect it could still be related to this ancient issue: http://www.mono-project.com/archived/articlethreadpool_deadlocks/
I have attached a small sample application to reproduce the issue. It occasionally deadlocks on Mono 220.127.116.11 / 18.104.22.1685 no matter whether the machine has 1 core or 8. What is consistent is that it performs worse than expected and fails. Not sure if this is a hint.
What the app does is:
* It launches 40 HttpWebRequest.GetResponse() in parallel.
* Most of them fail ("The authentication or decryption has failed.", "The request timed out") .
* The program retries if they fail - and retries also fail.
* Too high cpu usage (30% on my test box).
* We also tried the HttpClient class using async calls and no threadpool but result wasn't any better.
Some interesting notes:
* The same code works well in .NET/Windows.
* And launching the 40 concurrent requests is super fast there compared to super slow using Mono.
* It works better with mono-4.0.4 (it ends faster and without errors)
* Running mono with the '--server' flag doesn't make any difference.