Bug 10341 - NullReferenceException in MoveNext in async method
Summary: NullReferenceException in MoveNext in async method
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-02-15 23:18 UTC by Mathieu Fenniak
Modified: 2013-02-20 05:16 UTC (History)
1 user (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 Mathieu Fenniak 2013-02-15 23:18:21 UTC
Hello,

I've encountered a problem where an async method pattern is causing a NullReferenceException in the implementation of the IAsyncStateMachine.  This problem occurs on Mono 3.0.1.0.  I have run the same code in MS .NET 4.5 without the same issue occurring.  Below is the minimal test case that I was able to reproduce this issue with.


	class MainClass
	{
		public static void Main (string[] args)
		{
			var task = Connect ("invalid.hostname");
			task.Wait ();
		}

		private static async Task Connect(params string[] hostname)
		{
			foreach (var h in hostname)
			{
				try
				{
					await Dns.GetHostAddressesAsync(h);
				}
				catch (Exception)
				{
				}
			}
		}
	}

And the resulting error:

	Unhandled Exception:
	System.AggregateException:  ---> System.NullReferenceException: Object reference not set to an instance of an object
	  at NRETest.MainClass+<Connect>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
	  --- End of inner exception stack trace ---
	  at System.Threading.Tasks.Task.Wait (Int32 millisecondsTimeout, CancellationToken cancellationToken) [0x00000] in <filename unknown>:0 
	  at System.Threading.Tasks.Task.Wait () [0x00000] in <filename unknown>:0 
	  at NRETest.MainClass.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 NRETest.MainClass+<Connect>c__async0.MoveNext () [0x00000] in <filename unknown>:0 

I attempted to cut down the test case further by removing the Dns.GetHostAddressesAsync call, but I was unsuccessful in reproducing the error by substituting in a different task that faults.  The error also disappears if you remove the foreach loop.
Comment 1 Marek Safar 2013-02-20 05:16:54 UTC
Fixed in master