@@ -78,7 +78,7 @@ public static void uncaughtException(Thread thread, Throwable t)
7878 if (t2 != t && (t2 instanceof FSError || t2 instanceof CorruptSSTableException ))
7979 logger .error ("Exception in thread {}" , thread , t2 );
8080 }
81- JVMStabilityInspector . inspectThrowable (t );
81+ inspectThrowable (t , DiskErrorsHandlerService . get ():: inspectDiskError , true );
8282 }
8383
8484 /**
@@ -89,20 +89,20 @@ public static void uncaughtException(Thread thread, Throwable t)
8989 */
9090 public static void inspectThrowable (Throwable t ) throws OutOfMemoryError
9191 {
92- inspectThrowable (t , DiskErrorsHandlerService .get ()::inspectDiskError );
92+ inspectThrowable (t , DiskErrorsHandlerService .get ()::inspectDiskError , false );
9393 }
9494
9595 public static void inspectCommitLogThrowable (Throwable t )
9696 {
97- inspectThrowable (t , ex -> DiskErrorsHandlerService .get ().inspectCommitLogError (ex ));
97+ inspectThrowable (t , ex -> DiskErrorsHandlerService .get ().inspectCommitLogError (ex ), false );
9898 }
9999
100100 public static void inspectJournalThrowable (Throwable t , String journalName , FailurePolicy failurePolicy )
101101 {
102- inspectThrowable (t , th -> inspectJournalError (th , journalName , failurePolicy ));
102+ inspectThrowable (t , th -> inspectJournalError (th , journalName , failurePolicy ), false );
103103 }
104104
105- public static void inspectThrowable (Throwable t , Consumer <Throwable > fn ) throws OutOfMemoryError
105+ public static void inspectThrowable (Throwable t , Consumer <Throwable > fn , boolean isUncaughtException ) throws OutOfMemoryError
106106 {
107107 boolean isUnstable = false ;
108108 if (t instanceof OutOfMemoryError )
@@ -136,13 +136,17 @@ else if (t instanceof UnrecoverableIllegalStateException)
136136 }
137137
138138 // Anything other than an OOM, we should try and heap dump to capture what's going on if configured to do so
139- try
140- {
141- HeapUtils .maybeCreateHeapDump ();
142- }
143- catch (Throwable sub )
139+ if (isUncaughtException && DatabaseDescriptor .getDumpHeapOnUncaughtException ())
144140 {
145- t .addSuppressed (sub );
141+ try
142+ {
143+ // Avoid entering maybeCreateHeapDump unless the setting is enabled to avoid expensive lock
144+ HeapUtils .maybeCreateHeapDump ();
145+ }
146+ catch (Throwable sub )
147+ {
148+ t .addSuppressed (sub );
149+ }
146150 }
147151
148152 if (t instanceof InterruptedException )
@@ -177,7 +181,7 @@ else if (t instanceof UnrecoverableIllegalStateException)
177181 }
178182
179183 if (t .getCause () != null )
180- inspectThrowable (t .getCause (), fn );
184+ inspectThrowable (t .getCause (), fn , isUncaughtException );
181185 }
182186
183187 private static final Set <String > FORCE_HEAP_OOM_IGNORE_SET = ImmutableSet .of ("Java heap space" , "GC Overhead limit exceeded" );
0 commit comments