Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,34 +270,35 @@ public void teardown() throws UserCodeExecutionException {
Sleeper sleeper = configuration.getSleeperSupplier().get();

backoffIfNeeded(backOff, sleeper);

if (!configuration.getShouldRepeat()) {
incIfPresent(teardownCounter);
setupTeardown.teardown();
return;
}

Repeater<Void, Void> repeater =
Repeater.<Void, Void>builder()
.setBackOff(backOff)
.setSleeper(sleeper)
.setThrowableFunction(
ignored -> {
incIfPresent(teardownCounter);
setupTeardown.teardown();
return null;
})
.build()
.withBackoffCounter(backoffCounter)
.withSleeperCounter(sleeperCounter);

repeater.apply(null);

checkStateNotNull(executor).shutdown();
try {
boolean ignored = executor.awaitTermination(3L, TimeUnit.SECONDS);
} catch (InterruptedException ignored) {
// Ignore the interrupt during teardown.
if (!configuration.getShouldRepeat()) {
incIfPresent(teardownCounter);
setupTeardown.teardown();
return;
}

Repeater<Void, Void> repeater =
Repeater.<Void, Void>builder()
.setBackOff(backOff)
.setSleeper(sleeper)
.setThrowableFunction(
ignored -> {
incIfPresent(teardownCounter);
setupTeardown.teardown();
return null;
})
.build()
.withBackoffCounter(backoffCounter)
.withSleeperCounter(sleeperCounter);

repeater.apply(null);
} finally {
checkStateNotNull(executor).shutdown();
try {
boolean ignored = executor.awaitTermination(3L, TimeUnit.SECONDS);
} catch (InterruptedException ignored) {
// Ignore the interrupt during teardown.
}
Comment on lines +296 to +301
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using checkStateNotNull(executor) in a finally block is risky. If executor is null (for example, if setup() failed before the executor was initialized), this will throw an IllegalStateException. In Java, an exception thrown in a finally block will suppress any exception thrown from the try block, making debugging difficult. It is safer to use a null check here to ensure teardown() is idempotent and doesn't mask other errors. Additionally, the result of awaitTermination can be called without assigning it to a variable, avoiding a potential name conflict with the catch parameter.

        if (executor != null) {
          executor.shutdown();
          try {
            executor.awaitTermination(3L, TimeUnit.SECONDS);
          } catch (InterruptedException ignored) {
            // Ignore the interrupt during teardown.
          }
        }

}
}

Expand Down
Loading