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.

Wrong coverage for "await foreach" when method is generic

See original GitHub issue

I’m using coverlet.msbuild v3.1.0 (and tried also with the current nightly build) and there is an uncovered statement reported. image

I tried to reproduce it but I didn’t managed to get an reproduction. Any ideas what the problem could be and how I can provide an reproduction?

public class AsyncForeachReproduction
{
    private static async IAsyncEnumerable<int> RangeAsync(int start, int count)
    {
        for (int i = 0; i < count; i++)
        {
            await Task.Delay(i);
            yield return start + i;
        }
    }

    public async Task Execute()
    {
        await foreach (var a in RangeAsync(1, 5))
        {
            Console.WriteLine(a);
            await Task.CompletedTask;
        }

        await Task.CompletedTask;
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
daveMuellercommented, Mar 9, 2022

I started working on this issue.

1reaction
MarcoRossignolicommented, Feb 5, 2022

The code in the repro and the code that is failing are a lot different from compiler perspective, state machine are built in a different way because different object structure are involved(different nesting, visibility, different relationship between objects…different optimization)…you should create the same classes, mock those and retry.

Try to compare those with ILSpy and you’ll see that the emitted IL is different.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using async/await with a forEach loop
I'm trying to loop through an array of files and await on the contents of each file. import fs from 'fs-promise' async function...
Read more >
The anatomy of async iterators (aka await, foreach, yield)
Here I'm going to discuss the mechanisms and concepts relating to async iterators in C# - with the hope of both demystifying them...
Read more >
Async return types (C#)
Learn about the return types that async methods can have in C# with code examples for each type.
Read more >
The Simplest Guide to Using Async/Await with forEach() in ...
Using async/await with forEach() does not need to be a nightmare! Here are 4 solutions to your problem.
Read more >
Async streams in C# – Deep Dive
This article explores C# async streams in detail: from the basics and the reasoning behind them, to real-world examples of using and ...
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