question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Metaprogramming: `NullReferenceException` when exception is thrown in nested `AwaitForEach`

See original GitHub issue

When an exception is thrown in CodeGenerator.AwaitForEach which contains another CodeGenerator.AwaitForEach (nested), you’ll receive an NullReferenceException instead of the thrown exception.

Note: with a single CodeGenerator.AwaitForEach the exception that was being thrown in the body is being thrown, as expected.

Information

.NET 5 DotNext.Metaprogramming 2.12.1

Fiddle

Nested: https://dotnetfiddle.net/yfswYy (invalid, throws NullReferenceException) Single: https://dotnetfiddle.net/DPnQrZ (valid, throws user exception)

Example

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using static DotNext.Metaprogramming.CodeGenerator;
					
public class Program
{
	public static async Task Main()
	{
		var result = AsyncLambda<Func<IAsyncEnumerable<char>, IAsyncEnumerable<char>, Task>>(fun => 
		{
			AwaitForEach(fun[0], ch =>
			{
				WriteLine(ch);
				AwaitForEach(fun[1], ch => WriteLine(ch));
			});
		}).Compile();
		
		await result(GetData('1'), GetData('2'));
	}
	
	public static async IAsyncEnumerable<char> GetData(char data)
	{
		await Task.Yield();
		yield return data;
		throw new Exception("Expected exception");
	}
}

Expected

1
2
Unhandled exception. System.Exception: Expected exception

Actual

1
2
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method2(Closure , AsyncStateMachine`1& )
   at DotNext.Runtime.CompilerServices.AsyncStateMachine`1.System.Runtime.CompilerServices.IAsyncStateMachine.MoveNext() in /_/src/DotNext.Metaprogramming/Runtime/CompilerServices/AsyncStateMachine.cs:line 124
--- End of stack trace from previous location ---
   at Program.Main()
   at Program.<Main>()

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
saknocommented, Jan 7, 2021

Clone is the best way.

0reactions
GerardSmitcommented, Jan 7, 2021

I’ve tested it with the published NuGet package and can confirm it has been fixed. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does async await throw a NullReferenceException?
It doesn't wait for await. This will cause a NullReferenceException to be thrown directly, not placed on the returned task. However, you should ......
Read more >
Introduction To Csharp 8 | PDF
an asynchronous sequence that returns the data in chunks when they become available. A simple definition: Async Streams, allows you to use async...
Read more >
Linknovate | Profile for IBM
java with the following code that enables CORS and sets up a resource server. @Override protected void configure ( HttpSecurity http ) throws...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found