Bug 31041 - Random deadlock in WebConnection on device only
Summary: Random deadlock in WebConnection on device only
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 4.0.0
Hardware: All All
: --- normal
Target Milestone: 4.2.0 (C6)
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-06-11 18:49 UTC by Alexandre Emond
Modified: 2015-08-17 16:57 UTC (History)
7 users (show)

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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 GitHub or Developer Community 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.

Related Links:
Status:
RESOLVED FIXED

Description Alexandre Emond 2015-06-11 18:49:14 UTC
Recently, we did update to the Unified Api. Since then, the application randomly deadlock when deploying our application on devices (can't reproduce the bug on the simulator). It occurs after every 2-3 minutes of usage so it make our application hardly shippable as is...

Background threads in the application randomly deadlock when we are doing concurrent HTTP queries. It's especially easier to reproduce when we lost the Wi-Fi connection (eg.: airplane mode), probably because it's easier to have the right timing of 2 WebConnections closing at the same time. Here is the call stack that we captured when we get stuck in this deadlock situation:

THREAD 5:

System.Threading.Monitor.try_enter_with_atomic_var () in 
System.Threading.Monitor.TryEnter (obj=Value not available, millisecondsTimeout=Value not available, lockTaken=Value not available) in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/corlib/System.Threading/Monitor.cs:223
System.Threading.Monitor.Enter (obj=Value not available, lockTaken=Value not available) in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/corlib/System.Threading/Monitor.cs:197
System.Net.WebConnection.Close (sendNext=Value not available) in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnection.cs:1176
System.Net.WebConnectionGroup.Close () in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnectionGroup.cs:80
System.Net.ServicePoint.CloseConnectionGroup (connectionGroupName=Value not available) in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/ServicePoint.cs:407
System.Net.ServicePointManager.CloseConnectionGroup (connectionGroupName=Value not available) in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/ServicePointManager.cs:377
System.Net.Http.HttpClientHandler.Dispose (disposing=Value not available) in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:225
System.Net.Http.HttpMessageHandler.Dispose () in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpMessageHandler.cs:38
...
...

THREAD 7:

System.Threading.Monitor.try_enter_with_atomic_var () in 
System.Threading.Monitor.TryEnter (obj=Value not available, millisecondsTimeout=Value not available, lockTaken=Value not available) in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/corlib/System.Threading/Monitor.cs:223
System.Threading.Monitor.Enter (obj=Value not available, lockTaken=Value not available) in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/corlib/System.Threading/Monitor.cs:197
System.Net.WebConnectionGroup.ConnectionState.SetIdle () in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnectionGroup.cs:264
System.Net.WebConnection.Close (sendNext=Value not available) in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnection.cs:1203
System.Net.WebConnection.InitConnection (state=Value not available) in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnection.cs:779
System.Net.WebConnection.AnonymousMethod__0 (state=Value not available) in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnection.cs:124

THREAD 8:

System.Threading.Monitor.try_enter_with_atomic_var () in 
System.Threading.Monitor.TryEnter (obj=Value not available, millisecondsTimeout=Value not available, lockTaken=Value not available) in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/corlib/System.Threading/Monitor.cs:223
System.Threading.Monitor.Enter (obj=Value not available, lockTaken=Value not available) in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/corlib/System.Threading/Monitor.cs:197
System.Net.WebConnectionGroup.ConnectionState.SetIdle () in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnectionGroup.cs:264
System.Net.WebConnection.Close (sendNext=Value not available) in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnection.cs:1203
System.Net.WebConnection.InitConnection (state=Value not available) in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnection.cs:779
System.Net.WebConnection.AnonymousMethod__0 (state=Value not available) in //Library/Frameworks/Xamarin.iOS.framework/Versions/8.10.1.64/src/mono/mcs/class/System/System.Net/WebConnection.cs:124


WHAT HAPPEN:

- Thread 5 lock WebConnectionGroup.sPoint in (WebConnectionGroup.Close)
- Thread 7 lock WebConnection#1 in (WebConnection.Close)
- Thread 8 lock WebConnection#2 in (WebConnection.Close)
- Thread 5 WAIT to lock WebConnection#1(or #2) in (WebConnection.Close)
- Thread 7 WAIT to lock (ServicePoint [WebConnectionGroup.sPoint]) in WebConnectionGroup.ConnectionState.SetIdle
- Thread 8 WAIT to lock (ServicePoint [WebConnectionGroup.sPoint]) in WebConnectionGroup.ConnectionState.SetIdle

ACTUAL RESULT :
- DEADLOCK : 
  - Thread 5 wait for 7 or 8
  - Thread 7 wait for 5
  - Thread 8 wait for 5

EXPECTED RESULT :
- No deadlock scenario happens
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-06-17 03:11:34 UTC
@Marek, can you have a look at this?
Comment 2 Jim Borden 2015-07-29 01:28:09 UTC
For what it's worth, I've been running into this problem as well.  It's happened two or three times today from running this unit test:

https://github.com/couchbase/couchbase-lite-net/blob/cafcf44d80febcd2110aa0fea1d77ed3dba2b2fe/src/Couchbase.Lite.Tests.Shared/ReplicationTest.cs#L217
Comment 3 Marek Safar 2015-08-17 16:57:00 UTC
Already fixed in Mono master and Mono 4.2

commit 7ee2810f4a34096e55693b574b6ba4a33e8cbd74