Skip to content

Implement IAsyncDisposable on AdminClient #2470

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
74 changes: 37 additions & 37 deletions src/Confluent.Kafka/AdminClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Confluent.Kafka
/// <summary>
/// Implements an Apache Kafka admin client.
/// </summary>
internal class AdminClient : IAdminClient
internal sealed class AdminClient : IAdminClient
{
private int cancellationDelayMaxMs;

Expand Down Expand Up @@ -1654,51 +1654,51 @@ public Handle Handle

/// <summary>
/// Releases all resources used by this AdminClient. In the current
/// implementation, this method may block for up to 100ms. This
/// will be replaced with a non-blocking version in the future.
/// implementation, this method may block for up to 100ms. It is
/// recommended to use <see cref="DisposeAsync"/> instead.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}


/// <summary>
/// Releases the unmanaged resources used by the
/// <see cref="Confluent.Kafka.AdminClient" />
/// and optionally disposes the managed resources.
/// </summary>
/// <param name="disposing">
/// true to release both managed and unmanaged resources;
/// false to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
callbackCts.Cancel();
try
{
callbackCts.Cancel();
try
{
callbackTask.Wait();
}
catch (AggregateException e)
{
if (e.InnerException.GetType() != typeof(TaskCanceledException))
{
// program execution should never get here.
throw e.InnerException;
}
}
finally
callbackTask.Wait();
}
catch (AggregateException e)
{
if (e.InnerException.GetType() != typeof(TaskCanceledException))
{
callbackCts.Dispose();
// program execution should never get here.
throw e.InnerException;
}

DisposeResources();
}
finally
{
callbackCts.Dispose();
}

DisposeResources();
}

#if NET6_0_OR_GREATER
public async ValueTask DisposeAsync()
{
callbackCts.Cancel();
try
{
await callbackTask;
}
catch (TaskCanceledException)
{
}
finally
{
callbackCts.Dispose();
}

DisposeResources();
}
#endif

private void DisposeResources()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Confluent.Kafka/IAdminClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ namespace Confluent.Kafka
/// Defines an Apache Kafka admin client.
/// </summary>
public interface IAdminClient : IClient
#if NET6_0_OR_GREATER
, IAsyncDisposable
#endif
{
/// <summary>
/// DEPRECATED.
Expand Down
15 changes: 15 additions & 0 deletions test/Confluent.Kafka.UnitTests/Admin/DisposeAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading.Tasks;
using Xunit;

namespace Confluent.Kafka.UnitTests;

public class DisposeAsync
{
[Fact]
public async Task TestDisposeAsyncDoesNotThrow()
{
var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = "localhost:90922" }).Build();

await adminClient.DisposeAsync();
}
}