Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -462,32 +462,20 @@ internal static void TryInjectHeaders<TTopicPartitionMarker, TMessage>(
options.DuckCast<IDescribeClusterOptions>().RequestTimeout = TimeSpan.FromSeconds(2);

var duckTask = adminClient.DescribeClusterAsync(options);
var describeResult = SafeGetResult<IDuckTypeTask<IDescribeClusterResult>, IDescribeClusterResult>(duckTask);
return describeResult?.ClusterId;

static TResult? SafeGetResult<TTask, TResult>(TTask task)
where TTask : IDuckTypeTask<TResult>
where TResult : IDescribeClusterResult
var originalContext = SynchronizationContext.Current;
try
{
if (task.IsCompletedSuccessfully)
{
return task.Result;
}

var originalContext = SynchronizationContext.Current;
try
{
// Set the synchronization context to null to avoid deadlocks.
SynchronizationContext.SetSynchronizationContext(null);
// Set the synchronization context to null to avoid deadlocks.
SynchronizationContext.SetSynchronizationContext(null);

// Wait synchronously for the task to complete.
return task.GetAwaiter().GetResult();
}
finally
{
// Restore the original synchronization context.
SynchronizationContext.SetSynchronizationContext(originalContext);
}
// Wait synchronously for the task to complete.
return duckTask.GetAwaiter().GetResult()?.ClusterId;
}
finally
{
// Restore the original synchronization context.
SynchronizationContext.SetSynchronizationContext(originalContext);
}
}

Expand Down
10 changes: 0 additions & 10 deletions tracer/src/Datadog.Trace/DuckTyping/IDuckTypeTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace Datadog.Trace.DuckTyping;
/// <typeparam name="T">Type of the result</typeparam>
public interface IDuckTypeTask<out T> : IDuckType
{
/// <summary>
/// Gets a value indicating whether if the task is completed
/// </summary>
bool IsCompletedSuccessfully { get; }

/// <summary>
/// Gets the result of the task
/// </summary>
Expand All @@ -34,11 +29,6 @@ public interface IDuckTypeTask<out T> : IDuckType
/// </summary>
public interface IDuckTypeTask : IDuckType
{
/// <summary>
/// Gets a value indicating whether if the task is completed
/// </summary>
bool IsCompletedSuccessfully { get; }

/// <summary>
/// Gets the awaiter for the task
/// </summary>
Expand Down
Loading