GuidesBridge sync and async disposal
Bridge sync and async disposal
Hand an IDisposable to code that takes an IAsyncDisposable, and dispose whatever you hold with the best method it offers.
Code that disposes asynchronously takes an IAsyncDisposable, and a lot of existing code still holds an IDisposable. This page shows how to adapt one to the other, how to adapt for every call, how to dispose whatever you hold with the best method it offers, how a collection treats an item that implements both, and what to watch for.
Adapt an IDisposable
Section titled “Adapt an IDisposable”AsyncDisposable.Adapt wraps an IDisposable so that DisposeAsync disposes it. The resource is disposed once, however often DisposeAsync is called.
var log = new List<string>();IDisposable legacy = Disposable.Create(() => log.Add("legacy disposed"));
await using (AsyncDisposable.Adapt(legacy)){ // code that takes an IAsyncDisposable}
// log: "legacy disposed"Adapt for every call
Section titled “Adapt for every call”AsyncDisposable.AdaptRepeatable wraps a disposable that you want to run on every call, so each DisposeAsync disposes it again.
var calls = 0;var legacy = Disposable.CreateRepeatable(() => calls++);var adapter = AsyncDisposable.AdaptRepeatable(legacy);
await adapter.DisposeAsync();await adapter.DisposeAsync(); // calls == 2Dispose with the best method available
Section titled “Dispose with the best method available”DisposeAsyncIfAvailable disposes an IDisposable through DisposeAsync when it also implements IAsyncDisposable, and through Dispose when it does not. It does nothing for null.
// stream is an open Stream, such as a FileStream.IDisposable? resource = stream;await resource.DisposeAsyncIfAvailable(); // a Stream is IAsyncDisposable: DisposeAsync runs
resource = null;await resource.DisposeAsyncIfAvailable(); // null: nothing happensItems that implement both
Section titled “Items that implement both”A collection disposes each item through the interface its own disposal uses: synchronous disposal goes through IDisposable, and asynchronous disposal goes through IAsyncDisposable.
List<string> log = [];
// Connection implements both IDisposable and IAsyncDisposable.using (Disposable.Collection(new Connection(log))) { }await using (AsyncDisposable.Collection(new Connection(log))) { }
// log: "Dispose", "DisposeAsync": each collection disposes through its own interfaceSo a synchronous collection of a stream never calls the stream’s DisposeAsync:
// stream is an open Stream, such as a FileStream.using (var resources = Disposable.Collection(stream)){ stream.WriteByte(42);}
// stream.Dispose ran: the collection disposes every item through IDisposableSee synchronous and asynchronous disposal.
Pitfalls
Section titled “Pitfalls”- Adapting
null.- You write
AsyncDisposable.Adapt(maybeNull). - What happens: it throws
ArgumentNullException, as ADR-1008 records. - Write instead:
await maybeNull.DisposeAsyncIfAvailable(), which does nothing fornull.
- You write
- Disposing a stream through a synchronous collection.
- You write
Disposable.Collection(stream). - What happens:
Disposeruns, neverDisposeAsync, so the stream flushes synchronously and blocks the thread on its I/O. - Write instead:
AsyncDisposable.Collection(stream)withawait using.
- You write