0

I'm trying to get the stacktrace of the Thread.sleep in the following code snippet. Right now, the only stacktrace I'm getting is the one created by Mutiny upon timing out (which isn't helpful). I've tried following things:

  • Catch any exception on the long running code, that's not triggered
  • onCancellation is invoked but the thread stacktrace is the one from the TimeOut
  • onTermination is invoked but the throwable is null
  • onFailure after the timeout contains the stacktrace of the TimeOut

To give more context, in our case, we have a lot more going on which is long running and I can't pinpoint what exactly is causing the timeout. I would like to get the stacktrace of the long running code when the timeout happens so I know what needs to be fixed.

Uni.createFrom()
  .completionStage(CompletableFuture.supplyAsync(() -> {
      try {
          Thread.sleep(20000);
      } catch (Exception e) {
          e.printStackTrace(); //not triggered
      }
      return "item";
  }))
  .onCancellation().invoke(() ->{
      // stacktrace of timeout exception
  })
  .onTermination().invoke((s, throwable, aBoolean) -> {
      // throwable is NULL
  })
  .ifNoItem().after(Duration.ofMillis(100)).fail()
  .onFailure().invoke(ex -> {
      ex.printStackTrace(); //try to get the stacktrace of the Thread.sleep
  })
  .onFailure().recoverWithItem("error")
  .await().atMost(DEFAULT_TIMEOUT);
}

Thanks for assistance in advance!

Bob Claerhout
  • 781
  • 5
  • 24
  • Personally I don't think a stack trace will help, because as you have found catching something in the act of timing-out is going to be very difficult. I think instrumenting your code is a better approach. That means throwing exceptions so the code can catch them (and take action) and logging entry and exit of key methods so you can tell who is executing when the time-out occurs. Yes this can be a pain but it's probably necessary. (That said there might be something way down in the OS that monitors time-outs, as there needs to be an actual timer for that to work.) – markspace Sep 06 '22 at 15:12
  • Also you might want to explain your actual problem in more detail. When `Thread.sleep()` expires, that's normal and isn't going to thrown an exception or "cancel" a task. A network timeout is more of a "problem" and might have some hooks to investigate it, but we'd need to know the details of what exactly is timing out (all setup code for whatever is timing out) so we can make some sort of useful suggestion. – markspace Sep 06 '22 at 15:18
  • I would argue that the stacktrace is actually really helpful. I'm aware that I can add more logging and drill down the issue like that. However, I'm looking for a more elegant approach. – Bob Claerhout Sep 06 '22 at 15:27
  • Considering the actual problem. The `Thread.sleep` never expires, that's the point. I'm getting the `TimeOutException` from Mutiny. The `Thread.Sleep` isn't interrupted neither. I'm more looking for a solution on how I can achieve this in general. The functional code does a lot of different things: fetching data from the database, executing some HTTP posts, processing some stuff, sending messages to a kafka broker... – Bob Claerhout Sep 06 '22 at 15:29
  • 1
    I think I see. So is the above a [mcve]? If you've got code that reproduces the problem, please show us the whole code. I'm not sure how to reproduce your issue with the code above. – markspace Sep 06 '22 at 16:38
  • yes, this is the minimal reproducible example. If you put this in a test or an application, it will timeout and I don't have a clue on where I can get the stacktrace upon timeouting – Bob Claerhout Sep 08 '22 at 10:06

0 Answers0