Skip to content

Commit 06dadaa

Browse files
apuigres0nance
andauthored
fix WinRM thread leak (#1987)
see #1986 Co-authored-by: Raihaan Shouhell <[email protected]>
1 parent c0d17bb commit 06dadaa

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

src/main/java/hudson/plugins/ec2/win/EC2WindowsLauncher.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@ protected void launchScript(EC2Computer computer, TaskListener listener)
6161

6262
logger.println("Creating tmp directory if it does not exist");
6363
WindowsProcess mkdirProcess = connection.execute("if not exist " + tmpDir + " mkdir " + tmpDir);
64-
int exitCode = mkdirProcess.waitFor();
65-
if (exitCode != 0) {
66-
logger.println("Creating tmpdir failed=" + exitCode);
67-
return;
64+
try {
65+
int exitCode = mkdirProcess.waitFor();
66+
if (exitCode != 0) {
67+
logger.println("Creating tmpdir failed=" + exitCode);
68+
return;
69+
}
70+
} catch (Exception e) {
71+
mkdirProcess.destroy();
72+
throw e;
6873
}
6974

7075
if (initScript != null && !initScript.trim().isEmpty() && !connection.exists(tmpDir + ".jenkins-init")) {
@@ -74,12 +79,17 @@ protected void launchScript(EC2Computer computer, TaskListener listener)
7479
}
7580

7681
WindowsProcess initProcess = connection.execute("cmd /c " + tmpDir + "init.bat");
77-
IOUtils.copy(initProcess.getStdout(), logger);
82+
try {
83+
IOUtils.copy(initProcess.getStdout(), logger);
7884

79-
int exitStatus = initProcess.waitFor();
80-
if (exitStatus != 0) {
81-
logger.println("init script failed: exit code=" + exitStatus);
82-
return;
85+
int exitStatus = initProcess.waitFor();
86+
if (exitStatus != 0) {
87+
logger.println("init script failed: exit code=" + exitStatus);
88+
return;
89+
}
90+
} catch (Exception e) {
91+
initProcess.destroy();
92+
throw e;
8393
}
8494

8595
try (OutputStream initGuard = connection.putFile(tmpDir + ".jenkins-init")) {
@@ -102,13 +112,18 @@ protected void launchScript(EC2Computer computer, TaskListener listener)
102112
+ AGENT_JAR + " -workDir " + workDir;
103113
logger.println("Launching via WinRM:" + launchString);
104114
final WindowsProcess process = connection.execute(launchString, 86400);
105-
computer.setChannel(process.getStdout(), process.getStdin(), logger, new Listener() {
106-
@Override
107-
public void onClosed(Channel channel, IOException cause) {
108-
process.destroy();
109-
connection.close();
110-
}
111-
});
115+
try {
116+
computer.setChannel(process.getStdout(), process.getStdin(), logger, new Listener() {
117+
@Override
118+
public void onClosed(Channel channel, IOException cause) {
119+
process.destroy();
120+
connection.close();
121+
}
122+
});
123+
} catch (Exception e) {
124+
process.destroy();
125+
throw e;
126+
}
112127
} catch (EOFException eof) {
113128
// When we launch java with connection.execute(launchString... it keeps running, but if java is not
114129
// installed

src/main/java/hudson/plugins/ec2/win/winrm/WindowsProcess.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,19 @@ public synchronized void destroy() {
8686
return;
8787
}
8888

89-
client.signal();
90-
client.deleteShell();
89+
// Instance may already be terminated, causing WinRM operations to fail
90+
try {
91+
client.signal();
92+
} catch (Exception e) {
93+
LOGGER.log(Level.FINE, () -> "Failed to signal WinRM shell: " + e.getMessage());
94+
}
95+
96+
try {
97+
client.deleteShell();
98+
} catch (Exception e) {
99+
LOGGER.log(Level.FINE, () -> "Failed to delete WinRM shell: " + e.getMessage());
100+
}
101+
91102
terminated = true;
92103
Closeables.closeQuietly(toCallersStdout);
93104
Closeables.closeQuietly(toCallersStdin);

0 commit comments

Comments
 (0)