diff --git a/sdk/eventhub/Azure.Messaging.EventHubs/src/Producer/EventHubBufferedProducerClient.cs b/sdk/eventhub/Azure.Messaging.EventHubs/src/Producer/EventHubBufferedProducerClient.cs index 2138563bc3fa1..41ac5929f643d 100644 --- a/sdk/eventhub/Azure.Messaging.EventHubs/src/Producer/EventHubBufferedProducerClient.cs +++ b/sdk/eventhub/Azure.Messaging.EventHubs/src/Producer/EventHubBufferedProducerClient.cs @@ -1966,8 +1966,14 @@ private Task TrackPublishHandlerAsActiveAsync(Task handlerTask) _activePublishingHandlers.TryAdd(handlerTask, 0); - var continationTask = handlerTask.ContinueWith((runTask, trackedTask) => _activePublishingHandlers.TryRemove((Task)trackedTask, out _), handlerTask, TaskScheduler.Default); - return continationTask; + var continuationTask = handlerTask.ContinueWith(static (runTask, state) => + { + var (trackedTask, activeHandlers) = (Tuple>)state; + return activeHandlers.TryRemove(trackedTask, out _); + }, + Tuple.Create(handlerTask, _activePublishingHandlers), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); + + return continuationTask; } ///