I'm using similar code to below for a non critical fire-and-forget operation in ASP.NET
private void SomeMethod()
{
FireAndForgetAsync();
}
private async Task FireAndForgetAsync()
{
// Simulate medium running IO blocking operation
await Task.Delay(sleepTime).ConfigureAwait(false);
// Simulating the rare scenario where an error occurs
throw new Exception("An Error Occurred");
}
The exception is captured in the task being returned from the async method, which prevents an unhandled exception altogether crashing the application..
But would this code cause any memory leaks? Because the returned Task (and its exception) are not assigned / accessed? My thoughts are that it would simply be garbage collected unless some .NET framework code is holding a reference to the task..