Skip to content

Commit

Permalink
[Broker] Fix race conditions in closing producers and consumers (#13428)
Browse files Browse the repository at this point in the history
- closing ServerCnx while producers or consumers are created can lead
  to a producer or consumer never getting removed from the topic's
  list of producers

(cherry picked from commit 3316db5)
  • Loading branch information
lhotari authored and zymap committed Dec 23, 2021
1 parent d0eac9a commit c941b2f
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,24 +283,30 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {

// Connection is gone, close the producers immediately
producers.forEach((__, producerFuture) -> {
// prevent race conditions in completing producers
if (!producerFuture.isDone()
&& producerFuture.completeExceptionally(new IllegalStateException("Connection closed."))) {
return;
}
if (producerFuture.isDone() && !producerFuture.isCompletedExceptionally()) {
Producer producer = producerFuture.getNow(null);
producer.closeNow(true);
}
});

consumers.forEach((__, consumerFuture) -> {
Consumer consumer;
if (consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
consumer = consumerFuture.getNow(null);
} else {
// prevent race conditions in completing consumers
if (!consumerFuture.isDone()
&& consumerFuture.completeExceptionally(new IllegalStateException("Connection closed."))) {
return;
}

try {
consumer.close();
} catch (BrokerServiceException e) {
log.warn("Consumer {} was already closed: {}", consumer, e);
if (consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
Consumer consumer = consumerFuture.getNow(null);
try {
consumer.close();
} catch (BrokerServiceException e) {
log.warn("Consumer {} was already closed: {}", consumer, e);
}
}
});
this.service.getPulsarStats().recordConnectionClose();
Expand Down

0 comments on commit c941b2f

Please sign in to comment.