Skip to content

ConceptsSynchronous and asynchronous disposal

Synchronous and asynchronous disposal

How the IDisposable and IAsyncDisposable forms of each type differ, and which method disposes an item that implements both.

Most types come in two forms, one that disposes with Dispose and one that disposes with DisposeAsync. This page explains how the two forms differ and what happens to an item that implements both interfaces.

AsyncDisposable creates the IAsyncDisposable form of every type that Disposable creates, except Disposable.Flag, Disposable.PostingContext and the lock scopes. AsyncDisposable.Context always posts the disposal to the context, and its DisposeAsync completes once the disposal has run. To see which types have an asynchronous form, read the behavior of every disposable.

Synchronous disposal disposes such an item through IDisposable, so a stream held by a synchronous collection is disposed with Dispose:

// 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 IDisposable

Asynchronous disposal prefers DisposeAsync, so the same item is disposed with DisposeAsync when the collection is disposed asynchronously:

var log = new List<string>();
// stream is an open Stream, such as a FileStream.
object[] items = [Disposable.Create(() => log.Add("disposed")), stream];
await items.DisposeAllAsync(); // DisposeAsync for the stream, then Dispose for the rest

Each form of a collection disposes an item through its own interface:

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 interface

An item that implements only IAsyncDisposable is not disposed by DisposeAll(). It disposes the other items, then throws InvalidOperationException, and DisposeAllIgnoringExceptions() does not ignore that. ADR-1001 records the decision.

AsSharedReference() returns a lease that disposes with Dispose, and AsAsyncSharedReference() returns one that disposes with DisposeAsync. A name is never used for both, as ADR-1002 records.

// stream is an open Stream, which implements both IDisposable and IAsyncDisposable.
await using (var lease = stream.AsAsyncSharedReference())
{
lease.Value.WriteByte(42);
}
// stream.DisposeAsync ran when the last lease was released

An async holder cannot dispose in a setter

Section titled “An async holder cannot dispose in a setter”

After disposal, the async aggregate’s setter and the async collection’s Add throw ObjectDisposedException, because they cannot await the resource’s disposal. SetDisposableAsync and AddAsync dispose the resource instead:

var log = new List<string>();
var resources = AsyncDisposable.Collection();
await resources.DisposeAsync();
var lateItem = AsyncDisposable.Create(() => log.Add("late item closed"));
await resources.AddAsync(lateItem); // disposed at once, not kept; Add would throw ObjectDisposedException
// log: "late item closed"; resources.Count == 0

To see what every holder does with a late resource, read disposal status and late resources.