diff --git a/src/Orleans.Runtime/Catalog/ActivationDirectory.cs b/src/Orleans.Runtime/Catalog/ActivationDirectory.cs index f87cdec030..7e43f51453 100644 --- a/src/Orleans.Runtime/Catalog/ActivationDirectory.cs +++ b/src/Orleans.Runtime/Catalog/ActivationDirectory.cs @@ -1,11 +1,13 @@ +using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; +using System.Threading.Tasks; namespace Orleans.Runtime; -internal sealed class ActivationDirectory : IEnumerable> +internal sealed class ActivationDirectory : IEnumerable>, IAsyncDisposable, IDisposable { private int _activationsCount; @@ -43,4 +45,47 @@ public void RemoveTarget(IGrainContext target) public IEnumerator> GetEnumerator() => _activations.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + async ValueTask IAsyncDisposable.DisposeAsync() + { + var tasks = new List(); + foreach (var (_, value) in _activations) + { + try + { + if (value is IAsyncDisposable asyncDisposable) + { + tasks.Add(asyncDisposable.DisposeAsync().AsTask()); + } + else if (value is IDisposable disposable) + { + disposable.Dispose(); + } + } + catch + { + // Ignore exceptions during disposal. + } + } + + await Task.WhenAll(tasks).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing); + } + + void IDisposable.Dispose() + { + foreach (var (_, value) in _activations) + { + try + { + if (value is IDisposable disposable) + { + disposable.Dispose(); + } + } + catch + { + // Ignore exceptions during disposal. + } + } + } }