@@ -34,7 +34,7 @@ public GRIPCoreModule() {
3434
3535 try {
3636 // Remove the default handlers that stream to System.err
37- for (Handler handler : globalLogger .getHandlers ()) {
37+ for (Handler handler : globalLogger .getHandlers ()) {
3838 globalLogger .removeHandler (handler );
3939 }
4040
@@ -45,7 +45,7 @@ public GRIPCoreModule() {
4545 globalLogger .setLevel (Level .FINE );
4646
4747 // We need to stream to System.out instead of System.err
48- final StreamHandler sh = new StreamHandler (System .out , new SimpleFormatter ()) {
48+ final StreamHandler sh = new StreamHandler (System .out , new SimpleFormatter ()) {
4949
5050 @ Override
5151 public synchronized void publish (final LogRecord record ) {
@@ -114,14 +114,36 @@ private void onSubscriberException(Throwable exception, SubscriberExceptionConte
114114 }
115115 }
116116
117+ /*
118+ * We intentionally catch the throwable because we can't be sure what will happen.
119+ * We drop the last throwable because we clearly have a problem beyond our control.
120+ */
121+ @ SuppressWarnings ({"PMD.AvoidCatchingThrowable" , "PMD.EmptyCatchBlock" })
117122 private void onThreadException (Thread thread , Throwable exception ) {
118- if (exception instanceof InterruptedException ) {
119- logger .log (Level .FINE , "InterruptedException from thread " + thread , exception );
120- Thread .currentThread ().interrupt ();
121- } else {
122- // This can potentially happen before the main class has even been loaded to handle these exceptions
123- logger .log (Level .SEVERE , "Uncaught Exception on thread " + thread , exception );
124- eventBus .post (new UnexpectedThrowableEvent (exception , thread + " threw an exception" ));
123+ // Don't do anything outside of a try catch block when dealing with thread death
124+ try {
125+ if (exception instanceof Error && !(exception instanceof AssertionError )) {
126+ // Try, this may not work. but its worth a shot.
127+ logger .log (Level .SEVERE , "Error from " + thread , exception );
128+ } else if (exception instanceof InterruptedException ) {
129+ logger .log (Level .FINE , "InterruptedException from thread " + thread , exception );
130+ Thread .currentThread ().interrupt ();
131+ } else {
132+ // This can potentially happen before the main class has even been loaded to handle these exceptions
133+ logger .log (Level .SEVERE , "Uncaught Exception on thread " + thread , exception );
134+ final UnexpectedThrowableEvent event = new UnexpectedThrowableEvent (exception , thread + " threw an exception" );
135+ eventBus .post (event );
136+ // It is possible that the event was never handled.
137+ // If it wasn't we want to perform the shutdown hook here.
138+ event .shutdownIfFatal ();
139+ }
140+ } catch (Throwable throwable ) {
141+ try {
142+ logger .log (Level .SEVERE , "Failed to handle thread exception" , throwable );
143+ } catch (Throwable throwable1 ) {
144+ // Seriously, just give up at this point.
145+ }
125146 }
147+ // Don't do anything outside of a try catch block when dealing with thread death
126148 }
127149}
0 commit comments