Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timeout for pickle resolution to prevent indefinitely stuck builds #278

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,38 @@

package org.jenkinsci.plugins.workflow.support.pickles.serialization;

import org.jenkinsci.plugins.workflow.pickles.Pickle;
import org.jenkinsci.plugins.workflow.support.concurrent.Futures;
import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.jboss.marshalling.ObjectResolver;

import com.google.common.util.concurrent.MoreExecutors;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import jenkins.util.SystemProperties;
import jenkins.util.Timer;
import org.jboss.marshalling.ObjectResolver;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.jenkinsci.plugins.workflow.pickles.Pickle;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* {@link ObjectResolver} that resolves {@link DryCapsule} to unpickled objects.
*
* @author Kohsuke Kawaguchi
*/
public class PickleResolver implements ObjectResolver {

/**
* Pickle resolution will fail automatically after this many seconds.
* <p>This is intended to prevent Pipeline builds from hanging forever in unusual cases.
*/
@Restricted(NoExternalUse.class)
@SuppressFBWarnings(value = "MS_SHOULD_BE_FINAL", justification = "Non-final for modification from script console")
public static long RESOLUTION_TIMEOUT_SECONDS = SystemProperties.getLong(PickleResolver.class + ".RESOLUTION_TIMEOUT_SECONDS", TimeUnit.HOURS.toSeconds(1));
Copy link
Member Author

@dwnusbaum dwnusbaum Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what value is best here. One hour resolving pickles seemed roughly ok to me. I think we don't really expect TryRepeatedly to be used normally anyway now that ExecutorStepDynamicContext and FilePathDynamicContext exist, but there are a couple of other usages.


/**
* Persisted forms of the stateful objects.
*/
Expand Down Expand Up @@ -80,20 +95,20 @@

List<ListenableFuture<?>> members = new ArrayList<>();
for (Pickle r : pickles) {
// TODO log("rehydrating " + r);

Check warning on line 98 in src/main/java/org/jenkinsci/plugins/workflow/support/pickles/serialization/PickleResolver.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: log("rehydrating " + r);
ListenableFuture<?> future;
try {
future = r.rehydrate(owner);
future = Futures.withTimeout(r.rehydrate(owner), RESOLUTION_TIMEOUT_SECONDS, TimeUnit.SECONDS, Timer.get());
} catch (RuntimeException x) {
future = Futures.immediateFailedFuture(x);
}
pickleFutures.add(future);
members.add(Futures.transform(future, new Function<Object,Object>() {
@Override public Object apply(Object input) {
// TODO log("rehydrated to " + input);

Check warning on line 108 in src/main/java/org/jenkinsci/plugins/workflow/support/pickles/serialization/PickleResolver.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: log("rehydrated to " + input);
return input;
}
}));
}, MoreExecutors.directExecutor()));
}

ListenableFuture<List<Object>> all = Futures.allAsList(members);
Expand All @@ -104,7 +119,7 @@
values = input;
return PickleResolver.this;
}
});
}, MoreExecutors.directExecutor());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.jenkinsci.plugins.workflow.support.pickles.serialization;

import com.google.common.util.concurrent.ListenableFuture;
import hudson.model.Result;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.pickles.Pickle;
import org.jenkinsci.plugins.workflow.support.pickles.SingleTypedPickleFactory;
import org.jenkinsci.plugins.workflow.support.pickles.TryRepeatedly;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.FlagRule;
import org.jvnet.hudson.test.JenkinsSessionRule;
import org.jvnet.hudson.test.TestExtension;

public class PickleResolverTest {
@ClassRule
public static BuildWatcher watcher = new BuildWatcher();

@Rule
public JenkinsSessionRule sessions = new JenkinsSessionRule();

@Rule
public FlagRule<Long> resetPickleResolutionTimeout = new FlagRule<>(() -> PickleResolver.RESOLUTION_TIMEOUT_SECONDS, x -> PickleResolver.RESOLUTION_TIMEOUT_SECONDS = x);

@Test
public void timeout() throws Throwable {
sessions.then(r -> {
var p = r.jenkins.createProject(WorkflowJob.class, "stuckPickle");
p.setDefinition(new CpsFlowDefinition(
"def x = new org.jenkinsci.plugins.workflow.support.pickles.serialization.PickleResolverTest.StuckPickle.Marker()\n" +
"semaphore 'wait'\n" +
"echo x.getClass().getName()", false));
var b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("wait/1", b);
});
PickleResolver.RESOLUTION_TIMEOUT_SECONDS = 3;
sessions.then(r -> {
var p = r.jenkins.getItemByFullName("stuckPickle", WorkflowJob.class);
var b = p.getBuildByNumber(1);
r.assertBuildStatus(Result.FAILURE, r.waitForCompletion(b));
r.assertLogContains("Timed out: StuckPickle", b);
});
}

public static class StuckPickle extends Pickle {
@Override
public ListenableFuture<Marker> rehydrate(FlowExecutionOwner owner) {
return new TryRepeatedly<Marker>(1) {
@Override
protected Marker tryResolve() {
return null;
}
@Override protected FlowExecutionOwner getOwner() {
return owner;
}
@Override public String toString() {
return "StuckPickle for " + owner;
}
};
}

public static class Marker {}

@TestExtension("timeout")
public static final class Factory extends SingleTypedPickleFactory<Marker> {
@Override protected Pickle pickle(Marker object) {
return new StuckPickle();
}
}
}
}
Loading