diff --git a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java index 074188328..4497df975 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java @@ -171,6 +171,7 @@ public void stop(@NonNull Throwable cause) throws Exception { } else { LOGGER.warning(() -> "failed to cancel " + item + " in response to " + cause); } + // TODO RunningTasks.remove(context) ? break; } else { LOGGER.log(FINE, "no match on {0} with {1} vs. {2}", new Object[] {item, task.context, context}); @@ -1223,7 +1224,12 @@ public static class RunningTasks { static void add(StepContext context) { RunningTasks holder = ExtensionList.lookupSingleton(RunningTasks.class); synchronized (holder) { - holder.runningTasks.putIfAbsent(context, new RunningTask()); + RunningTask existing = holder.runningTasks.putIfAbsent(context, new RunningTask()); + if (existing == null) { + LOGGER.fine(() -> "registered new RunningTask for " + context); + } else { + LOGGER.warning(() -> context + " already had a RunningTask"); + } } } @@ -1260,7 +1266,10 @@ static void run(StepContext context, Consumer fn) { static void remove(StepContext context) { RunningTasks holder = ExtensionList.lookupSingleton(RunningTasks.class); synchronized (holder) { - if (holder.runningTasks.remove(context) == null) { + if (holder.runningTasks.remove(context) != null) { + LOGGER.fine(() -> "removed RunningTask for " + context); + Thread.dumpStack();//TODO + } else { LOGGER.fine(() -> "no RunningTask to remove associated with " + context); } } diff --git a/src/test/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepTest.java b/src/test/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepTest.java index 56b210ad3..ffbfc1a74 100644 --- a/src/test/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepTest.java +++ b/src/test/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepTest.java @@ -1228,6 +1228,25 @@ public void getOwnerTaskPermissions() throws Throwable { }); } + @Issue("JENKINS-60507") + @Test public void recreateJob() throws Throwable { + Assume.assumeFalse(useWatching); // irrelevant here + sessions.then(r -> { + logging.record(ExecutorStepExecution.class, Level.FINE); + WorkflowJob p = r.createProject(WorkflowJob.class, "p"); + p.setDefinition(new CpsFlowDefinition("node('nonexistent') {}", true)); + WorkflowRun b = p.scheduleBuild2(0).waitForStart(); + assertTrue(Queue.getInstance().cancel(await().until( + () -> Stream.of(Queue.getInstance().getItems()).filter(i -> i.task instanceof ExecutorStepExecution.PlaceholderTask && i.task.getOwnerTask() == p). + findFirst().orElse(null), notNullValue()))); + r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b)); + p.delete(); + WorkflowJob p2 = r.createProject(WorkflowJob.class, "p"); + p2.setDefinition(new CpsFlowDefinition("node {}", true)); + r.buildAndAssertSuccess(p2); + }); + } + private static class MainAuthenticator extends QueueItemAuthenticator { @Override public Authentication authenticate(Queue.Task task) { return task instanceof WorkflowJob ? User.getById("dev", true).impersonate() : null;