-
Notifications
You must be signed in to change notification settings - Fork 121
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
fix: close inputStream to avoid resource leaks #1025
Conversation
WalkthroughThe recent updates to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java (2 hunks)
Additional comments not posted (5)
bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java (5)
28-28
: Good practice: Added Logger import.The addition of the
Logger
import indicates a shift towards better logging practices.
39-39
: Improvement: InitializeinputStream
to null.Initializing
inputStream
to null is a good practice to ensure it is properly managed within the try-finally block.
41-45
: Improvement: Use try block for resource management.The use of a try block ensures that the
FileInputStream
is properly closed after use, preventing potential resource leaks.
66-69
: Improvement: Handle IOException during file writing.The nested try-catch block for handling
IOException
during file writing is a good practice to ensure errors are properly managed and reported.
70-82
: Improvement: EnsureinputStream
is closed in finally block.Ensuring that the
inputStream
is closed in the finally block is crucial for preventing resource leaks. The addition of logging for theIOException
is also a good practice.
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.
LGTM.
NIT: I would use try-with-resources on both inputStream and fileWriter to reduce nesting and mention encoding to satisfy spotbugs
@@ -35,33 +36,50 @@ public class ClangdConfigFileHandler | |||
public void update(IProject project) throws CoreException, IOException | |||
{ | |||
File file = getClangdConfigFile(project); | |||
FileInputStream inputStream = null; |
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.
Since FileInputStream is AutoClosable we can use try-with-resources. Also, to reduce nesting we can put both inputStream and fileWriter into one try-with-resources block like this:
try (FileInputStream inputStream = new FileInputStream(file); Writer writer = new FileWriter(file))
{}
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.
fixed
} | ||
catch (IOException e) | ||
// Write updated clangd back to file | ||
try (Writer writer = new FileWriter(file)) |
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.
spotbugs complains about reliance on the default encoding here, so we can use UTF-8 to avoid this
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.
Fixed
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java (2 hunks)
Additional comments not posted (5)
bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java (5)
12-12
: Good practice: Use of explicit character encoding.Using
StandardCharsets.UTF_8
ensures consistent character encoding and avoids reliance on the platform's default encoding.
41-44
: Improved resource management:try-with-resources
forFileInputStream
.The
try-with-resources
statement ensures thatinputStream
is automatically closed, preventing potential resource leaks.
60-63
: Improved resource management:try-with-resources
forFileWriter
.The
try-with-resources
statement ensures thatwriter
is automatically closed, preventing potential resource leaks.
64-67
: Enhanced error handling: Specific IOException message for writing errors.The catch block provides a more descriptive error message, indicating that the issue occurred while writing the
.clangd
file.
70-71
: Enhanced error handling: Specific IOException message for reading errors.The catch block provides a more descriptive error message, indicating that the issue occurred while reading the
.clangd
file.
Description
PR will address the below issue by closing inputStream to avoid resource leaks
Type of change
How has this been tested?
Windows 11 / MacOS
New workspace -> create project -> delete .clangd file -> create new one using "ESP-IDF: Create clangd Config" -> try to delete - can't delete:
happens SOMETIME. Try multiple times with newly created workspace !
Test Configuration:
Dependent components impacted by this PR:
Checklist
Summary by CodeRabbit
Bug Fixes
New Features
Chores