From 7ea9d7ae1d57b6f38456ddd6b92873f651bf317b Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Tue, 9 Jun 2020 16:44:50 -0500 Subject: [PATCH] Fix leak of launcher on disconnect (#117) - afterDisconnect is called asynchronously from the deletion of the Node. Ensure that we continue to process in the event that `slaveComputer.getNode()` returns null, as we still have to ensure that the launcher is torn down. Without this, SSHLauncher could sometimes leak sockets. --- .../plugins/vSphereCloudLauncher.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/vSphereCloudLauncher.java b/src/main/java/org/jenkinsci/plugins/vSphereCloudLauncher.java index 1d67f993..d0487322 100644 --- a/src/main/java/org/jenkinsci/plugins/vSphereCloudLauncher.java +++ b/src/main/java/org/jenkinsci/plugins/vSphereCloudLauncher.java @@ -282,18 +282,20 @@ public void launch(SlaveComputer slaveComputer, TaskListener taskListener) public synchronized void afterDisconnect(SlaveComputer slaveComputer, TaskListener taskListener) { final vSphereCloudSlave vsSlave = (vSphereCloudSlave) slaveComputer.getNode(); - if(vsSlave == null) { - vSphereCloud.Log(slaveComputer, taskListener, "Slave is null."); - return; - } - if (vsSlave.slaveIsStarting == Boolean.TRUE) { - vSphereCloud.Log(slaveComputer, taskListener, "Ignoring disconnect attempt because a connect attempt is in progress."); - return; - } - if (vsSlave.slaveIsDisconnecting == Boolean.TRUE) { - vSphereCloud.Log(slaveComputer, taskListener, "Already disconnecting on a separate thread"); - return; + if (vsSlave != null) { + if (vsSlave.slaveIsStarting == Boolean.TRUE) { + vSphereCloud.Log(slaveComputer, taskListener, "Ignoring disconnect attempt because a connect attempt is in progress."); + return; + } + if (vsSlave.slaveIsDisconnecting == Boolean.TRUE) { + vSphereCloud.Log(slaveComputer, taskListener, "Already disconnecting on a separate thread"); + return; + } + vsSlave.slaveIsDisconnecting = Boolean.TRUE; + } else { + vSphereCloud.Log(slaveComputer, taskListener, "Slave is null. Will still attempt to tear down launcher."); } + if (slaveComputer.isTemporarilyOffline()) { if (!(slaveComputer.getOfflineCause() instanceof VSphereOfflineCause)) { vSphereCloud.Log(slaveComputer, taskListener, "Not disconnecting VM because it's not accepting tasks"); @@ -301,7 +303,6 @@ public synchronized void afterDisconnect(SlaveComputer slaveComputer, TaskListen } } - vsSlave.slaveIsDisconnecting = Boolean.TRUE; VSphere v = null; boolean reconnect = false; try { @@ -380,8 +381,10 @@ public synchronized void afterDisconnect(SlaveComputer slaveComputer, TaskListen v.disconnect(); v = null; } - vsSlave.slaveIsDisconnecting = Boolean.FALSE; - vsSlave.slaveIsStarting = Boolean.FALSE; + if (vsSlave != null) { + vsSlave.slaveIsDisconnecting = Boolean.FALSE; + vsSlave.slaveIsStarting = Boolean.FALSE; + } if (reconnect) { slaveComputer.connect(false);