Skip to content

refactor(core): improve multihasher seek error handling and close behavior #882

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

Merged
merged 1 commit into from
Jul 8, 2025

Conversation

pcfreak30
Copy link
Member

@pcfreak30 pcfreak30 commented Jul 8, 2025

  • Changed seek error from generic "seek error" to io.ErrUnexpectedEOF for consistency
  • Added proper error handling for closing seekable sources
  • Implemented io.Seeker interface with position reset functionality
  • Added test cases for seek and close operations

Summary by CodeRabbit

  • New Features

    • Added support for seeking within the MultiHasher component, allowing users to change the read position if the underlying source supports it.
    • MultiHasher now provides a method to retrieve internal hashers, enhancing transparency for advanced users.
  • Bug Fixes

    • Improved error handling when closing sources and during seek operations, ensuring more consistent and informative error reporting.
  • Tests

    • Expanded test coverage for seeking, closing, and error scenarios to improve reliability and correctness.

Copy link

changeset-bot bot commented Jul 8, 2025

⚠️ No Changeset found

Latest commit: 1d548d1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

coderabbitai bot commented Jul 8, 2025

Warning

Rate limit exceeded

@pcfreak30 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 1 minutes and 21 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 4355478 and 1d548d1.

📒 Files selected for processing (2)
  • core/internal/core_tests/multihasher_test.go (3 hunks)
  • core/multihasher.go (5 hunks)

Walkthrough

The changes introduce seeking support and enhanced closing behavior to the MultiHasher component. The MultiHasher now implements io.Seeker and io.ReadCloser, with a new Seek method and an updated Close method that returns an error and closes the underlying source if possible. Additional tests verify these behaviors and improve error handling.

Changes

File(s) Change Summary
core/multihasher.go Added Seek and GetHashes methods to MultiHasher; updated Close to return error and close source; added interface assertions; minor formatting changes.
core/internal/core_tests/multihasher_test.go Added tests for seeking, closing with a closable source, error cases on seek, and improved teardown error handling.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant MultiHasher
    participant Source (io.Reader/io.Seeker/io.Closer)
    participant Hashers

    Caller->>MultiHasher: Seek(offset, whence)
    alt Source supports io.Seeker
        MultiHasher->>Source: Seek(offset, whence)
        Source-->>MultiHasher: newPos, err
        MultiHasher->>Hashers: Reset all hashers
        MultiHasher-->>Caller: newPos, err
    else Source doesn't support io.Seeker
        MultiHasher-->>Caller: error
    end

    Caller->>MultiHasher: Close()
    alt Source supports io.Closer
        MultiHasher->>Source: Close()
        Source-->>MultiHasher: err
        MultiHasher-->>Caller: err
    else
        MultiHasher-->>Caller: nil
    end
Loading

Possibly related PRs

Poem

In the land of hashes, a leap was made,
Now seeking and closing, our MultiHasher's trade.
With tests that ensure no errors slip through,
And closures that work, just like they should do.
🐇✨ Hash on, little bunny, your code is brand new!

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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

🧹 Nitpick comments (1)
core/multihasher.go (1)

291-315: Consider using more specific error types for different failure scenarios.

The method correctly implements seeking functionality and properly resets hashers after position changes. However, returning io.ErrUnexpectedEOF for all error cases might not be the most appropriate:

  • Line 293: When source is nil, io.ErrUnexpectedEOF is reasonable
  • Line 298: When source doesn't implement io.Seeker, consider errors.New("seek not supported") or a custom error
  • Line 304: When the underlying seek fails, consider returning the original error instead of wrapping it

Consider this more specific error handling:

 func (m *MultiHasher) Seek(offset int64, whence int) (int64, error) {
 	if m.source == nil {
 		return 0, io.ErrUnexpectedEOF
 	}
 
 	seeker, ok := m.source.(io.Seeker)
 	if !ok {
-		return 0, io.ErrUnexpectedEOF
+		return 0, errors.New("source does not support seeking")
 	}
 
 	// Seek the underlying reader
 	newPos, err := seeker.Seek(offset, whence)
 	if err != nil {
-		return newPos, io.ErrUnexpectedEOF
+		return newPos, err
 	}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6cf2572 and 4355478.

📒 Files selected for processing (2)
  • core/internal/core_tests/multihasher_test.go (3 hunks)
  • core/multihasher.go (4 hunks)
🔇 Additional comments (9)
core/multihasher.go (4)

33-37: LGTM! Good practice for interface compliance verification.

The explicit interface assertions ensure compile-time verification that MultiHasher implements the expected interfaces. This is a best practice for interface compliance.


317-326: LGTM! Proper implementation of io.Closer interface.

The method correctly:

  • Waits for worker pool completion before closing
  • Closes the source reader if it implements io.Closer
  • Returns appropriate errors

328-335: LGTM! Thread-safe getter method for testing.

The method properly uses mutex for thread safety and is clearly documented as being for testing purposes only.


81-86: Fix the error handling logic in Close call.

The current logic only returns the original seek error if the Close call succeeds, but returns the Close error if it fails. This seems incorrect - you should prioritize the original error.

Apply this fix:

 		if _, err := seeker.Seek(0, io.SeekStart); err != nil {
-			err2 := hasher.Close()
-			if err2 != nil {
-				return nil, err
-			}
+			if closeErr := hasher.Close(); closeErr != nil {
+				// Log the close error but return the original seek error
+				return nil, err
+			}
 			return nil, err
 		}

Likely an incorrect or invalid review comment.

core/internal/core_tests/multihasher_test.go (5)

27-27: LGTM! Error type now matches the Seek method implementation.

The change to return io.ErrUnexpectedEOF aligns with the error handling in the main Seek method implementation.


620-644: LGTM! Comprehensive test for Close method with closable source.

The test properly verifies that:

  • The Close method returns no error
  • The underlying source remains intact after closing
  • The functionality works correctly with io.ReadCloser

646-688: LGTM! Thorough test coverage for Seek functionality.

The test comprehensively covers:

  • Seeking to start, end, and middle positions
  • Proper position returns
  • Hash reset verification after seeking

The use of GetHashes() method for verification is appropriate for testing purposes.


690-725: LGTM! Good error case coverage for Seek method.

The tests properly cover both error scenarios:

  • Non-seekable source returning the expected error
  • Failing seek operations during initialization

The error assertions match the expected behavior from the implementation.


883-888: LGTM! Improved error handling in test cleanup.

The deferred Close call now properly asserts that no error occurs, which aligns with the updated Close method signature.

…avior

- Changed seek error from generic "seek error" to `io.ErrUnexpectedEOF` for consistency
- Added proper error handling for closing seekable sources
- Implemented `io.Seeker` interface with position reset functionality
- Added test cases for seek and close operations
@pcfreak30 pcfreak30 merged commit 8d909a5 into develop Jul 8, 2025
1 of 2 checks passed
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.

1 participant