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.
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
Loggerimport indicates a shift towards better logging practices.
39-39: Improvement: InitializeinputStreamto null.Initializing
inputStreamto 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
FileInputStreamis properly closed after use, preventing potential resource leaks.
66-69: Improvement: Handle IOException during file writing.The nested try-catch block for handling
IOExceptionduring file writing is a good practice to ensure errors are properly managed and reported.
70-82: Improvement: EnsureinputStreamis closed in finally block.Ensuring that the
inputStreamis closed in the finally block is crucial for preventing resource leaks. The addition of logging for theIOExceptionis also a good practice.
sigmaaa
left a comment
There was a problem hiding this comment.
LGTM.
NIT: I would use try-with-resources on both inputStream and fileWriter to reduce nesting and mention encoding to satisfy spotbugs
| public void update(IProject project) throws CoreException, IOException | ||
| { | ||
| File file = getClangdConfigFile(project); | ||
| FileInputStream inputStream = null; |
There was a problem hiding this comment.
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))
{}
| } | ||
| catch (IOException e) | ||
| // Write updated clangd back to file | ||
| try (Writer writer = new FileWriter(file)) |
There was a problem hiding this comment.
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.
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_8ensures consistent character encoding and avoids reliance on the platform's default encoding.
41-44: Improved resource management:try-with-resourcesforFileInputStream.The
try-with-resourcesstatement ensures thatinputStreamis automatically closed, preventing potential resource leaks.
60-63: Improved resource management:try-with-resourcesforFileWriter.The
try-with-resourcesstatement ensures thatwriteris 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
.clangdfile.
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
.clangdfile.
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