Skip to content

Commit

Permalink
[FIXED JENKINS-26379][FIXED JENKINS-41163][JENKINS-34100] Do not swal…
Browse files Browse the repository at this point in the history
…low IOException in case it is not recoverable.
  • Loading branch information
olivergondza committed Jan 19, 2017
1 parent 1df5326 commit 44d7e2f
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/main/java/hudson/plugins/sshslaves/SSHLauncher.java
Expand Up @@ -147,6 +147,16 @@ public class SSHLauncher extends ComputerLauncher {
public static final String JDKVERSION = "jdk-7u80";
public static final String DEFAULT_JDK = JDKVERSION + "-oth-JPR";

// Some of the messages observed in the wild:
// "Connection refused (Connection refused)"
// "Connection reset"
// "Connection timed out", "Connection timed out (Connection timed out)"
// "No route to host", "No route to host (Host unreachable)"
// "Premature connection close"
private static final List<String> RECOVERABLE_FAILURES = Arrays.asList(
"Connection refused", "Connection reset", "Connection timed out", "No route to host", "Premature connection close"
);

/**
* @deprecated
* Subtype of {@link JDKInstaller} causes JENKINS-10641.
Expand Down Expand Up @@ -1177,7 +1187,8 @@ protected String checkJavaVersion(final PrintStream logger, String javaCommand,
}

protected void openConnection(TaskListener listener) throws IOException, InterruptedException {
listener.getLogger().println(Messages.SSHLauncher_OpeningSSHConnection(getTimestamp(), host + ":" + port));
PrintStream logger = listener.getLogger();
logger.println(Messages.SSHLauncher_OpeningSSHConnection(getTimestamp(), host + ":" + port));
connection.setTCPNoDelay(true);

int maxNumRetries = this.maxNumRetries == null || this.maxNumRetries < 0 ? 0 : this.maxNumRetries;
Expand All @@ -1187,20 +1198,21 @@ protected void openConnection(TaskListener listener) throws IOException, Interru
connection.connect();
break;
} catch (IOException ioexception) {
listener.getLogger().println(ioexception.getCause().getMessage());
String ioExceptionMessageCause = "";
if (ioexception.getCause() != null) {
ioExceptionMessageCause = ioexception.getCause().getMessage();
String message = "";
Throwable cause = ioexception.getCause();
if (cause != null) {
message = cause.getMessage();
logger.println(message);
}
if (!ioExceptionMessageCause.equals("Connection refused")) {
break;
if (cause == null || !isRecoverable(message)) {
throw ioexception;
}
if (maxNumRetries - i > 0) {
listener.getLogger().println("SSH Connection failed with IOException: \"" + ioExceptionMessageCause
logger.println("SSH Connection failed with IOException: \"" + message
+ "\", retrying in " + retryWaitTime + " seconds. There are "
+ (maxNumRetries - i) + " more retries left.");
} else {
listener.getLogger().println("SSH Connection failed with IOException: \"" + ioExceptionMessageCause + "\".");
logger.println("SSH Connection failed with IOException: \"" + message + "\".");
throw ioexception;
}
}
Expand All @@ -1213,13 +1225,20 @@ protected void openConnection(TaskListener listener) throws IOException, Interru
}
if (SSHAuthenticator.newInstance(connection, credentials).authenticate(listener)
&& connection.isAuthenticationComplete()) {
listener.getLogger().println(Messages.SSHLauncher_AuthenticationSuccessful(getTimestamp()));
logger.println(Messages.SSHLauncher_AuthenticationSuccessful(getTimestamp()));
} else {
listener.getLogger().println(Messages.SSHLauncher_AuthenticationFailed(getTimestamp()));
logger.println(Messages.SSHLauncher_AuthenticationFailed(getTimestamp()));
throw new AbortException(Messages.SSHLauncher_AuthenticationFailedException());
}
}

private boolean isRecoverable(String message) {
for (String s : RECOVERABLE_FAILURES) {
if (message.startsWith(s)) return true;
}
return false;
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 44d7e2f

Please sign in to comment.