Bug 4151 - NullReferenceException on async
Summary: NullReferenceException on async
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-03-29 10:15 UTC by soywiz
Modified: 2013-04-12 15:56 UTC (History)
4 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 soywiz 2012-03-29 10:15:58 UTC
Note: I'm not sure if this is a problem with the runtime or with the library implementation.

I have created a small example that works fine on .NET 4.5 and throws a NullReferenceException on mono 2.11 trunk.


C:\...>ConsoleApplication1.exe
[0][1][2]

mono --version
Mono JIT compiler version 2.11 (master/4751bee Thu Mar 29 13:55:24 CEST 2012)

[root@FEDORADEV Debug]# --debug ./ConsoleApplication1.exe
[0][1]
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].SetException (System.Exception exception) [0x00000] in <filename unknown>:0
  at ConsoleApplication1.Program+<RunPropagatingExceptionsAsync>d__3`1[System.String].MoveNext () [0x00000] in <filename unknown>:0
  at (wrapper unbox) ConsoleApplication1.Program/<RunPropagatingExceptionsAsync>d__3`1<string>:MoveNext ()
  at System.Threading.Tasks.ActionContinuation.Execute () [0x00000] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/TaskContinuation.cs:118
  at System.Threading.Tasks.Task.ProcessCompleteDelegates () [0x00015] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:517
  at System.Threading.Tasks.Task.Finish () [0x0005d] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:497
  at System.Threading.Tasks.Task.ThreadStart () [0x000d6] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:400
  at System.Threading.Tasks.Task.Execute () [0x00000] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:445
  at System.Threading.Tasks.TpScheduler.TaskExecuterCallback (System.Object obj) [0x00000] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/TpScheduler.cs:42

Unhandled Exception: System.AggregateException:  ---> System.NullReferenceException: Object reference not set to an instance of an object
  at ConsoleApplication1.Program+<TestNullErrorAsync>d__9.MoveNext () [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Threading.Tasks.Task.Wait (Int32 millisecondsTimeout, CancellationToken cancellationToken) [0x000c9] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:618
  at System.Threading.Tasks.Task.Wait () [0x00000] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:564
  at ConsoleApplication1.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
 --> (Inner exception 0) System.NullReferenceException: Object reference not set to an instance of an object
  at ConsoleApplication1.Program+<TestNullErrorAsync>d__9.MoveNext () [0x00000] in <filename unknown>:0



class Program
{
	async static public Task<TReturn> RunPropagatingExceptionsAsync<TReturn>(Func<TReturn> Action)
	{
		Exception YieldedException = null;

		var Result = await Task.Run(() =>
		{
			try
			{
				return Action();
			}
			catch (Exception Exception)
			{
				YieldedException = Exception;
				return default(TReturn);
			}
		});

		if (YieldedException != null) throw YieldedException;

		return Result;
	}

	// You can change the "string" with any reference type.
	static async public Task<string> TestNullErrorAsync()
	{
		return await RunPropagatingExceptionsAsync(() =>
		{
			return "";
		});
	}

	// With non-reference types works just fine (including structs)
	static async public Task<int> TestOkAsync()
	{
		return await RunPropagatingExceptionsAsync(() =>
		{
			return 1;
		});
	}

	static void Main(string[] args)
	{
		Console.Write("[0]");
		TestOkAsync().Wait();
		Console.Write("[1]");
		TestNullErrorAsync().Wait();
		Console.Write("[2]");
	}
}
Comment 1 Zoltan Varga 2012-03-30 03:07:34 UTC
I can't reproduce this. Could you try running it with mono -O=-gshared ?
Comment 2 soywiz 2012-03-30 10:35:58 UTC
I have executed it again, and it doesn't happen anymore (even without the -O=-gshared). It just "solved" after restarting the computer.

But, I am using the "RunPropagatingExceptionsAsync" method in a more complex code, and it still triggers that problem (without the flags).

Using the -O=-gshared flags fixes the problem in the more complex code.
Comment 3 Zoltan Varga 2012-03-30 10:59:28 UTC
Could you attach that testcase which still fails ?
Comment 4 soywiz 2012-03-30 11:02:08 UTC
That's a complete (and private) application with lots of code.
I though I simplified it eough with that example, but it seems that doesn't trigger always.
I will try to get another example reproducing the problem when I get some time.

Thanks!
Comment 5 soywiz 2012-04-07 11:42:44 UTC
Here you can trigger the error (I expect):

https://github.com/downloads/soywiz/NodeNet/demo1-bugs-mono.7z

Running on windows creates a HTTP server serving an static folder using my library Node.Net: https://github.com/soywiz/NodeNet
On mono:

mono ./Sample.exe

triggers that error.

And even when that error doesn't triggers with the -O=-gshared parameter. The server stops handling connections? That's a bit strange. But I will create a report later.

I'm using Fedora 15. If you need it, I can create a VirtualBox image triggering the problem.

Regards.
Comment 6 soywiz 2012-06-19 13:21:06 UTC
I have reduced a bit more the problem. Also I have determined that compiling with "gmcs -sdk:4.5 sample.cs" doesn't trigger the problem. The problem is triggered with an executable created with Visual Studio 11 Beta. So I will provide it in binary form (https://github.com/downloads/soywiz/NodeNet/MonoBug-SampleApplication.7z)
If I change the Run2 call for Task.Run directly, it works. With -O=-gshared works too.
To run:
mono ./SampleApplicationVs11.exe FAILS (compiled with vs11)
mono ./SampleApplicationMono.exe WORKS (compiled with gmcs)
mono -O=-gshared ./SampleApplicationVs11.exe WORKS

class Program
{
	async static public Task<TReturn> Run2<TReturn>(Func<TReturn> Action)
	{
		return await Task.Run(() =>
		{
			return Action();
		});
	}

	static async public Task<string> TestNullErrorAsync()
	{
		return await Run2(() =>
		{
			return "";
		});
	}

	static async public Task<int> TestOkAsync()
	{
		return await Run2(() =>
		{
			return 1;
		});
	}

	static void Main(string[] args)
	{
		Console.Write("[0]");
		TestOkAsync().Wait();
		Console.Write("[1]");
		TestNullErrorAsync().Wait();
		Console.Write("[2]");
	}
}

Stack trace:

mono --debug ./TestProgram.exe
[0][1]Stacktrace:

  at <unknown> <0xffffffff>
  at System.Threading.Tasks.Task`1.GetAwaiter () <0x00018>
  at TestProgram.Program/<TestNullErrorAsync>d__8.MoveNext () <IL 0x00033, 0x000b2>
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<string>.Start<TestProgram.Program/<TestNullErrorAsync>d__8> (TestProgram.Program/<TestNullErrorAsync>d__8&) [0x0001b] in /home/src/mono/mono/mcs/class/corlib/System.Runtime.CompilerServices/AsyncTaskMethodBuilder_T.cs:112
  at TestProgram.Program.TestNullErrorAsync () <IL 0x00020, 0x0009a>
  at TestProgram.Program.Main (string[]) <IL 0x00022, 0x0004f>
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Native stacktrace:

        mono() [0x497b91]
        mono() [0x4eae3f]
        mono() [0x41c287]
        /lib64/libpthread.so.0() [0x33d9e0eee0]
        /lib64/libpthread.so.0(pthread_mutex_lock+0) [0x33d9e099b0]
        mono() [0x4a1cf2]
        [0x41aafb16]

Debug info from gdb:


Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].SetException (System.Exception exception) [0x00000] in <filename unknown>:0
  at TestProgram.Program+<Run2>d__3`1[System.String].MoveNext () [0x00000] in <filename unknown>:0
  at (wrapper unbox) TestProgram.Program/<Run2>d__3`1<string>:MoveNext ()
  at System.Threading.Tasks.ActionContinuation.Execute () [0x00000] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/TaskContinuation.cs:124
  at System.Threading.Tasks.Task.ProcessCompleteDelegates () [0x00015] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:525
  at System.Threading.Tasks.Task.HandleGenericException (System.AggregateException e) [0x0001a] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:566
  at System.Threading.Tasks.Task.HandleGenericException (System.Exception e) [0x00000] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:558
  at System.Threading.Tasks.Task.ThreadStart () [0x000ce] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:400
  at System.Threading.Tasks.Task.Execute () [0x00000] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:449
  at System.Threading.Tasks.TpScheduler.TaskExecuterCallback (System.Object obj) [0x00000] in /home/src/mono/mono/mcs/class/corlib/System.Threading.Tasks/TpScheduler.cs:58
Comment 7 soywiz 2012-06-19 13:56:24 UTC
If it helps, I have decompiled the VS generated code and created a .cs that, when compiled (even with gmcs), triggers the error:

https://github.com/downloads/soywiz/NodeNet/mono-bug-decompiled-cs11-source-code.7z
Comment 8 soywiz 2013-04-12 15:56:21 UTC
This issue is already fixed not sure the first version fixing it, but it works on 3.0.10.

I changed the repository name, so you can access samples reproducing the already fixed bug here:
https://github.com/soywiz/NodeNetAsync/downloads
https://github.com/downloads/soywiz/NodeNetAsync/mono-bug-decompiled-cs11-source-code.7z
https://github.com/downloads/soywiz/NodeNetAsync/MonoBug-SampleApplication.7z
https://github.com/downloads/soywiz/NodeNetAsync/demo1-bugs-mono.7z