|
77 | 77 | import com.palantir.logsafe.UnsafeArg; |
78 | 78 | import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; |
79 | 79 | import com.palantir.logsafe.exceptions.SafeIllegalStateException; |
| 80 | +import com.palantir.logsafe.exceptions.SafeRuntimeException; |
80 | 81 | import com.palantir.logsafe.logger.SafeLogger; |
81 | 82 | import com.palantir.logsafe.logger.SafeLoggerFactory; |
82 | 83 | import com.palantir.nylon.threads.ThreadNames; |
83 | 84 | import com.palantir.refreshable.Refreshable; |
84 | 85 | import com.palantir.util.JMXUtils; |
85 | 86 | import com.palantir.util.Ownable; |
| 87 | +import com.palantir.util.Pair; |
86 | 88 | import java.math.BigInteger; |
87 | 89 | import java.time.Instant; |
88 | 90 | import java.util.ArrayDeque; |
@@ -1241,10 +1243,38 @@ private String safeLockStats() { |
1241 | 1243 | @Override |
1242 | 1244 | public void close() { |
1243 | 1245 | if (isShutDown.compareAndSet(false, true)) { |
1244 | | - lockReapRunner.close(); |
1245 | | - threadInfoSnapshotManager.close(); |
1246 | | - blockingThreads.forEach(Thread::interrupt); |
1247 | | - callOnClose.run(); |
| 1246 | + List<Pair<String, Runnable>> namedClosingActions = List.of( |
| 1247 | + Pair.create("lockReapRunnerClose", lockReapRunner::close), |
| 1248 | + Pair.create("threadInfoSnapshotManagerClose", threadInfoSnapshotManager::close), |
| 1249 | + Pair.create("blockingThreadsInterruption", () -> blockingThreads.forEach(Thread::interrupt)), |
| 1250 | + Pair.create("onCloseRunnable", callOnClose)); |
| 1251 | + |
| 1252 | + List<Exception> encounteredExceptions = new ArrayList<>(); |
| 1253 | + namedClosingActions.forEach(namedAction -> { |
| 1254 | + try { |
| 1255 | + namedAction.getRhSide().run(); |
| 1256 | + } catch (RuntimeException exception) { |
| 1257 | + log.warn( |
| 1258 | + "Failed to perform closing action due to exception", |
| 1259 | + SafeArg.of("action", namedAction.getLhSide()), |
| 1260 | + exception); |
| 1261 | + encounteredExceptions.add(exception); |
| 1262 | + } catch (Error error) { |
| 1263 | + log.warn( |
| 1264 | + "Failed to perform closing action due to error", |
| 1265 | + SafeArg.of("action", namedAction.getLhSide()), |
| 1266 | + error); |
| 1267 | + encounteredExceptions.forEach(error::addSuppressed); |
| 1268 | + throw error; |
| 1269 | + } |
| 1270 | + }); |
| 1271 | + |
| 1272 | + if (!encounteredExceptions.isEmpty()) { |
| 1273 | + SafeRuntimeException exception = |
| 1274 | + new SafeRuntimeException("Encountered exceptions or errors while performing closing actions"); |
| 1275 | + encounteredExceptions.forEach(exception::addSuppressed); |
| 1276 | + throw exception; |
| 1277 | + } |
1248 | 1278 | } |
1249 | 1279 | } |
1250 | 1280 |
|
|
0 commit comments