-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[ZEPPELIN-6268] Fix resource leaks and add null checks for getResourceAsStream #5010
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 |
---|---|---|
|
@@ -134,6 +134,9 @@ private void setupJVMGateway(String gatewayHost, int gatewayPort) throws IOExcep | |
private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws IOException { | ||
InputStream input = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, closing the input stream is missing; this can be done directly with try-with-resource. |
||
getClass().getClassLoader().getResourceAsStream("python/zeppelin_ipython.py"); | ||
if (input == null) { | ||
throw new IOException("Cannot find resource: python/zeppelin_ipython.py"); | ||
} | ||
List<String> lines = IOUtils.readLines(input, StandardCharsets.UTF_8); | ||
ExecuteResponse response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder() | ||
.setCode(StringUtils.join(lines, System.lineSeparator()) | ||
|
@@ -145,6 +148,9 @@ private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws I | |
|
||
input = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, closing the input stream is missing; this can be done directly with try-with-resource. |
||
getClass().getClassLoader().getResourceAsStream("python/zeppelin_context.py"); | ||
if (input == null) { | ||
throw new IOException("Cannot find resource: python/zeppelin_context.py"); | ||
} | ||
lines = IOUtils.readLines(input, StandardCharsets.UTF_8); | ||
response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder() | ||
.setCode(StringUtils.join(lines, System.lineSeparator())).build()); | ||
|
@@ -161,6 +167,9 @@ private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws I | |
|
||
if (additionalPythonInitFile != null) { | ||
input = getClass().getClassLoader().getResourceAsStream(additionalPythonInitFile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, closing the input stream is missing; this can be done directly with try-with-resource. |
||
if (input == null) { | ||
throw new IOException("Cannot find resource: " + additionalPythonInitFile); | ||
} | ||
lines = IOUtils.readLines(input, StandardCharsets.UTF_8); | ||
response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder() | ||
.setCode(StringUtils.join(lines, System.lineSeparator()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
@@ -196,15 +197,21 @@ protected boolean useIPython() { | |
private void copyResourceToPythonWorkDir(String srcResourceName, | ||
String dstFileName) throws IOException { | ||
FileOutputStream out = null; | ||
InputStream in = null; | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use try-with-resources, also here |
||
out = new FileOutputStream(pythonWorkDir.getAbsoluteFile() + "/" + dstFileName); | ||
IOUtils.copy( | ||
getClass().getClassLoader().getResourceAsStream(srcResourceName), | ||
out); | ||
in = getClass().getClassLoader().getResourceAsStream(srcResourceName); | ||
if (in == null) { | ||
throw new IOException("Cannot find resource: " + srcResourceName); | ||
} | ||
IOUtils.copy(in, out); | ||
} finally { | ||
if (out != null) { | ||
out.close(); | ||
} | ||
if (in != null) { | ||
in.close(); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,9 @@ protected void initIRKernel() throws IOException, InterpreterException { | |
String timeout = getProperty("spark.r.backendConnectionTimeout", "6000"); | ||
InputStream input = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same please use try-with-resource do close the stream. |
||
getClass().getClassLoader().getResourceAsStream("R/zeppelin_isparkr.R"); | ||
if (input == null) { | ||
throw new IOException("Cannot find resource: R/zeppelin_isparkr.R"); | ||
} | ||
String code = IOUtils.toString(input, StandardCharsets.UTF_8) | ||
.replace("${Port}", sparkRBackend.port() + "") | ||
.replace("${version}", sparkVersion() + "") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,9 @@ public void open() throws IOException, InterpreterException { | |
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same use try-with-resource for the input and FileOutputStream |
||
out = new FileOutputStream(scriptFile); | ||
in = getClass().getClassLoader().getResourceAsStream("R/zeppelin_sparkr.R"); | ||
if (in == null) { | ||
throw new InterpreterException("Cannot find resource: R/zeppelin_sparkr.R"); | ||
} | ||
IOUtils.copy(in, out); | ||
} catch (IOException e) { | ||
throw new InterpreterException(e); | ||
|
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.
Please use try-with-resources. This is more elegant.
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html