Skip to content

Commit 9be854c

Browse files
authored
Fix Process resource leak in system metrics collection (#17212)
1 parent 402abdd commit 9be854c

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

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

Lines changed: 12 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,19 @@ 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+
} catch (NumberFormatException e) {
240+
LOGGER.error("Failed to parse socket num from command output: '{}'", e.getMessage());
241+
} finally {
242+
if (process != null && process.isAlive()) {
243+
process.destroyForcibly();
244+
}
234245
}
235246
}
236247
}

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 && process.isAlive()) {
250+
process.destroyForcibly();
251+
}
243252
}
244253
}
245254
}

0 commit comments

Comments
 (0)