CommunityToolkit / CommunityToolkit/dotnet
Possible bug in [RelayCommand] async task
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### Discussed in https://github.com/CommunityToolkit/dotnet/discussions/355
Originally posted by **DarthMazut** July 26, 2022
Hello,
when I mark my command with `[RelayCommand]` and use `async Task` signature then when synchronouse part of code throws exception this exception is swallow and another criptic exception is thrown instead. Let me give you an excample:
In my view I bind Button's command to my VM command, like so:
```xml
Click Me
```
In my VM I do the following:
```cs
[RelayCommand]
private async Task Command()
{
throw new NotImplementedException();
await Task.Delay(100);
}
```
this simulates any method that has some async call, but before awaiting it, an exception has been thrown by prior part of code.
In such case, after pressing button I receive:
`Unhandled exception at 0x76B757B1 (combase.dll) in TestApp1.exe: 0xC000027B: Wystąpił wewnętrzny wyjątek aplikacji (parameters: 0x16B993B0, 0x00000001).` and eventually this:
`Exception thrown at 0x00DBE538 in TestApp1.exe: 0xC0000005: Access violation executing location 0x00DBE538.`
This happens only when my method signature is `async Task`, when changing it to `async void` the proper exception is thrown.
My question is whether this is expected or it's a bug? Should I use `async void`? I though `async Task` is recommended?
This is obviously problematic because if I throw meaningfull exception from my code I'd like to see those instead of this `Access violation` internal stuff.
Contributor guide
Assessment
This issue has not been assessed yet.