diff --git a/projects/RabbitMQ.Client/Impl/Channel.PublisherConfirms.cs b/projects/RabbitMQ.Client/Impl/Channel.PublisherConfirms.cs index 680ac314f..2ba8e0524 100644 --- a/projects/RabbitMQ.Client/Impl/Channel.PublisherConfirms.cs +++ b/projects/RabbitMQ.Client/Impl/Channel.PublisherConfirms.cs @@ -295,7 +295,10 @@ await _confirmSemaphore.WaitAsync(cancellationToken) if (_publisherConfirmationTrackingEnabled) { publisherConfirmationTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - _confirmsTaskCompletionSources[publishSequenceNumber] = publisherConfirmationTcs; + if (!_confirmsTaskCompletionSources.TryAdd(publishSequenceNumber, publisherConfirmationTcs)) + { + throw new InvalidOperationException($"Failed to track the publisher confirmation for sequence number '{publishSequenceNumber}' because it already exists."); + } } _nextPublishSeqNo++;