Skip to content

Commit

Permalink
[JENKINS-62006] Move getChannelToMaster to a separate class
Browse files Browse the repository at this point in the history
This should avoid classloading issues I've seen, such as

Caused by: java.lang.NoClassDefFoundError:
javax/servlet/ServletException
  at hudson.util.ProcessTree.get(ProcessTree.java:432)
...
  • Loading branch information
Vlatombe committed Apr 22, 2020
1 parent 3b85c0a commit 85974f3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
12 changes: 3 additions & 9 deletions core/src/main/java/hudson/slaves/SlaveComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1067,17 +1067,11 @@ public synchronized void publish(LogRecord record) {
*
* @return null if the calling thread doesn't have any trace of where its master is.
* @since 1.362
* @deprecated Use {@link SlaveComputerUtil#getChannelToMaster()} instead.
*/
@Deprecated
public static VirtualChannel getChannelToMaster() {
if (Jenkins.getInstanceOrNull()!=null) // check if calling thread is on master or on slave
return FilePath.localChannel;

// if this method is called from within the agent computation thread, this should work
Channel c = Channel.current();
if (c!=null && Boolean.TRUE.equals(c.getProperty("slave")))
return c;

return null;
return SlaveComputerUtil.getChannelToMaster();
}

/**
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/java/hudson/slaves/SlaveComputerUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hudson.slaves;

import hudson.FilePath;
import hudson.remoting.Channel;
import hudson.remoting.VirtualChannel;
import jenkins.model.Jenkins;

public final class SlaveComputerUtil {
private SlaveComputerUtil() {}
/**
* Obtains a {@link VirtualChannel} that allows some computation to be performed on the master.
* This method can be called from any thread on the master, or from agent (more precisely,
* it only works from the remoting request-handling thread in agents, which means if you've started
* separate thread on agents, that'll fail.)
*
* @return null if the calling thread doesn't have any trace of where its master is.
* @since XXX
*/
public static VirtualChannel getChannelToMaster() {
if (Jenkins.getInstanceOrNull()!=null) // check if calling thread is on master or on slave
return FilePath.localChannel;

// if this method is called from within the agent computation thread, this should work
Channel c = Channel.current();
if (c!=null && Boolean.TRUE.equals(c.getProperty("slave")))
return c;

return null;
}
}
8 changes: 4 additions & 4 deletions core/src/main/java/hudson/util/ProcessTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import hudson.Util;
import hudson.remoting.Channel;
import hudson.remoting.VirtualChannel;
import hudson.slaves.SlaveComputer;
import hudson.slaves.SlaveComputerUtil;
import hudson.util.ProcessKillingVeto.VetoCause;
import hudson.util.ProcessTree.OSProcess;
import hudson.util.ProcessTreeRemoting.IOSProcess;
Expand Down Expand Up @@ -193,7 +193,7 @@ public void killAll(@CheckForNull Process proc, @CheckForNull Map<String, String
/*package*/ final List<ProcessKiller> getKillers() throws InterruptedException {
if (killers==null)
try {
VirtualChannel channelToMaster = SlaveComputer.getChannelToMaster();
VirtualChannel channelToMaster = SlaveComputerUtil.getChannelToMaster();
if (channelToMaster!=null) {
killers = channelToMaster.call(new ListAll());
} else {
Expand Down Expand Up @@ -288,7 +288,7 @@ void killByKiller() throws InterruptedException {
// Quick check, does anything exist to check against
if (!skipVetoes) {
try {
VirtualChannel channelToMaster = SlaveComputer.getChannelToMaster();
VirtualChannel channelToMaster = SlaveComputerUtil.getChannelToMaster();
if (channelToMaster!=null) {
CheckVetoes vetoCheck = new CheckVetoes(this);
causeMessage = channelToMaster.call(vetoCheck);
Expand Down Expand Up @@ -429,7 +429,7 @@ public static ProcessTree get() {
// Check for the existance of vetoers if I don't know already
if (vetoersExist == null) {
try {
VirtualChannel channelToMaster = SlaveComputer.getChannelToMaster();
VirtualChannel channelToMaster = SlaveComputerUtil.getChannelToMaster();
if (channelToMaster != null) {
vetoersExist = channelToMaster.call(new DoVetoersExist());
}
Expand Down

0 comments on commit 85974f3

Please sign in to comment.