Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.

Commit 9ef9d34

Browse files
authored
Try to close all resources even if one throws (#6746)
1 parent fe1c71b commit 9ef9d34

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

changelog/@unreleased/pr-6746.v2.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: fix
2+
fix:
3+
description: Try to close all resources even if one throws
4+
links:
5+
- https://github.com/palantir/atlasdb/pull/6746

lock-impl/src/main/java/com/palantir/lock/impl/LockServiceImpl.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@
7777
import com.palantir.logsafe.UnsafeArg;
7878
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
7979
import com.palantir.logsafe.exceptions.SafeIllegalStateException;
80+
import com.palantir.logsafe.exceptions.SafeRuntimeException;
8081
import com.palantir.logsafe.logger.SafeLogger;
8182
import com.palantir.logsafe.logger.SafeLoggerFactory;
8283
import com.palantir.nylon.threads.ThreadNames;
8384
import com.palantir.refreshable.Refreshable;
8485
import com.palantir.util.JMXUtils;
8586
import com.palantir.util.Ownable;
87+
import com.palantir.util.Pair;
8688
import java.math.BigInteger;
8789
import java.time.Instant;
8890
import java.util.ArrayDeque;
@@ -1241,10 +1243,38 @@ private String safeLockStats() {
12411243
@Override
12421244
public void close() {
12431245
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+
}
12481278
}
12491279
}
12501280

0 commit comments

Comments
 (0)