lhotari commented on a change in pull request #14538:
URL: https://github.com/apache/pulsar/pull/14538#discussion_r817636144



##########
File path: 
pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientImpl.java
##########
@@ -710,14 +713,30 @@ public void close() throws PulsarClientException {
         final CompletableFuture<Void> closeFuture = new CompletableFuture<>();
         List<CompletableFuture<Void>> futures = new ArrayList<>();
 
-        producers.forEach(p -> futures.add(p.closeAsync()));
-        consumers.forEach(c -> futures.add(c.closeAsync()));
+        producers.forEach(p -> futures.add(p.closeAsync().handle((__, t) -> {
+            log.error("Error closing producer {}", p, t);
+            return null;
+        })));
+        consumers.forEach(c -> futures.add(c.closeAsync().handle((__, t) -> {
+            log.error("Error closing consumer {}", c, t);
+            return null;
+        })));
 
         // Need to run the shutdown sequence in a separate thread to prevent 
deadlocks
         // If there are consumers or producers that need to be shutdown we 
cannot use the same thread
         // to shutdown the EventLoopGroup as well as that would be trying to 
shutdown itself thus a deadlock
         // would happen
-        FutureUtil.waitForAll(futures).thenRun(() -> new Thread(() -> {
+        CompletableFuture<Void> combinedFuture = 
FutureUtil.waitForAll(futures);
+        ScheduledExecutorService shutdownExecutor = 
Executors.newSingleThreadScheduledExecutor(

Review comment:
       I don't think it's a problem from performance perspective in any way. I 
admin that it's ugly. 
   The other thread pre-existed and it's needed to prevent deadlocks in certain 
cases. 
   
   Adding timeout handling for futures requires a ScheduledExecutorService 
reference. Since this is part of shutdown, other executors will be shutdown so 
that's why need a new instance. 
   
   btw. Timeout handling for Futures exists in the JDK since Java 9 with 
[CompletableFuture#completeOnTimeout](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CompletableFuture.html#completeOnTimeout(T,long,java.util.concurrent.TimeUnit)).
 Since we must support Java 8, we need to have our own solution for 
CompletableFuture timeout handling. In Java 9+ , there's an internal class 
called java.util.concurrent.CompletableFuture.Delayer which contains the since 
threaded executor that the JDK uses for completeOnTimeout.
   




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to