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(store/v2): chunkBody.Close and chunkFile.Close both are called twice #23271

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lfz941
Copy link
Contributor

@lfz941 lfz941 commented Jan 9, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling in the chunk saving process.
    • Enhanced resource closure error reporting mechanism.
  • Refactor

    • Updated method signature to provide more comprehensive error tracking.
    • Consolidated error handling for resource closures.

@lfz941 lfz941 requested a review from a team as a code owner January 9, 2025 12:41
Copy link
Contributor

coderabbitai bot commented Jan 9, 2025

📝 Walkthrough

Walkthrough

The pull request modifies the saveChunk method in the store/v2/snapshots/store.go file, focusing on improving error handling during resource management. The method signature is updated to explicitly return an error, and the resource closure process is restructured to use deferred functions. This change ensures that any errors occurring during the closure of chunkBody and chunkFile are captured and reported back through the method's return value.

Changes

File Change Summary
store/v2/snapshots/store.go - Updated method signature to return an error
- Implemented deferred functions for closing resources
- Consolidated error handling for resource closures

Possibly related PRs

  • fix(store/v2): chunk.Close is called twice #23168: The changes in the Load method also involve improved resource management and error handling related to the chunk variable in the same store/v2/snapshots/store.go file, indicating a focus on robust closure handling similar to the modifications made in the saveChunk method.

Suggested reviewers

  • kocubinski
  • cool-develope
  • aljo242
  • julienrbrt

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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
Contributor

@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

🧹 Nitpick comments (1)
store/v2/snapshots/store.go (1)

314-318: Consider combining errors from both Close operations.

While the current implementation is good, consider handling the case where both chunkBody.Close() and chunkFile.Close() fail. You could use errors.Join to combine both errors:

 func (s *Store) saveChunk(chunkBody io.ReadCloser, index uint32, snapshot *types.Snapshot, chunkHasher, snapshotHasher hash.Hash) (err error) {
+    var bodyErr, fileErr error
     defer func() {
-        if cErr := chunkBody.Close(); cErr != nil {
-            err = errors.Wrapf(cErr, "failed to close snapshot chunk body %d", index)
+        if cErr := chunkBody.Close(); cErr != nil {
+            bodyErr = errors.Wrapf(cErr, "failed to close snapshot chunk body %d", index)
         }
     }()
     // ... existing code ...
     defer func() {
-        if cErr := chunkFile.Close(); cErr != nil {
-            err = errors.Wrapf(cErr, "failed to close snapshot chunk file %d", index)
+        if cErr := chunkFile.Close(); cErr != nil {
+            fileErr = errors.Wrapf(cErr, "failed to close snapshot chunk file %d", index)
         }
+        if bodyErr != nil || fileErr != nil {
+            err = errors.Join(bodyErr, fileErr)
+        }
     }()
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 60e8c6a and bef295e.

📒 Files selected for processing (1)
  • store/v2/snapshots/store.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
store/v2/snapshots/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: tests (03)
  • GitHub Check: tests (02)
  • GitHub Check: tests (01)
  • GitHub Check: tests (00)
  • GitHub Check: test-simapp-v2
  • GitHub Check: test-integration
  • GitHub Check: test-system-v2
  • GitHub Check: build (arm64)
  • GitHub Check: build (amd64)
  • GitHub Check: Analyze
  • GitHub Check: golangci-lint
  • GitHub Check: Summary
🔇 Additional comments (2)
store/v2/snapshots/store.go (2)

301-301: LGTM! Good use of named return parameter.

The named return parameter err is well-suited for the deferred error handling pattern being implemented.


302-306: LGTM! Well-structured error handling for resource cleanup.

The deferred function properly captures and wraps any errors that occur during chunkBody.Close(), providing good context for debugging.

@lfz941 lfz941 force-pushed the fix/duplicate-close branch from f4d880e to dbfa2d8 Compare January 10, 2025 02:02
Copy link
Contributor

@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

🧹 Nitpick comments (1)
store/v2/snapshots/store.go (1)

313-318: Consider enhancing error handling for multiple closure failures.

While the current implementation is correct, consider preserving both errors when both chunkBody and chunkFile fail to close.

 func (s *Store) saveChunk(chunkBody io.ReadCloser, index uint32, snapshot *types.Snapshot, chunkHasher, snapshotHasher hash.Hash) (err error) {
+    var chunkBodyErr error
     defer func() {
         if cErr := chunkBody.Close(); cErr != nil {
-            err = errors.Wrapf(cErr, "failed to close snapshot chunk body %d", index)
+            chunkBodyErr = errors.Wrapf(cErr, "failed to close snapshot chunk body %d", index)
+            if err == nil {
+                err = chunkBodyErr
+            } else {
+                err = errors.Wrapf(err, "also failed to close chunk body: %v", chunkBodyErr)
+            }
         }
     }()
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bef295e and dbfa2d8.

📒 Files selected for processing (1)
  • store/v2/snapshots/store.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
store/v2/snapshots/store.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: tests (00)
  • GitHub Check: test-simapp-v2
  • GitHub Check: test-system-v2
  • GitHub Check: Analyze
  • GitHub Check: Summary
🔇 Additional comments (2)
store/v2/snapshots/store.go (2)

301-301: LGTM! Good improvement to error handling.

The addition of a named return parameter err enhances readability and makes error propagation more explicit.


302-306: LGTM! Well-structured error handling for resource cleanup.

The deferred closure of chunkBody with proper error wrapping and contextual information is a good practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant