Skip to content

Commit

Permalink
Merge pull request #355 from jglick/Callback.finished
Browse files Browse the repository at this point in the history
`PlaceholderTask.Callback.finished` did not correctly suppress `CancelledItemListener`, and `willContinue` was unreliable
  • Loading branch information
jglick authored Feb 2, 2024
2 parents 63864b7 + 114fd9a commit e57634f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
16 changes: 6 additions & 10 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/*
See the documentation for more options:
https://github.com/jenkins-infra/pipeline-library/
*/
configurations = [[platform: 'linux', jdk: 21]]
if (env.CHANGE_ID == null) { // TODO https://github.com/jenkins-infra/helpdesk/issues/3931 workaround
configurations += [platform: 'windows', jdk: 17]
}
buildPlugin(
useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests
forkCount: '1C',
configurations: [
[platform: 'linux', jdk: 21],
[platform: 'windows', jdk: 17],
])
configurations: configurations
)
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ public boolean equals(Object obj) {
}

private static void finish(StepContext context, @CheckForNull final String cookie) {
RunningTask runningTask = RunningTasks.get(context, t -> t, () -> null);
RunningTask runningTask = RunningTasks.get(context);
if (runningTask == null) {
LOGGER.fine(() -> "no known running task for " + context);
return;
Expand All @@ -900,6 +900,7 @@ private static void finish(StepContext context, @CheckForNull final String cooki
return;
}
assert runningTask.launcher != null;
runningTask.execution = null;
Timer.get().submit(() -> execution.completed(null)); // JENKINS-31614
if (cookie == null) {
LOGGER.fine(() -> "no cookie to kill from " + context);
Expand Down Expand Up @@ -952,7 +953,7 @@ private static final class Callback extends BodyExecutionCallback.TailCall {
finish(execution.getContext(), cookie);
}
execution.body = null;
RunningTask t = RunningTasks.remove(execution.getContext());
RunningTask t = RunningTasks.get(execution.getContext());
if (t != null) {
LOGGER.fine(() -> "cancelling any leftover task from " + execution.getContext());
boolean _stopping = t.stopping;
Expand All @@ -967,6 +968,7 @@ private static final class Callback extends BodyExecutionCallback.TailCall {
}
execution.state = null;
bodyContext.saveState();
RunningTasks.remove(execution.getContext());
}

}
Expand Down Expand Up @@ -1239,6 +1241,10 @@ static <T> T get(StepContext context, Function<RunningTask, T> fn, Supplier<T> f
}
}

static @CheckForNull RunningTask get(StepContext context) {
return get(context, t -> t, () -> null);
}

static void run(StepContext context, Consumer<RunningTask> fn) {
RunningTask t = find(context);
if (t != null) {
Expand All @@ -1248,10 +1254,12 @@ static void run(StepContext context, Consumer<RunningTask> fn) {
}
}

static @CheckForNull RunningTask remove(StepContext context) {
static void remove(StepContext context) {
RunningTasks holder = ExtensionList.lookupSingleton(RunningTasks.class);
synchronized (holder) {
return holder.runningTasks.remove(context);
if (holder.runningTasks.remove(context) == null) {

Check warning on line 1260 in src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1260 is only partially covered, one branch is missing
LOGGER.fine(() -> "no RunningTask to remove associated with " + context);

Check warning on line 1261 in src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1261 is not covered by tests
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import jenkins.model.Jenkins;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.empty;
import org.jenkinsci.plugins.durabletask.executors.OnceRetentionStrategy;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand All @@ -55,6 +58,7 @@
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsSessionRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.SimpleCommandLauncher;
import org.jvnet.hudson.test.TestExtension;

Expand All @@ -65,6 +69,7 @@ public final class ExecutorStep2Test {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public JenkinsSessionRule rr = new JenkinsSessionRule();
@Rule public LoggerRule logging = new LoggerRule();

@Issue("JENKINS-53837")
@Test public void queueTaskOwnerCorrectWhenRestarting() throws Throwable {
Expand Down Expand Up @@ -117,8 +122,11 @@ public String getShortDescription() {
r.jenkins.clouds.add(new TestCloud());
var p = r.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("node('test') {}", true));
logging.record(OnceRetentionStrategy.class, Level.FINE).record(ExecutorStepExecution.class, Level.FINE);
r.assertLogContains("Running on test-1", r.buildAndAssertSuccess(p));
await("waiting for test-1 to be removed").until(r.jenkins::getNodes, empty());
r.assertLogContains("Running on test-2", r.buildAndAssertSuccess(p));
await("waiting for test-2 to be removed").until(r.jenkins::getNodes, empty());
});
}
// adapted from org.jenkinci.plugins.mock_slave.MockCloud
Expand Down

0 comments on commit e57634f

Please sign in to comment.