Bug 19015 - Using Parallel.For and a lock+collection or Parallel.For and a Concurrent collection causes Dead locks (with GC pressure test case )
Summary: Using Parallel.For and a lock+collection or Parallel.For and a Concurrent col...
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.12.2
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Marek Habersack
URL:
Depends on:
Blocks:
 
Reported: 2014-04-13 20:25 UTC by T.J. Purtell
Modified: 2016-08-29 16:42 UTC (History)
3 users (show)

Tags: XATriaged
Is this bug a regression?: No
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 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.

Related Links:
Status:
RESOLVED FIXED

Description T.J. Purtell 2014-04-13 20:25:03 UTC
repository with test case: https://bitbucket.org/tpurtell/contendedlockparallelforgcdeadlock

I started seeing deadlocks recently once we started doing multi-core optimizations for parts of our application.  I was observing ANRs with the main thread stuck in unexpected places (e.g. mono android class registration, various n_OnXXX methods of my application). 

Ultimately, I found a piece of code that we had just changed to use a Parallel.For operation and disabled that optimization.  The end result was that the dead locks went away.  I was able to put together a test case that demonstrates the hangs that occur.

I found bug #18363 which seems like it can cause iterations of the loop to be skipped or run multiple times... but I couldn't understand how it could cause a dead lock.  My test case includes checks for #18363 and I do see that issue periodically (maybe 1/10000 iterations).

The test case runs a GC every 100ms.  And at the same time, it keep launching a 20 iteration parallel for loop.  I have two variants which both produce the deadlock (lock+collection like in my original app, and a lock free concurrent collection).

The deadlock occurs for both 4.12.2 stable and 4.12.3 alpha.  They occur with both debug and release, though the time required varies.  I never waited for the counter to go above 60000 to reproduce the hang.  My device is an HTC One ATT - Android 4.3.

I also experimented with the test case using a plain old for loop (which is much faster) and appears not to show dead lock issues within a reasonable reproduction time limit.  I suspect this is most likely a GC bug...
Comment 1 Marek Habersack 2016-08-29 16:42:45 UTC
This issue no longer exists in Xamarin.Android/master and monodroid/master