Bug 19564

Summary: Lazy<T>.Value throws InvalidOperationException instead of original exception when called by second thread
Product: [Mono] Class Libraries Reporter: Matt Ward <matt.ward>
Component: mscorlibAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, mohitk, mono-bugs+mono
Priority: ---    
Version: 3.4.0   
Target Milestone: Untriaged   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Unit test for Lazy<T>.Value

Description Matt Ward 2014-05-06 12:46:40 UTC
Created attachment 6744 [details]
Unit test for Lazy<T>.Value

With a valueFactory method that throws an exception, when two threads try to access Lazy<T>.Value at the same time an InvalidOperationException is returned for the second thread and not the original exception.

Tested with: MonoFramework-MDK-3.4.0.204.macos10.xamarin.x86.pkg


Code which reproduces the problem (unit test is attached):


var goEvent = new AutoResetEvent (false);

var lazy = new Lazy<string> (() => {
	goEvent.Set ();
	Thread.Sleep (2000);
	throw new Exception ("Lazy exception");
});

Exception firstLazyEx = null;
var thread = new Thread (() => {
	try {
		string value = lazy.Value;
	} catch (Exception ex) {
		firstLazyEx = ex;
	}
});
thread.Start ();

goEvent.WaitOne ();
Exception secondLazyEx = null;
try {
	string value = lazy.Value;
} catch (Exception ex) {
	secondLazyEx = ex;
}

Assert.AreEqual ("Lazy exception", secondLazyEx.Message);


The above test succeeds on Windows using Microsoft's .NET Framework 4, but on Mono 3.4.0 we get an InvalidOperationException.

System.InvalidOperationException: The initialization function tries to access Value on this instance   at System.Lazy`1[System.String].InitValue () [0x0012d] in /Volumes/build-root-ramdisk/mono-3.4.0/mcs/class/corlib/System/Lazy.cs:151    at System.Lazy`1[System.String].get_Value () [0x00024] in /Volumes/build-root-ramdisk/mono-3.4.0/mcs/class/corlib/System/Lazy.cs:100    at LazyTests.ThreadedLazyTests.LazyValueCalledBySecondThreadWhilstFirstThreadInLock ()


Looking at the corlib/System/Lazy.cs code a possible fix would be to insert the following code:

lock (monitor) {
    if (inited)
        return value;

    /// New code:
    //
    if (exception != null)
        throw exception;
    //
    /// End new code. 
   
    if (factory == null)
        throw exception = new InvalidOperationException ("The initialisation..");

    ...
}
Comment 1 Marek Safar 2014-05-07 07:13:07 UTC
Fixed in master
Comment 2 Mohit Kheterpal 2014-06-03 13:57:13 UTC
I have checked this issue and getting same error when I run the attached project as shown in screencast : http://screencast.com/t/RaaI9BDo

Exception Details :
"at System.Lazy`1[System.String].InitValue () [0x0012d] in /Volumes/build-root-ramdisk/mono-3.4.0/mcs/class/corlib/System/Lazy.cs:151 
  at System.Lazy`1[System.String].get_Value () [0x00024] in /Volumes/build-root-ramdisk/mono-3.4.0/mcs/class/corlib/System/Lazy.cs:100 
  at LazyTests.ThreadedLazyTests.LazyValueCalledBySecondThreadWhilstFirstThreadInLock () [0x0005e] in /Users/360logicaxamarinmacmini/Projects/dsfdsfe34434444334/dsfdsfe34434444334/ThreadedLazyTests.cs:34 "


Hence Reopening this issue.

Environment info :
=== Xamarin Studio ===

Version 5.1 (build 327)
Installation UUID: 449f40dd-b3f1-4028-9a6b-cca0d1a2307d
Runtime:
	Mono 3.4.0 ((no/c3fc3ba)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000204

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.iOS ===

Version: 7.2.99.1 (Enterprise Edition)
Hash: a82e30c
Branch: 
Build date: 2014-06-02 11:31:47-0400

=== Xamarin.Android ===

Version: 4.14.0 (Enterprise Edition)
Android SDK: /Users/360logicaxamarinmacmini/Desktop/android-sdk-macosx_Róbert_à
	Supported Android versions:
		1.6   (API level 4)
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		3.2   (API level 13)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Xamarin.Mac ===

Xamarin.Mac: 1.8.0.7

=== Build Information ===

Release ID: 501000327
Git revision: 9a4bf62f59ec39169e4e9b61c3816a03c8ac961f
Build date: 2014-06-03 06:01:06-04
Xamarin addins: b68a34ef2fc4c46b045dc38e26fb199bfe1b201d

=== Operating System ===

Mac OS X 10.8.4
Darwin 360Logicas-Mac-mini.local 12.4.0 Darwin Kernel Version 12.4.0
    Sun Mar 10 18:01:10 PDT 2013
    root:xnu-2050.24.6~1/RELEASE_X86_64 x86_64