Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispose all activations when host is disposed #9001

Merged
merged 1 commit into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/Orleans.Runtime/Catalog/ActivationDirectory.cs
Original file line number Diff line number Diff line change
@@ -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<KeyValuePair<GrainId, IGrainContext>>
internal sealed class ActivationDirectory : IEnumerable<KeyValuePair<GrainId, IGrainContext>>, IAsyncDisposable, IDisposable
{
private int _activationsCount;

Expand Down Expand Up @@ -43,4 +45,47 @@ public void RemoveTarget(IGrainContext target)
public IEnumerator<KeyValuePair<GrainId, IGrainContext>> GetEnumerator() => _activations.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

async ValueTask IAsyncDisposable.DisposeAsync()
{
var tasks = new List<Task>();
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.
}
}
}
}
Loading