Skip to content

Commit

Permalink
Merge pull request #478 from Vlatombe/close-session-on-exception
Browse files Browse the repository at this point in the history
If an exception occurs while launching an agent, the underlying connection and session should be closed
  • Loading branch information
basil authored Oct 18, 2024
2 parents 6fe3406 + ae479e1 commit c9f7ca9
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,11 @@ private void launch(ComputeEngineComputer computer, TaskListener listener) {
return;
}

final Connection conn;
Connection conn = null;
Optional<Connection> cleanupConn;
PrintStream logger = listener.getLogger();
logInfo(computer, listener, "Launching instance: " + node.getNodeName());
Session sess = null;
try {
cleanupConn = setupConnection(node, computer, listener);
if (!cleanupConn.isPresent()) {
Expand All @@ -298,16 +299,24 @@ private void launch(ComputeEngineComputer computer, TaskListener listener) {
copyAgentJar(computer, conn, listener, jenkinsDir);
String launchString = getJavaLaunchString(javaExecPath, jenkinsDir);
logInfo(computer, listener, "Launching Jenkins agent via plugin SSH: " + launchString);
final Session sess = conn.openSession();
sess = conn.openSession();
sess.execCommand(launchString);
Session finalSess = sess;
Connection finalConn = conn;
computer.setChannel(sess.getStdout(), sess.getStdin(), logger, new Channel.Listener() {
@Override
public void onClosed(Channel channel, IOException cause) {
sess.close();
conn.close();
finalSess.close();
finalConn.close();
}
});
} catch (Exception e) {
if (sess != null) {
sess.close();
}
if (conn != null) {
conn.close();

Check warning on line 318 in src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineComputerLauncher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 283-318 are not covered by tests
}
logException(computer, listener, "Error: ", e);
}
}
Expand Down

0 comments on commit c9f7ca9

Please sign in to comment.