Skip to content
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

Merged
merged 2 commits into from
Aug 20, 2024
Merged

Conversation

kolipakakondal
Copy link
Collaborator

@kolipakakondal kolipakakondal commented Jul 29, 2024

Description

PR will address the below issue by closing inputStream to avoid resource leaks

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 ! 

image

Type of change

  • Bug fix (non-breaking change which fixes an issue)

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:

  • ESP-IDF Version: master
  • OS (Windows,Linux and macOS): tested with macOS

Dependent components impacted by this PR:

  • ESP-IDF: Create clangd Config

Checklist

  • PR Self Reviewed
  • Applied Code formatting
  • Added Documentation
  • Added Unit Test
  • Verified on all platforms - Windows,Linux and macOS

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced error handling for the update process, providing clearer error messages and improving overall clarity.
  • New Features

    • Implemented improved resource management for file operations, significantly reducing the risk of memory leaks.
  • Chores

    • Adopted better logging practices for effective tracking of issues during the update process.

Copy link

coderabbitai bot commented Jul 29, 2024

Walkthrough

The recent updates to the ClangdConfigFileHandler class significantly enhance error handling and resource management within the update method. By integrating a logging system and ensuring proper closure of resources with a try-with-resources statement, the changes improve clarity in error reporting and bolster the overall robustness and maintainability of the codebase.

Changes

Files Change Summary
bundles/com.espressif.idf.core/... Enhanced error handling and resource management in ClangdConfigFileHandler.update method. Introduced logging, ensured proper closure of FileInputStream, and specified UTF-8 encoding for FileWriter.

Poem

🐰 In the code, a change so bright,
Error logs now shine with light.
Resources close, no leaks in sight,
A happy code, all feels just right!
With every line, we leap and bound,
In rabbit's joy, improvements found! 🐇✨


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a 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

Commits

Files that changed from the base of the PR and between e97546f and e43366e.

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: Initialize inputStream 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: Ensure inputStream 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 the IOException is also a good practice.

Copy link
Collaborator

@sigmaaa sigmaaa left a 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;
Copy link
Collaborator

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))
{}

Copy link
Collaborator Author

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))
Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

Copy link

@coderabbitai coderabbitai bot left a 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

Commits

Files that changed from the base of the PR and between e43366e and 7506a62.

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 for FileInputStream.

The try-with-resources statement ensures that inputStream is automatically closed, preventing potential resource leaks.


60-63: Improved resource management: try-with-resources for FileWriter.

The try-with-resources statement ensures that writer 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.

@kolipakakondal kolipakakondal merged commit 0b73fe9 into master Aug 20, 2024
2 of 5 checks passed
@kolipakakondal kolipakakondal deleted the clangd_issue branch August 20, 2024 12:41
@kolipakakondal kolipakakondal added this to the v3.1.0 milestone Sep 4, 2024
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