提交 c64e4d97 编写于 作者: J Jesse Glick

Permit RunnerStack.peek to return null in case it is not in use (for example,...

Permit RunnerStack.peek to return null in case it is not in use (for example, from a workflow), so that we do not get an NPE when trying to run a task that asks for checkpoints.
https://trello.com/c/DoCMV6gB/31-runnerstack
上级 dd914688
......@@ -1518,7 +1518,11 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
* @see CheckPoint#report()
*/
/*package*/ static void reportCheckpoint(@Nonnull CheckPoint id) {
RunnerStack.INSTANCE.peek().checkpoints.report(id);
Run<?,?>.RunExecution exec = RunnerStack.INSTANCE.peek();
if (exec == null) {
return;
}
exec.checkpoints.report(id);
}
/**
......@@ -1526,7 +1530,11 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
*/
/*package*/ static void waitForCheckpoint(@Nonnull CheckPoint id, @CheckForNull BuildListener listener, @CheckForNull String waiter) throws InterruptedException {
while(true) {
Run b = RunnerStack.INSTANCE.peek().getBuild().getPreviousBuildInProgress();
Run<?,?>.RunExecution exec = RunnerStack.INSTANCE.peek();
if (exec == null) {
return;
}
Run b = exec.getBuild().getPreviousBuildInProgress();
if(b==null) return; // no pending earlier build
Run.RunExecution runner = b.runner;
if(runner==null) {
......
......@@ -28,6 +28,7 @@ import hudson.model.Run.RunExecution;
import java.util.Stack;
import java.util.Map;
import java.util.WeakHashMap;
import javax.annotation.CheckForNull;
/**
* Keeps track of {@link RunExecution}s that are currently executing on the given thread
......@@ -53,8 +54,19 @@ final class RunnerStack {
if(s.isEmpty()) stack.remove(e);
}
synchronized RunExecution peek() {
return stack.get(Executor.currentExecutor()).peek();
/**
* Looks up the currently running build, if known.
* @return a running build, or null if one has not been recorded
*/
synchronized @CheckForNull RunExecution peek() {
Executor e = Executor.currentExecutor();
if (e != null) {
Stack<RunExecution> s = stack.get(e);
if (s != null && !s.isEmpty()) {
return s.peek();
}
}
return null;
}
static final RunnerStack INSTANCE = new RunnerStack();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册