Skip to content

Commit dfc1206

Browse files
authored
Avoid channel closed exception when stopping batching producer (#2262)
1 parent 851044d commit dfc1206

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

src/Proto.Cluster/PubSub/BatchingProducer.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,9 @@ private void RemoveCancelledFromBatch(PubSubBatchWithReceipts batchWrapper)
214214

215215
private void StopAcceptingNewMessages()
216216
{
217-
if (!_publisherChannel.Reader.Completion.IsCompleted)
218-
{
219-
_publisherChannel.Writer.Complete();
220-
}
217+
// Attempt to complete the channel without throwing if it has already been closed
218+
// The publisher loop and publish error handling might both call this concurrently
219+
_publisherChannel.Writer.TryComplete();
221220
}
222221

223222
private async Task PublishBatch(PubSubBatchWithReceipts batchWrapper)

0 commit comments

Comments
 (0)