Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()))) {
Expand All @@ -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);
Copy link

Copilot AI Feb 24, 2026

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.

Suggested change
LOGGER.error("Failed to get socket num", e);
LOGGER.error("Failed to get socket num", e);
} catch (NumberFormatException e) {
LOGGER.error(
"Failed to parse socket num from command output: '{}'",
e.getMessage());

Copilot uses AI. Check for mistakes.
} 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
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling destroyForcibly() unconditionally in the finally block can forcibly terminate a process that has already completed successfully via waitFor(). This is problematic because:

  1. After waitFor() returns successfully, the process has already exited normally, and destroyForcibly() should not be needed
  2. destroyForcibly() may interfere with proper cleanup of process resources on some platforms
  3. This pattern goes against Java best practices for process management

The fix should only call destroyForcibly() when the process hasn't completed normally (i.e., when an exception occurs before or during waitFor()). Consider adding a boolean flag to track whether waitFor() completed successfully, or only destroy the process in the catch blocks.

Copilot uses AI. Check for mistakes.
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))) {
Expand All @@ -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) {
Expand All @@ -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) {
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling destroyForcibly() unconditionally in the finally block can forcibly terminate a process that has already completed successfully via waitFor(). This is problematic because:

  1. After waitFor() returns successfully, the process has already exited normally, and destroyForcibly() should not be needed
  2. destroyForcibly() may interfere with proper cleanup of process resources on some platforms
  3. This pattern goes against Java best practices for process management

The fix should only call destroyForcibly() when the process hasn't completed normally (i.e., when an exception occurs before or during waitFor()). Consider adding a boolean flag to track whether waitFor() completed successfully, or only destroy the process in the catch blocks.

Suggested change
if (process != null) {
if (process != null && process.isAlive()) {

Copilot uses AI. Check for mistakes.
process.destroyForcibly();
}
}
}
}
Expand Down