-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -15,6 +16,7 @@ public class DefaultWorker { | |
public static void main(String[] args) { | ||
try { | ||
System.setProperty("ray.worker.mode", "WORKER"); | ||
Thread.setDefaultUncaughtExceptionHandler(new WorkerUncaughtExceptionHandler()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just use a lambda here? As the handler is pretty simple. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a callback function, can this be a lambda? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, all interfaces with |
||
Ray.init(); | ||
LOGGER.info("Worker started."); | ||
((AbstractRayRuntime)Ray.internal()).loop(); | ||
|
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); | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOGGER.error
can ensure consistent log format.There was a problem hiding this comment.
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
, willCode 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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
The output is:
Therefore, I think the exception logic won't be break. It is fine to use this handler.