Skip to content

Commit bfee7a6

Browse files
authored
[fix][client] Fix some potential resource leak (apache#24402)
Fix resource leak using try-catch stmt method in apache#24389, and close other leaks in this project I've found later
1 parent 7d54f40 commit bfee7a6

File tree

5 files changed

+24
-26
lines changed

5 files changed

+24
-26
lines changed

pulsar-client/src/main/java/org/apache/pulsar/client/impl/AutoClusterFailover.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ boolean probeAvailable(String url) {
128128
try {
129129
resolver.updateServiceUrl(url);
130130
InetSocketAddress endpoint = resolver.resolveHost();
131-
Socket socket = new Socket();
132-
socket.connect(new InetSocketAddress(endpoint.getHostName(), endpoint.getPort()), TIMEOUT);
133-
socket.close();
131+
try (Socket socket = new Socket()) {
132+
socket.connect(new InetSocketAddress(endpoint.getHostName(), endpoint.getPort()), TIMEOUT);
133+
}
134134
return true;
135135
} catch (Exception e) {
136136
log.warn("Failed to probe available, url: {}", url, e);

pulsar-functions/runtime/src/main/java/org/apache/pulsar/functions/runtime/RuntimeUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -540,17 +540,17 @@ public static String genFunctionLogFolder(String logDirectory, InstanceConfig in
540540
}
541541

542542
public static String getPrometheusMetrics(int metricsPort) throws IOException {
543-
StringBuilder result = new StringBuilder();
544543
URL url = new URL(String.format("http://%s:%s", InetAddress.getLocalHost().getHostAddress(), metricsPort));
545544
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
546545
conn.setRequestMethod("GET");
547-
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
548-
String line;
549-
while ((line = rd.readLine()) != null) {
550-
result.append(line + System.lineSeparator());
546+
try (BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
547+
StringBuilder result = new StringBuilder();
548+
String line;
549+
while ((line = rd.readLine()) != null) {
550+
result.append(line + System.lineSeparator());
551+
}
552+
return result.toString();
551553
}
552-
rd.close();
553-
return result.toString();
554554
}
555555

556556
/**

pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/FunctionActioner.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,11 @@ private void downloadFile(File pkgFile, boolean isPkgUrlProvided, FunctionMetaDa
266266
} else if (downloadFromPackageManagementService) {
267267
getPulsarAdmin().packages().download(pkgLocationPath, tempPkgFile.getPath());
268268
} else {
269-
FileOutputStream tempPkgFos = new FileOutputStream(tempPkgFile);
270-
WorkerUtils.downloadFromBookkeeper(
271-
dlogNamespace,
272-
tempPkgFos,
273-
pkgLocationPath);
274-
if (tempPkgFos != null) {
275-
tempPkgFos.close();
269+
try (FileOutputStream tempPkgFos = new FileOutputStream(tempPkgFile)) {
270+
WorkerUtils.downloadFromBookkeeper(
271+
dlogNamespace,
272+
tempPkgFos,
273+
pkgLocationPath);
276274
}
277275
}
278276

pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/ComponentImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,8 +1283,9 @@ public void uploadFunction(final InputStream uploadedInputStream, final String p
12831283
if (worker().getWorkerConfig().isFunctionsWorkerEnablePackageManagement()) {
12841284
File tempFile = createPkgTempFile();
12851285
tempFile.deleteOnExit();
1286-
FileOutputStream out = new FileOutputStream(tempFile);
1287-
IOUtils.copy(uploadedInputStream, out);
1286+
try (FileOutputStream out = new FileOutputStream(tempFile)) {
1287+
IOUtils.copy(uploadedInputStream, out);
1288+
}
12881289
PackageMetadata metadata = PackageMetadata.builder().createTime(System.currentTimeMillis()).build();
12891290
worker().getBrokerAdmin().packages().upload(metadata, path, tempFile.getAbsolutePath());
12901291
} else {

pulsar-testclient/src/main/java/org/apache/pulsar/testclient/LoadSimulationController.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -638,14 +638,13 @@ private void read(final String[] args) {
638638
final List<String> commandArguments = arguments.commandArguments;
639639
checkAppArgs(commandArguments.size() - 1, 1);
640640
final String scriptName = commandArguments.get(1);
641-
final BufferedReader scriptReader = new BufferedReader(
642-
new InputStreamReader(new FileInputStream(Paths.get(scriptName).toFile())));
643-
String line = scriptReader.readLine();
644-
while (line != null) {
645-
read(line.split("\\s+"));
646-
line = scriptReader.readLine();
641+
try (BufferedReader scriptReader = new BufferedReader(
642+
new InputStreamReader(new FileInputStream(Paths.get(scriptName).toFile())))) {
643+
String line;
644+
while ((line = scriptReader.readLine()) != null) {
645+
read(line.split("\\s+"));
646+
}
647647
}
648-
scriptReader.close();
649648
break;
650649
case "copy":
651650
handleCopy(arguments);

0 commit comments

Comments
 (0)