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

zstd: Fix extra CRC written with multiple Close calls #1017

Merged
merged 2 commits into from
Oct 8, 2024

Conversation

klauspost
Copy link
Owner

@klauspost klauspost commented Oct 8, 2024

Fixes #1016

Summary by CodeRabbit

  • New Features
    • Introduced a new error variable, ErrEncoderClosed, for improved error handling when using the Encoder after closure.
  • Bug Fixes
    • Enhanced error handling in the Write, Flush, and Close methods of the Encoder to manage specific error states after closure.
  • Tests
    • Improved test cases for the Zstandard encoder, including refined error messages and performance logging.
    • Streamlined code and enhanced test data handling for better accuracy and clarity.

Copy link

coderabbitai bot commented Oct 8, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes introduced in this pull request enhance error handling and control flow in the Encoder struct within zstd/encoder.go. Key modifications include the introduction of a new error variable, ErrEncoderClosed, and updates to the Write, Flush, and Close methods to manage encoder state more effectively. Additionally, the test file zstd/encoder_test.go has been improved with better error handling, logging, and code cleanup. These modifications collectively aim to ensure robust encoder lifecycle management and accurate test reporting.

Changes

File Change Summary
zstd/encoder.go Enhanced Write, Flush, and Close methods to handle ErrEncoderClosed; added import for errors. New error variable ErrEncoderClosed defined.
zstd/encoder_test.go Improved error handling and logging in tests; removed redundant code; enhanced clarity of test failures; general code cleanup.
zstd/zstd.go Added new error variable ErrEncoderClosed.

Assessment against linked issues

Objective Addressed Explanation
Ensure Close() is idempotent to prevent duplicate CRC checksums (Issue #1016)

Possibly related PRs

  • zstd: Improve memory usage on small streaming encodes #1007: The changes in zstd/encoder.go regarding error handling and the introduction of ErrEncoderClosed are directly related to the modifications in the Encoder functionality in the same file, which also involves resource management and error handling improvements.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a452cbb and 05c93ea.

📒 Files selected for processing (2)
  • zstd/encoder.go (6 hunks)
  • zstd/encoder_test.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • zstd/encoder.go
  • zstd/encoder_test.go

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

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

🧹 Outside diff range and nitpick comments (2)
zstd/zstd.go (1)

91-93: LGTM! Consider adding a comment about usage.

The addition of ErrEncoderClosed is well-placed and consistent with the existing error declarations. It complements ErrDecoderClosed and aligns with the PR objectives.

Consider adding a brief comment above the error declaration to provide context on when and where this error is used, similar to the comment for ErrDecoderClosed. This would enhance code readability and maintainability. For example:

// ErrEncoderClosed will be returned if the Encoder is used after
// Close has been called.
ErrEncoderClosed = errors.New("encoder used after Close")
zstd/encoder.go (1)

421-421: LGTM: Improved error handling for idempotent Close() method.

The changes effectively address the issue of multiple Close() calls writing duplicate CRC checksums. The method is now idempotent, aligning with the behavior of other io.WriteCloser implementations.

A minor suggestion for improvement:
Consider using a custom error variable (e.g., ErrEncoderClosed) instead of ErrDecoderClosed for better clarity and consistency with the encoder context.

If you agree, you could implement this change as follows:

-if s.encoder == nil || errors.Is(s.err, ErrDecoderClosed) {
+if s.encoder == nil || errors.Is(s.err, ErrEncoderClosed) {
 	return nil
 }
 // ... (existing code)
 if s.err == nil {
-	s.err = ErrDecoderClosed
+	s.err = ErrEncoderClosed
 	return nil
 }

Don't forget to declare ErrEncoderClosed at the package level:

var ErrEncoderClosed = errors.New("encoder closed")

Also applies to: 463-467

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between dbd6c38 and a452cbb.

📒 Files selected for processing (3)
  • zstd/encoder.go (3 hunks)
  • zstd/encoder_test.go (1 hunks)
  • zstd/zstd.go (1 hunks)
🧰 Additional context used
🔇 Additional comments (3)
zstd/encoder.go (1)

9-9: LGTM: New import for error handling.

The addition of the errors package import is appropriate for the new error handling in the Close() method.

zstd/encoder_test.go (2)

281-284: LGTM: Testing multiple Close() calls

This addition improves the test coverage by verifying the behavior of calling Close() multiple times on the encoder. It aligns well with the PR objective of fixing issues related to multiple Close() calls.


291-291: Improved error logging

The modification enhances error reporting by including the expected and actual lengths of the decoded data. This additional information will be valuable for debugging in case of test failures.

@klauspost klauspost merged commit 72cd4a9 into master Oct 8, 2024
19 checks passed
@klauspost klauspost deleted the zstd-fix-double-close branch October 8, 2024 09:20
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.

Multiple Calls to zstd.(*Encoder).Close() Write Duplicate CRC Checksums
1 participant