Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA] setDefaultUncaughtExceptionHandler to log uncaught exception in user thread. #4798

Merged
merged 3 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.ray.api.Ray;
import org.ray.runtime.AbstractRayRuntime;
import org.ray.runtime.util.WorkerUncaughtExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,6 +16,7 @@ public class DefaultWorker {
public static void main(String[] args) {
try {
System.setProperty("ray.worker.mode", "WORKER");
Thread.setDefaultUncaughtExceptionHandler(new WorkerUncaughtExceptionHandler());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think It's a good idea to catch all exceptions. Why not simply redirect stderr to the log file?

I guess the process will not crash even if there's a runtime exception in main thread.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main thread will catch all the exceptions. For exceptions in other threads, user can catch the exception without triggering this hander. This handler will only be triggered when the exception is not caught by user. It may not be a good idea to suppress this exception. Maybe we need to re-throw this exception? After re-throwing, the error message may still appear in raylet.err. If we redirect the stderr, can this message be redirected to the log4j log file? Can threads write the same file at the same time?

Copy link
Contributor

@raulchen raulchen May 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Using LOGGER.error can ensure consistent log format.
  2. This doesn't impact main thread, only the background threads.
  3. If we don't set this, the process won't crash either, because we catch all exceptions in Worker.java

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raulchen One question, after using setDefaultUncaughtExceptionHandler , will Code snippet 2 be executed in the following code example in another thread? If it is yes, the loggic maybe strange. However, I think it probably won't be executed.

Code snippet 1
throw new Exception();
Code snippet 2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it won't execute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the following code:

@Test
  public void testMultithreadException() {
    Thread.setDefaultUncaughtExceptionHandler(new WorkerUncaughtExceptionHandler());
    Thread thread = new Thread(() -> {
      System.out.println("Before exception.");
      int a = 1/0;
      System.out.println("After exception.");
    });
    thread.start();
    try {
      thread.join();
    } catch (Exception e) {
      System.out.println(e);
    }
  }

The output is:

2019-05-16 17:31:37 INFO RayNativeRuntime [main]: RayNativeRuntime started with store /tmp/ray/sockets/object_store, raylet /tmp/ray/sockets/raylet
Before exception.
2019-05-16 17:31:37 ERROR WorkerUncaughtExceptionHandler [Thread-4]: Uncaught exception in thread Thread[Thread-4,5,main]: {}
java.lang.ArithmeticException: / by zero
	at org.ray.api.test.MultiThreadingTest.lambda$testMultithreadException$0(MultiThreadingTest.java:53)
	at java.lang.Thread.run(Thread.java:748)

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Therefore, I think the exception logic won't be break. It is fine to use this handler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just use a lambda here? As the handler is pretty simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a callback function, can this be a lambda?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all interfaces with @FunctionalInterface can be represented as a lambda

Ray.init();
LOGGER.info("Worker started.");
((AbstractRayRuntime)Ray.internal()).loop();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.ray.runtime.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WorkerUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

private static final Logger LOGGER =
LoggerFactory.getLogger(WorkerUncaughtExceptionHandler.class);

@Override
public void uncaughtException(Thread thread, Throwable ex) {
LOGGER.error("Uncaught exception in thread {}: {}", thread, ex);
}
}