chia7712 commented on a change in pull request #9707: URL: https://github.com/apache/kafka/pull/9707#discussion_r538242938
########## File path: clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java ########## @@ -1201,7 +1211,7 @@ private void close(Duration timeout, boolean swallowException) { // this will keep track of the first encountered exception AtomicReference<Throwable> firstException = new AtomicReference<>(); - boolean invokedFromCallback = Thread.currentThread() == this.ioThread; + boolean invokedFromCallback = isInvokedFromCallback(); Review comment: this local variable is redundant ########## File path: clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java ########## @@ -1079,6 +1079,13 @@ private void ensureValidRecordSize(int size) { " configuration."); } + /** + * @return is called in callback. Review comment: I feel the method name is good to be document. ########## File path: clients/src/test/java/org/apache/kafka/clients/producer/KafkaProducerTest.java ########## @@ -391,6 +391,27 @@ Sender newSender(LogContext logContext, KafkaClient kafkaClient, ProducerMetadat }; } + @Test(timeout = 10000) + public void testFlushInCallbackNotCauseDeadlock() throws InterruptedException { + Map<String, Object> configs = new HashMap<>(); + configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999"); + ProducerMetadata metadata = mock(ProducerMetadata.class); + + when(metadata.fetch()).thenReturn(emptyCluster, emptyCluster, emptyCluster, onePartitionCluster); + + KafkaProducer<String, String> producer = producerWithOverrideNewSender(configs, metadata); + ProducerRecord<String, String> record = new ProducerRecord<>(topic, "value"); + + producer.send(record, (metadata1, exception) -> { + producer.flush(); + Assert.fail("Flush method needs to throw exception."); Review comment: I don't think this is valid since this callback is triggered by another thread. The assert failure can't be aware by test thread. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org