Skip to content

Commit 0765323

Browse files
committed
Cancel triggered build on InterruptException
With 2.44, and while used in a post build script, when the parent job is cancelled, while the downstream job is waiting to be started, we get an unhandled InterruptException at waitForStart(). Catch InterruptException while waitingForStart and properly cancel the build. Stacktrace (with 2.44): org.jenkinsci.plugins.postbuildscript.PostBuildScriptException: java.lang.InterruptedException at org.jenkinsci.plugins.postbuildscript.processor.Processor.processBuildSteps(Processor.java:190) at org.jenkinsci.plugins.postbuildscript.processor.Processor.processScripts(Processor.java:91) at org.jenkinsci.plugins.postbuildscript.processor.Processor.process(Processor.java:79) at org.jenkinsci.plugins.postbuildscript.processor.Processor.process(Processor.java:73) at org.jenkinsci.plugins.postbuildscript.PostBuildScript.perform(PostBuildScript.java:116) at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20) at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:803) at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:752) at hudson.model.Build$BuildExecution.post2(Build.java:177) at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:697) at hudson.model.Run.execute(Run.java:1932) at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:429) Caused by: java.lang.InterruptedException at java.base/java.lang.Object.wait(Native Method) at java.base/java.lang.Object.wait(Object.java:328) at hudson.remoting.AsyncFutureImpl.get(AsyncFutureImpl.java:79) at hudson.model.queue.FutureImpl.waitForStart(FutureImpl.java:68) at hudson.plugins.parameterizedtrigger.TriggerBuilder.perform(TriggerBuilder.java:146) at org.jenkinsci.plugins.postbuildscript.processor.Processor.processBuildSteps(Processor.java:180) ... 13 more https://phabricator.wikimedia.org/T282893
1 parent ac442af commit 0765323

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/main/java/hudson/plugins/parameterizedtrigger/TriggerBuilder.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,16 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
148148
try {
149149
if (future != null ) {
150150
listener.getLogger().println("Waiting for the completion of " + HyperlinkNote.encodeTo('/'+ p.getUrl(), p.getFullDisplayName()));
151-
Run startedRun = future.waitForStart();
151+
//Run startedRun = future.waitForStart();
152+
Run startedRun;
153+
try {
154+
startedRun = future.waitForStart();
155+
} catch (InterruptedException x) {
156+
listener.getLogger().println( "Build aborting: cancelling queued project " + HyperlinkNote.encodeTo('/'+ p.getUrl(), p.getFullDisplayName()) );
157+
future.cancel(true);
158+
throw x; // rethrow
159+
}
160+
152161
listener.getLogger().println(HyperlinkNote.encodeTo('/' + startedRun.getUrl(), startedRun.getFullDisplayName()) + " started.");
153162

154163
Run completedRun = future.get();

src/test/java/hudson/plugins/parameterizedtrigger/test/TriggerBuilderTest.java

+37
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252

5353

5454
import java.util.ArrayList;
55+
import java.util.concurrent.Future;
5556
import java.util.Collections;
5657
import java.util.List;
5758
import java.util.ListIterator;
59+
import java.util.logging.Level;
5860
import java.util.regex.Pattern;
5961
import java.util.regex.Matcher;
6062
import java.io.IOException;
@@ -64,21 +66,28 @@
6466
import jenkins.model.Jenkins;
6567
import org.jvnet.hudson.test.Issue;
6668
import org.jvnet.hudson.test.JenkinsRule;
69+
import org.jvnet.hudson.test.LoggerRule;
6770
import org.jvnet.hudson.test.SleepBuilder;
6871
import org.mockito.Mockito;
6972

7073
import static org.junit.Assert.assertEquals;
7174
import static org.junit.Assert.assertFalse;
7275
import static org.junit.Assert.assertNotNull;
76+
import static org.junit.Assert.assertNull;
77+
import static org.junit.Assert.assertArrayEquals;
7378
import static org.junit.Assert.assertTrue;
7479
import static org.mockito.ArgumentMatchers.any;
7580
import static org.mockito.Mockito.when;
7681

82+
7783
public class TriggerBuilderTest {
7884

7985
@Rule
8086
public JenkinsRule r = new JenkinsRule();
8187

88+
@Rule
89+
public LoggerRule logger = new LoggerRule();
90+
8291
private BlockableBuildTriggerConfig createTriggerConfig(String projects) {
8392
return new BlockableBuildTriggerConfig(projects, new BlockingBehaviour("never", "never", "never"), null);
8493
}
@@ -224,6 +233,34 @@ public void testNonBlockingTrigger() throws Exception {
224233
"Triggering projects: project1, project2, project3");
225234
}
226235

236+
@Test
237+
public void testCancelsDownstreamBuildWhenInterrupted() throws Exception{
238+
r.jenkins.setNumExecutors(1); // the downstream-project would be in the build queue
239+
240+
FreeStyleProject triggerProject = r.createFreeStyleProject("upstream-project");
241+
FreeStyleProject downstreamProject = r.createFreeStyleProject("downstream-project");
242+
243+
TriggerBuilder triggerBuilder = new TriggerBuilder(createTriggerConfig("downstream-project"));
244+
245+
triggerProject.getBuildersList().add(triggerBuilder);
246+
247+
QueueTaskFuture<FreeStyleBuild> parentBuild = triggerProject.scheduleBuild2(0);
248+
parentBuild.waitForStart();
249+
250+
// Now cancel it and let it finishes
251+
triggerProject.getLastBuild().getExecutor().interrupt();
252+
parentBuild.get();
253+
254+
assertLines(triggerProject.getLastBuild(),
255+
"Waiting for the completion of downstream-project",
256+
"Build aborting: cancelling queued project downstream-project",
257+
"Build was aborted",
258+
"Finished: ABORTED"
259+
);
260+
assertNull("No downstream build has been run", downstreamProject.getLastBuild());
261+
assertEquals("No build left in queue", 0, r.jenkins.getQueue().countBuildableItems());
262+
}
263+
227264
@Test
228265
public void testConsoleOutputWithCounterParameters() throws Exception{
229266
r.createFreeStyleProject("project1");

0 commit comments

Comments
 (0)