Bug 51189 - Easy way to deadlock Mono with concurrent HttpWebRequests
Summary: Easy way to deadlock Mono with concurrent HttpWebRequests
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Net.Http (show other bugs)
Version: 4.8.0 (C9)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-01-03 12:37 UTC by rubarax
Modified: 2017-01-03 12:37 UTC (History)
1 user (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Application code (2.45 KB, text/plain)
2017-01-03 12:37 UTC, rubarax
Details

Description rubarax 2017-01-03 12:37:19 UTC
Created attachment 19066 [details]
Application code

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 4.6.2.7 / 4.8.0.395 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.

Note You need to log in before you can comment on or make changes to this bug.