Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-46680] Disconnect computer on ping timeout (#3005)
* [JENKINS-46680] Reproduce in unittest * [FIX JENKINS-46680] Reset SlaveComputer channel before closing it on ping timeout * [JENKINS-46680] Attach channel termination offline cause on ping timeouts (cherry picked from commit dbb5e44)
- Loading branch information
1 parent
8ec2510
commit 06b0cd6
Showing
3 changed files
with
141 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) Red Hat, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package hudson.slaves; | ||
|
||
import hudson.Functions; | ||
import hudson.model.Computer; | ||
import hudson.remoting.Channel; | ||
import hudson.remoting.ChannelClosedException; | ||
import hudson.remoting.PingThread; | ||
import jenkins.security.MasterToSlaveCallable; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import java.io.IOException; | ||
import java.lang.management.ManagementFactory; | ||
import java.lang.reflect.Method; | ||
import java.util.Date; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
import static org.junit.Assume.assumeFalse; | ||
|
||
/** | ||
* @author ogondza. | ||
*/ | ||
public class PingThreadTest { | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
public void failedPingThreadResetsComputerChannel() throws Exception { | ||
assumeFalse("We simulate hung agent by sending the SIGTSTP signal", Functions.isWindows()); | ||
|
||
DumbSlave slave = j.createOnlineSlave(); | ||
Computer computer = slave.toComputer(); | ||
Channel channel = (Channel) slave.getChannel(); | ||
String pid = channel.call(new GetPid()); | ||
|
||
PingThread pingThread = null; | ||
for (Thread it: Thread.getAllStackTraces().keySet()) { | ||
if (it instanceof PingThread && it.getName().endsWith(channel.toString())) { | ||
pingThread = (PingThread) it; | ||
} | ||
} | ||
assertNotNull(pingThread); | ||
|
||
// Simulate lost connection | ||
assert new ProcessBuilder("kill", "-TSTP", pid).start().waitFor() == 0; | ||
try { | ||
// ... do not wait for Ping Thread to notice | ||
Method onDead = PingThread.class.getDeclaredMethod("onDead", Throwable.class); | ||
onDead.setAccessible(true); | ||
onDead.invoke(pingThread, new TimeoutException("No ping")); | ||
|
||
try { | ||
channel.call(new GetPid()); | ||
fail(); | ||
} catch (ChannelClosedException ex) { | ||
// Expected | ||
} | ||
|
||
assertNull(slave.getComputer().getChannel()); | ||
assertNull(computer.getChannel()); | ||
} finally { | ||
assert new ProcessBuilder("kill", "-CONT", pid).start().waitFor() == 0; | ||
} | ||
} | ||
|
||
private static final class GetPid extends MasterToSlaveCallable<String, IOException> { | ||
@Override public String call() throws IOException { | ||
return ManagementFactory.getRuntimeMXBean().getName().replaceAll("@.*", ""); | ||
} | ||
} | ||
} |