You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "thetumbled (via GitHub)" <gi...@apache.org> on 2023/04/21 04:28:55 UTC

[GitHub] [pulsar] thetumbled commented on pull request #19258: [fix][txn] Catch and log runtime exceptions in async operations

thetumbled commented on PR #19258:
URL: https://github.com/apache/pulsar/pull/19258#issuecomment-1517240953

   I notice that `safeRun` method in PR https://github.com/apache/pulsar/pull/17484 is implemented by try/catch block, which is different from here. 
   ```
       public static SafeRunnable safeRun(Runnable runnable, Consumer<Throwable> exceptionHandler) {
           return new SafeRunnable() {
               @Override
               public void safeRun() {
                   try {
                       runnable.run();
                   } catch (Throwable t) {
                       exceptionHandler.accept(t);
                       throw t;
                   }
               }
           };
       }
   ```
   
   And I write some test code to test `safeRunAsync` method.
   ```
       public static void safeRunAsync(Runnable runnable,
                                       Executor executor,
                                       CompletableFuture completableFuture) {
           CompletableFuture
                   .runAsync(runnable, executor)
                   .exceptionally((throwable) -> {
                       completableFuture.completeExceptionally(throwable);
                       return null;
                   });
       }
   
       @Test
       public void test() throws ExecutionException, InterruptedException {
           CompletableFuture<String> future = new CompletableFuture<>();
           ExecutorService executorService = Executors.newSingleThreadExecutor();
           executorService.shutdown();
           safeRunAsync(() -> {
               try {
                   Thread.sleep(2000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               future.completeExceptionally(new RuntimeException("test"));
           }, executorService, future);
           future.exceptionally(throwable -> {
               System.out.println(throwable.getMessage());
               return null;
           }).get();
       }
   ```
   The test is failed, we can't catch the error thrown by thread pool itself.
   <img width="762" alt="image" src="https://user-images.githubusercontent.com/52550727/233540766-c5284663-71b2-41b6-bff8-94387ce3853f.png">
   
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org