🔧 The DisposeAsync That Was Called the Synchronous Way
A type can implement both IDisposable and IAsyncDisposable at once, which is common for anything that wraps a resource with async-friendly cleanup, like flushing a buffered stream over the network. The two interfaces look interchangeable from the call site because a using statement compiles against either one, but they are not interchangeable in what actually runs: a plain using block, or an explicit call to Dispose, invokes only the synchronous Dispose path, and if that type’s Dispose method is a thin fallback that skips the async flush entirely, the object’s real cleanup logic in DisposeAsync never executes at all, with no exception or warning to say so.
🔎 The Problem
public sealed class BufferedUploadSession : IDisposable, IAsyncDisposable
{
public void Dispose()
{
// The synchronous fallback. It releases the handle, but it
// does NOT flush the buffered bytes - that part only
// happens in DisposeAsync below, by design, because
// flushing requires an async network write.
_handle?.Dispose();
}
public async ValueTask DisposeAsync()
{
await _networkStream.FlushAsync();
_handle?.Dispose();
}
}
public void UploadReport(byte[] data)
{
// A plain 'using' block binds to IDisposable, not
// IAsyncDisposable - this silently calls Dispose(), not
// DisposeAsync(), even though the type implements both.
using var session = new BufferedUploadSession();
session.Write(data);
} // the buffered bytes are never flushed; the file is incomplete
✅ Fix: Use await using to Reach the Async Path
- Switch the call site to await using instead of a plain using whenever the object implements IAsyncDisposable, so DisposeAsync is the method that actually runs – the compiler only picks the async path when the code explicitly asks for it with await using, never automatically just because the type offers both.
- When a type implements both interfaces, make the synchronous Dispose perform the same cleanup as DisposeAsync wherever that’s actually possible – blocking on the async work inside Dispose if there’s truly no other option – rather than leaving Dispose as a silent partial fallback that skips the real cleanup entirely.
- Add a debug-only check inside Dispose that asserts or logs loudly whenever it runs on a type that also implements IAsyncDisposable, so a using block that should have been await using fails fast in testing instead of shipping a quietly incomplete cleanup to production.
⚠️ Why This Is Easy to Miss
- The compiler accepts a plain using block on a type that implements IAsyncDisposable without any warning at all, because IDisposable alone is sufficient to satisfy it – nothing short of actually reading both Dispose and DisposeAsync side by side reveals that only one of them does the real work.
- The missing flush often doesn’t fail loudly – it produces a file or a message that’s merely short by whatever was still sitting in the buffer, which can look like a legitimate data issue somewhere upstream rather than a disposal problem in this exact method.
IDisposable and IAsyncDisposable on the same type are two different doors into cleanup – using only ever opens one of them, and nothing forces you to notice which.
