Metaprogramming: `NullReferenceException` when exception is thrown in nested `AwaitForEach`
See original GitHub issueWhen 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:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Clone is the best way.
I’ve tested it with the published NuGet package and can confirm it has been fixed. Thanks!