Updated, a good point by @SriramSakthivel, it turns out I've already answered a very similar question:
Why does GC collects my object when I have a reference to it?
So I'm marking this one as a community wiki.
However, let's say SetResult(..) is never called, and
someClassInstance stops being referenced and is garbage collected.
Does this create a memory leak? Or does .Net auto-magically know the
calling-context needs to be disposed?
If by the calling-context you mean the compiler-generated state machine object (which represents the state of the async method), then yes, it will indeed be finalized.
Example:
static void Main(string[] args)
{
var task = TestSomethingAsync();
Console.WriteLine("Press enter to GC");
Console.ReadLine();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
GC.WaitForFullGCComplete();
GC.WaitForPendingFinalizers();
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
static async Task TestSomethingAsync()
{
using (var something = new SomeDisposable())
{
await something.WaitForThingAsync();
}
}
class SomeDisposable : IDisposable
{
readonly TaskCompletionSource<string> _tcs = new TaskCompletionSource<string>();
~SomeDisposable()
{
Console.WriteLine("~SomeDisposable");
}
public Task<string> WaitForThingAsync()
{
return _tcs.Task;
}
public void Dispose()
{
Console.WriteLine("SomeDisposable.Dispose");
GC.SuppressFinalize(this);
}
}
Output:
Press enter to GC
~SomeDisposable
Press enter to exit
IMO, this behavior is logical, but it still might be a bit unexpected that something gets finalized despite the fact that the using scope for it has never ended (and hence its SomeDisposable.Dispose has never been called) and that the Task returned by TestSomethingAsync is still alive and referenced in Main.
This could lead to some obscure bugs when coding system-level asynchronous stuff. It's really important to use GCHandle.Alloc(callback) on any OS interop callbacks which are not referenced outside async methods. Doing GC.KeepAlive(callback) alone at the end of the async method is not effective. I wrote about this in details here:
Async/await, custom awaiter and garbage collector
On a side note, there's another kind of C# state machine: a method with return yield. Interestingly, along with IEnumerable or IEnumerator, it also implements IDisposable. Invoking its Dispose will unwind any using and finally statements (even in the case of incomplete enumerable sequence):
static IEnumerator SomethingEnumerable()
{
using (var disposable = new SomeDisposable())
{
try
{
Console.WriteLine("Step 1");
yield return null;
Console.WriteLine("Step 2");
yield return null;
Console.WriteLine("Step 3");
yield return null;
}
finally
{
Console.WriteLine("Finally");
}
}
}
// ...
var something = SomethingEnumerable();
something.MoveNext(); // prints "Step 1"
var disposable = (IDisposable)something;
disposable.Dispose(); // prints "Finally", "SomeDisposable.Dispose"
Unlike this, with async methods there's no direct way of controlling the unwiding of using and finally.