|
24 | 24 |
|
25 | 25 | package org.jenkinsci.plugins.workflow.flow;
|
26 | 26 |
|
| 27 | +import static org.awaitility.Awaitility.await; |
27 | 28 | import static org.hamcrest.MatcherAssert.assertThat;
|
28 | 29 | import static org.hamcrest.Matchers.containsString;
|
29 | 30 | import static org.hamcrest.Matchers.hasItem;
|
|
38 | 39 | import hudson.model.TaskListener;
|
39 | 40 | import hudson.model.queue.QueueTaskFuture;
|
40 | 41 | import java.io.Serializable;
|
41 |
| -import java.time.Duration; |
42 |
| -import java.time.Instant; |
| 42 | +import java.lang.ref.WeakReference; |
43 | 43 | import java.util.Collections;
|
| 44 | +import java.util.HashMap; |
| 45 | +import java.util.Map; |
44 | 46 | import java.util.Set;
|
45 |
| -import java.util.function.Supplier; |
| 47 | +import java.util.concurrent.TimeUnit; |
46 | 48 | import java.util.logging.Level;
|
47 |
| -import org.hamcrest.Matcher; |
| 49 | +import jenkins.model.Jenkins; |
48 | 50 | import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
|
49 | 51 | import org.jenkinsci.plugins.workflow.job.WorkflowJob;
|
50 | 52 | import org.jenkinsci.plugins.workflow.job.WorkflowRun;
|
51 | 53 | import org.jenkinsci.plugins.workflow.steps.Step;
|
52 | 54 | import org.jenkinsci.plugins.workflow.steps.StepContext;
|
53 | 55 | import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
|
54 | 56 | import org.jenkinsci.plugins.workflow.steps.StepExecution;
|
| 57 | +import org.jenkinsci.plugins.workflow.steps.StepExecutions; |
55 | 58 | import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
|
56 | 59 | import org.junit.ClassRule;
|
57 | 60 | import org.junit.Test;
|
|
60 | 63 | import org.jvnet.hudson.test.Issue;
|
61 | 64 | import org.jvnet.hudson.test.LoggerRule;
|
62 | 65 | import org.jvnet.hudson.test.JenkinsSessionRule;
|
| 66 | +import org.jvnet.hudson.test.MemoryAssert; |
63 | 67 | import org.jvnet.hudson.test.TestExtension;
|
64 | 68 | import org.kohsuke.stapler.DataBoundConstructor;
|
65 | 69 |
|
@@ -132,7 +136,7 @@ public class FlowExecutionListTest {
|
132 | 136 | at org.jenkinsci.plugins.workflow.flow.FlowExecutionList$ItemListenerImpl.onLoaded(FlowExecutionList.java:175)
|
133 | 137 | at jenkins.model.Jenkins.<init>(Jenkins.java:1019)
|
134 | 138 | */
|
135 |
| - waitFor(logging::getMessages, hasItem(containsString("Will resume [org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep"))); |
| 139 | + await().atMost(5, TimeUnit.SECONDS).until(logging::getMessages, hasItem(containsString("Will resume [org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep"))); |
136 | 140 | WorkflowJob p = r.jenkins.getItemByFullName("p", WorkflowJob.class);
|
137 | 141 | SemaphoreStep.success("wait/1", null);
|
138 | 142 | WorkflowRun b = p.getBuildByNumber(1);
|
@@ -160,6 +164,34 @@ public class FlowExecutionListTest {
|
160 | 164 | });
|
161 | 165 | }
|
162 | 166 |
|
| 167 | + @Test public void stepExecutionIteratorDoesNotLeakBuildsWhenOneIsStuck() throws Throwable { |
| 168 | + sessions.then(r -> { |
| 169 | + var notStuck = r.createProject(WorkflowJob.class, "not-stuck"); |
| 170 | + notStuck.setDefinition(new CpsFlowDefinition("semaphore 'wait'", true)); |
| 171 | + var notStuckBuild = notStuck.scheduleBuild2(0).waitForStart(); |
| 172 | + SemaphoreStep.waitForStart("wait/1", notStuckBuild); |
| 173 | + WeakReference<Object> notStuckBuildRef = new WeakReference<>(notStuckBuild); |
| 174 | + // Create a Pipeline that runs a long-lived task on its CpsVmExecutorService, causing it to get stuck. |
| 175 | + var stuck = r.createProject(WorkflowJob.class, "stuck"); |
| 176 | + stuck.setDefinition(new CpsFlowDefinition("blockSynchronously 'stuck'", false)); |
| 177 | + var stuckBuild = stuck.scheduleBuild2(0).waitForStart(); |
| 178 | + await().atMost(5, TimeUnit.SECONDS).until(() -> SynchronousBlockingStep.isStarted("stuck")); |
| 179 | + // Make FlowExecutionList$StepExecutionIteratorImpl.applyAll submit a task to the CpsVmExecutorService |
| 180 | + // for stuck #1 that will never complete, so the resulting future will never complete. |
| 181 | + StepExecution.applyAll(e -> null); |
| 182 | + // Let notStuckBuild complete and clean up all references. |
| 183 | + SemaphoreStep.success("wait/1", null); |
| 184 | + r.waitForCompletion(notStuckBuild); |
| 185 | + notStuckBuild = null; // Clear out the local variable in this thread. |
| 186 | + Jenkins.get().getQueue().clearLeftItems(); // Otherwise we'd have to wait 5 minutes for the cache to be cleared. |
| 187 | + // Make sure that the reference can be GC'd. |
| 188 | + MemoryAssert.assertGC(notStuckBuildRef, true); |
| 189 | + // Allow stuck #1 to complete so the test can be cleaned up promptly. |
| 190 | + SynchronousBlockingStep.unblock("stuck"); |
| 191 | + r.waitForCompletion(stuckBuild); |
| 192 | + }); |
| 193 | + } |
| 194 | + |
163 | 195 | public static class NonResumableStep extends Step implements Serializable {
|
164 | 196 | public static final long serialVersionUID = 1L;
|
165 | 197 | @DataBoundConstructor
|
@@ -198,14 +230,59 @@ public String getFunctionName() {
|
198 | 230 | }
|
199 | 231 |
|
200 | 232 | /**
|
201 |
| - * Wait up to 5 seconds for the given supplier to return a matching value. |
| 233 | + * Blocks the CPS VM thread synchronously (bad!) to test related problems. |
202 | 234 | */
|
203 |
| - private static <T> void waitFor(Supplier<T> valueSupplier, Matcher<T> matcher) throws InterruptedException { |
204 |
| - Instant end = Instant.now().plus(Duration.ofSeconds(5)); |
205 |
| - while (!matcher.matches(valueSupplier.get()) && Instant.now().isBefore(end)) { |
206 |
| - Thread.sleep(100L); |
| 235 | + public static class SynchronousBlockingStep extends Step implements Serializable { |
| 236 | + private static final long serialVersionUID = 1L; |
| 237 | + private static final Map<String, State> blocked = new HashMap<>(); |
| 238 | + private final String id; |
| 239 | + |
| 240 | + @DataBoundConstructor |
| 241 | + public SynchronousBlockingStep(String id) { |
| 242 | + this.id = id; |
| 243 | + if (blocked.put(id, State.NOT_STARTED) != null) { |
| 244 | + throw new IllegalArgumentException("Attempting to reuse ID: " + id); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public StepExecution start(StepContext context) throws Exception { |
| 250 | + return StepExecutions.synchronous(context, c -> { |
| 251 | + blocked.put(id, State.BLOCKED); |
| 252 | + c.get(TaskListener.class).getLogger().println(id + " blocked"); |
| 253 | + while (blocked.get(id) == State.BLOCKED) { |
| 254 | + Thread.sleep(100L); |
| 255 | + } |
| 256 | + c.get(TaskListener.class).getLogger().println(id + " unblocked "); |
| 257 | + return null; |
| 258 | + }); |
| 259 | + } |
| 260 | + |
| 261 | + public static boolean isStarted(String id) { |
| 262 | + var state = blocked.get(id); |
| 263 | + return state != null && state != State.NOT_STARTED; |
| 264 | + } |
| 265 | + |
| 266 | + public static void unblock(String id) { |
| 267 | + blocked.put(id, State.UNBLOCKED); |
| 268 | + } |
| 269 | + |
| 270 | + private enum State { |
| 271 | + NOT_STARTED, |
| 272 | + BLOCKED, |
| 273 | + UNBLOCKED, |
| 274 | + } |
| 275 | + |
| 276 | + @TestExtension("stepExecutionIteratorDoesNotLeakBuildsWhenOneIsStuck") public static class DescriptorImpl extends StepDescriptor { |
| 277 | + @Override |
| 278 | + public Set<? extends Class<?>> getRequiredContext() { |
| 279 | + return Collections.singleton(TaskListener.class); |
| 280 | + } |
| 281 | + @Override |
| 282 | + public String getFunctionName() { |
| 283 | + return "blockSynchronously"; |
| 284 | + } |
207 | 285 | }
|
208 |
| - assertThat("Matcher should have matched after 5s", valueSupplier.get(), matcher); |
209 | 286 | }
|
210 | 287 |
|
211 | 288 | }
|
0 commit comments