Skip to content

Commit c72455c

Browse files
committed
Fix Process resource leak in system metrics collection
Add process.waitFor() and process.destroyForcibly() in finally block to prevent zombie processes when collecting system memory and network metrics via Runtime.exec(). Also handle InterruptedException properly by restoring the interrupt flag. Affected files: - SystemMetrics: memory info collection via 'free' command - LinuxNetMetricManager: socket count collection via 'ss' command
1 parent 7e0837b commit c72455c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/LinuxNetMetricManager.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ private void updateNetStatus() {
218218

219219
if (MetricLevel.higherOrEqual(MetricLevel.NORMAL, METRIC_CONFIG.getMetricLevel())) {
220220
// update socket num
221+
Process process = null;
221222
try {
222-
Process process = Runtime.getRuntime().exec(this.getConnectNumCmd);
223+
process = Runtime.getRuntime().exec(this.getConnectNumCmd);
223224
StringBuilder result = new StringBuilder();
224225
try (BufferedReader input =
225226
new BufferedReader(new InputStreamReader(process.getInputStream()))) {
@@ -228,9 +229,17 @@ private void updateNetStatus() {
228229
result.append(line);
229230
}
230231
}
232+
process.waitFor();
231233
this.connectionNum = Integer.parseInt(result.toString().trim());
232234
} catch (IOException e) {
233235
LOGGER.error("Failed to get socket num", e);
236+
} catch (InterruptedException e) {
237+
LOGGER.error("Interrupted while waiting for socket num command", e);
238+
Thread.currentThread().interrupt();
239+
} finally {
240+
if (process != null) {
241+
process.destroyForcibly();
242+
}
234243
}
235244
}
236245
}

iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/system/SystemMetrics.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ private void updateLinuxSystemMemInfo() {
217217
long time = System.currentTimeMillis();
218218
if (time - lastUpdateTime > MetricConstant.UPDATE_INTERVAL) {
219219
lastUpdateTime = time;
220+
Process process = null;
220221
try {
221-
Process process = runtime.exec(getSystemMemoryCommand);
222+
process = runtime.exec(getSystemMemoryCommand);
222223
StringBuilder result = new StringBuilder();
223224
try (BufferedReader input =
224225
new BufferedReader(new InputStreamReader(process.getInputStream()))) {
@@ -227,6 +228,7 @@ private void updateLinuxSystemMemInfo() {
227228
result.append(line).append("\n");
228229
}
229230
}
231+
process.waitFor();
230232
String[] lines = result.toString().trim().split("\n");
231233
// if failed to get result
232234
if (lines.length >= 2) {
@@ -240,6 +242,13 @@ private void updateLinuxSystemMemInfo() {
240242
}
241243
} catch (IOException e) {
242244
logger.debug("Failed to get memory, because ", e);
245+
} catch (InterruptedException e) {
246+
logger.debug("Interrupted while waiting for memory command", e);
247+
Thread.currentThread().interrupt();
248+
} finally {
249+
if (process != null) {
250+
process.destroyForcibly();
251+
}
243252
}
244253
}
245254
}

0 commit comments

Comments
 (0)