dotnet / dotnet/command-line-api

Problem running async Actions

Open
#2,152 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

While porting an existing project from the beta4 package in nuget to a more recent build (beta4.23181.5) I saw that async actions were not running to completion. After some debugging I found that if I made the action non-async, it would work as expected. I was able to reproduce it with the following.
```c#
using System;
using System.CommandLine;
using System.Threading.Tasks;

namespace CliAsyncTest;

public class Program
{
static async Task Main(string[] args)
{
var rootCmd = new CliRootCommand("CliAsyncTest - Test InvokeAsync()");
rootCmd.SetAction(async (_) => // Fix: remove async
{
Console.WriteLine("rootCmd Action starting");
await Task.Delay(100); // Fix: remove await, add .Wait()
Console.WriteLine("rootCmd Action complete");
});

return await rootCmd.Parse(args).InvokeAsync();
}
}
```
When I run this I will see the first line output to the console, but not the second. After applying the fixes in the comments I will see both lines output.

Tested with:
- beta4.23181.5
- Windows 10

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.