-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix Process resource leak in system metrics collection #17212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,8 +218,9 @@ private void updateNetStatus() { | |
|
|
||
| if (MetricLevel.higherOrEqual(MetricLevel.NORMAL, METRIC_CONFIG.getMetricLevel())) { | ||
| // update socket num | ||
| Process process = null; | ||
| try { | ||
| Process process = Runtime.getRuntime().exec(this.getConnectNumCmd); | ||
| process = Runtime.getRuntime().exec(this.getConnectNumCmd); | ||
| StringBuilder result = new StringBuilder(); | ||
| try (BufferedReader input = | ||
| new BufferedReader(new InputStreamReader(process.getInputStream()))) { | ||
|
|
@@ -228,9 +229,17 @@ private void updateNetStatus() { | |
| result.append(line); | ||
| } | ||
| } | ||
| process.waitFor(); | ||
| this.connectionNum = Integer.parseInt(result.toString().trim()); | ||
| } catch (IOException e) { | ||
| LOGGER.error("Failed to get socket num", e); | ||
| } catch (InterruptedException e) { | ||
| LOGGER.error("Interrupted while waiting for socket num command", e); | ||
| Thread.currentThread().interrupt(); | ||
| } finally { | ||
| if (process != null) { | ||
| process.destroyForcibly(); | ||
| } | ||
|
Comment on lines
+239
to
+242
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -217,8 +217,9 @@ private void updateLinuxSystemMemInfo() { | |||||
| long time = System.currentTimeMillis(); | ||||||
| if (time - lastUpdateTime > MetricConstant.UPDATE_INTERVAL) { | ||||||
| lastUpdateTime = time; | ||||||
| Process process = null; | ||||||
| try { | ||||||
| Process process = runtime.exec(getSystemMemoryCommand); | ||||||
| process = runtime.exec(getSystemMemoryCommand); | ||||||
| StringBuilder result = new StringBuilder(); | ||||||
| try (BufferedReader input = | ||||||
| new BufferedReader(new InputStreamReader(process.getInputStream()))) { | ||||||
|
|
@@ -227,6 +228,7 @@ private void updateLinuxSystemMemInfo() { | |||||
| result.append(line).append("\n"); | ||||||
| } | ||||||
| } | ||||||
| process.waitFor(); | ||||||
| String[] lines = result.toString().trim().split("\n"); | ||||||
| // if failed to get result | ||||||
| if (lines.length >= 2) { | ||||||
|
|
@@ -240,6 +242,13 @@ private void updateLinuxSystemMemInfo() { | |||||
| } | ||||||
| } catch (IOException e) { | ||||||
| logger.debug("Failed to get memory, because ", e); | ||||||
| } catch (InterruptedException e) { | ||||||
| logger.debug("Interrupted while waiting for memory command", e); | ||||||
| Thread.currentThread().interrupt(); | ||||||
| } finally { | ||||||
| if (process != null) { | ||||||
|
||||||
| if (process != null) { | |
| if (process != null && process.isAlive()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Integer.parseInt() call can throw NumberFormatException if the command output is not a valid integer, but this exception is not caught. If the external command returns unexpected output, this will cause an unhandled exception that could disrupt metrics collection. Consider adding a catch block for NumberFormatException to handle malformed output gracefully, similar to how IOException is handled.