Task<IDisposable> surprise
This combination of Task
and IDisposable
surprised me quite well. And yes, it’s my fault. As usual.
I was writing a simple asynchronous method that returns an object that implements IDisposable
. Really barebones example.
async Task<IDisposable> Foo()
{
return default;
}
And then I used this method like this. Nothing to talk about. Or?
using (Foo())
{
}
Can you see the problem? No? Neither did I. With this call, I’m disposing the task (Task
also implements IDisposable
) that I just started/received, not the disposable itself.
The correct code is like this.
using (await Foo())
{
}
Which I’ve found after I deployed an application to my Raspberry Pi and it failed. I suppose that qualifies me for “testing in production guy” label. 😎
And now, forever, I’ll remember it and I’ll be carefull around this combination. And maybe you’ll too.