Skip to content

Commit

Permalink
Merge pull request #846 from jglick/safeExit
Browse files Browse the repository at this point in the history
`doSafeExit` from `RealJenkinsRule` could hang
  • Loading branch information
jglick authored Sep 26, 2024
2 parents 7f819ec + f3dfdd5 commit 2b74fdc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1342,11 +1342,17 @@ public void doStep(StaplerRequest req, StaplerResponse rsp) throws Throwable {
}
Init2.writeSer(rsp.getOutputStream(), new OutputPayload(object, err));
}
public HttpResponse doExit(@QueryParameter String token) throws IOException {
public HttpResponse doExit(@QueryParameter String token) throws IOException, InterruptedException {
checkToken(token);
try (ACLContext ctx = ACL.as2(ACL.SYSTEM2)) {
return Jenkins.get().doSafeExit((StaplerRequest) null);
Jenkins j = Jenkins.get();
j.doQuietDown(true, 30_000, null, false); // 30s < 60s timeout of stopJenkins
// Cannot use doExit since it requires StaplerRequest2, so would throw an error on older cores:
j.getLifecycle().onStop("RealJenkinsRule", null);
j.cleanUp();
new Thread(() -> System.exit(0), "exiting").start();
}
return HttpResponses.ok();
}
public void doTimeout(@QueryParameter String token) {
checkToken(token);
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/jvnet/hudson/test/RealJenkinsRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,22 @@ private static void _noDetachedPlugins(JenkinsRule r) throws Throwable {
assertThat(plugins, hasSize(1));
assertThat(plugins.get(0).getShortName(), is("RealJenkinsRuleInit"));
}

@Test
public void safeExit() throws Throwable {
rr.then(RealJenkinsRuleTest::_safeExit);
}

private static void _safeExit(JenkinsRule r) throws Throwable {
var p = r.createFreeStyleProject();
p.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
Thread.sleep(Long.MAX_VALUE);
return false;
}
});
p.scheduleBuild2(0).waitForStart();
}

}

0 comments on commit 2b74fdc

Please sign in to comment.