diff --git a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Kafka/KafkaHelper.cs b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Kafka/KafkaHelper.cs index e77cf84309d7..b4cb44fe8468 100644 --- a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Kafka/KafkaHelper.cs +++ b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Kafka/KafkaHelper.cs @@ -462,32 +462,20 @@ internal static void TryInjectHeaders( options.DuckCast().RequestTimeout = TimeSpan.FromSeconds(2); var duckTask = adminClient.DescribeClusterAsync(options); - var describeResult = SafeGetResult, IDescribeClusterResult>(duckTask); - return describeResult?.ClusterId; - static TResult? SafeGetResult(TTask task) - where TTask : IDuckTypeTask - 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); } } diff --git a/tracer/src/Datadog.Trace/DuckTyping/IDuckTypeTask.cs b/tracer/src/Datadog.Trace/DuckTyping/IDuckTypeTask.cs index 1e682e95987f..1d4a6110daab 100644 --- a/tracer/src/Datadog.Trace/DuckTyping/IDuckTypeTask.cs +++ b/tracer/src/Datadog.Trace/DuckTyping/IDuckTypeTask.cs @@ -12,11 +12,6 @@ namespace Datadog.Trace.DuckTyping; /// Type of the result public interface IDuckTypeTask : IDuckType { - /// - /// Gets a value indicating whether if the task is completed - /// - bool IsCompletedSuccessfully { get; } - /// /// Gets the result of the task /// @@ -34,11 +29,6 @@ public interface IDuckTypeTask : IDuckType /// public interface IDuckTypeTask : IDuckType { - /// - /// Gets a value indicating whether if the task is completed - /// - bool IsCompletedSuccessfully { get; } - /// /// Gets the awaiter for the task ///