Skip to content

[ZEPPELIN-6268] Fix resource leaks and add null checks for getResourceAsStream#5010

Open
renechoi wants to merge 1 commit into
apache:masterfrom
renechoi:ZEPPELIN-6268-clean
Open

[ZEPPELIN-6268] Fix resource leaks and add null checks for getResourceAsStream#5010
renechoi wants to merge 1 commit into
apache:masterfrom
renechoi:ZEPPELIN-6268-clean

Conversation

@renechoi

@renechoi renechoi commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

What is this PR for?

This PR fixes resource management issues throughout the Zeppelin codebase, specifically addressing resource leaks and potential NullPointerExceptions.

What type of PR is it?

Bug Fix / Improvement

What is the Jira issue?

(Please create a JIRA issue if needed)

How should this be tested?

  • All modified files compile successfully
  • Existing tests should pass without modification
  • The changes maintain backward compatibility

Screenshots (if appropriate)

N/A

Questions:

  • Does the license files need update? No
  • Is there breaking changes for older versions? No
  • Does this need documentation? No (internal improvements only)

Changes Made:

1. AlluxioInterpreterTest.java

  • Fixed FileInputStream resource leak by using try-finally block
  • Ensures stream is closed even when exceptions occur

2. Resource Loading Null Checks

Added null checks for getResourceAsStream() calls in:

  • ZeppelinR.java: Loading R/zeppelin_sparkr.R
  • PythonInterpreter.java: Loading Python resources (also fixed missing InputStream import)
  • IPythonInterpreter.java: Loading python/zeppelin_ipython.py, python/zeppelin_context.py, and additional init files
  • IRInterpreter.java: Loading R/zeppelin_isparkr.R

Benefits:

  • Prevents resource leaks that could cause memory issues in long-running processes
  • Prevents NullPointerException when resources are missing
  • Provides clear error messages when resources cannot be found
  • Improves overall stability and reliability

Testing Notes:

While I couldn't run all tests due to environment setup issues, all files compile successfully and the changes are minimal and safe - they only add defensive programming practices without changing any business logic.

@Reamer Reamer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As you can see, I am a big fan of try-with-resources.
This is a JDK8 feature and should be used because it saves a lot of boilerplate code.

fis.read(read);
fis.close();
assertTrue(BufferUtils.equalIncreasingByteArray(size, read));
try {

Copy link
Copy Markdown
Contributor

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

String dstFileName) throws IOException {
FileOutputStream out = null;
InputStream in = null;
try {

Copy link
Copy Markdown
Contributor

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, also here

@@ -134,6 +134,9 @@ private void setupJVMGateway(String gatewayHost, int gatewayPort) throws IOExcep
private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws IOException {
InputStream input =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

@@ -145,6 +148,9 @@ private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws I

input =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

@@ -161,6 +167,9 @@ private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws I

if (additionalPythonInitFile != null) {
input = getClass().getClassLoader().getResourceAsStream(additionalPythonInitFile);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

@@ -129,6 +129,9 @@ protected void initIRKernel() throws IOException, InterpreterException {
String timeout = getProperty("spark.r.backendConnectionTimeout", "6000");
InputStream input =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same please use try-with-resource do close the stream.

@@ -97,6 +97,9 @@ public void open() throws IOException, InterpreterException {
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same use try-with-resource for the input and FileOutputStream

@Reamer Reamer self-assigned this Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants