Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-48613] - Hotfix: Prevent piling up of tearDownConnection() c…
…alls if they take long (#78)
  • Loading branch information
oleg-nenashev committed Dec 18, 2017
1 parent 1ff0fb7 commit 5eea3a0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/hudson/plugins/sshslaves/SSHLauncher.java
Expand Up @@ -125,6 +125,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -241,6 +242,12 @@ public Object readResolve() {
*/
private transient volatile Connection connection;

/**
* Indicates that the {@link #tearDownConnection(SlaveComputer, TaskListener)} is in progress.
* It is used in {@link #afterDisconnect(SlaveComputer, TaskListener)} to avoid multiple parallel calls.
*/
private transient volatile boolean tearingDownConnection;

/**
* The session inside {@link #connection} that controls the slave process.
*/
Expand Down Expand Up @@ -1372,11 +1379,24 @@ public void afterDisconnect(SlaveComputer slaveComputer, final TaskListener list
srv.shutdown();
}

if (tearingDownConnection) {
// tear down operation is in progress, do not even try to synchronize the call.
//TODO: what if reconnect attempts collide? It should not be possible due to locks, but maybe it worth investigation
LOGGER.log(Level.FINE, "There is already a tear down operation in progress for connection {0}. Skipping the call", connection);
return;
}
tearDownConnection(slaveComputer, listener);
}

private synchronized void tearDownConnection(@Nonnull SlaveComputer slaveComputer, final @Nonnull TaskListener listener) {
if (connection != null) {
tearDownConnectionImpl(slaveComputer, listener);
}
}

private void tearDownConnectionImpl(@Nonnull SlaveComputer slaveComputer, final @Nonnull TaskListener listener) {
try {
tearingDownConnection = true;
boolean connectionLost = reportTransportLoss(connection, listener);
if (session!=null) {
// give the process 3 seconds to write out its dying message before we cut the loss
Expand Down Expand Up @@ -1447,6 +1467,8 @@ public void run() {

PluginImpl.unregister(connection);
cleanupConnection(listener);
} finally {
tearingDownConnection = false;
}
}

Expand Down

0 comments on commit 5eea3a0

Please sign in to comment.