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(abci): invalid error returned when codec terminates #117

Merged
merged 3 commits into from
Nov 28, 2024

Conversation

lklimek
Copy link
Collaborator

@lklimek lklimek commented Nov 28, 2024

Issue being fixed or feature implemented

When the Codec worker terminates (eg. client disconnected or some error occurred), it returns a generic Error::Async,

Detected by flaky Github Actions pipeline.

What was done?

Return Error::Cancelled() instead of Error::Async

How Has This Been Tested?

GHA pipeline pass

Breaking Changes

None

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

Summary by CodeRabbit

  • New Features

    • Introduced a new method to enhance message processing between the codec and request/response queues.
  • Bug Fixes

    • Improved error handling in message sending to better reflect cancellation scenarios.
  • Tests

    • Enhanced clarity in the test code for the KVStore by refining variable usage and improving readability.

@lklimek lklimek added this to the 1.4 milestone Nov 28, 2024
Copy link
Contributor

coderabbitai bot commented Nov 28, 2024

Walkthrough

The changes in this pull request involve modifications to the Codec struct in abci/src/server/codec.rs, including the addition of a new method process_worker_queues for improved message handling and error management. The send method's error handling logic is also updated. In the test file abci/tests/kvstore.rs, the test_kvstore function is enhanced for clarity by renaming variables and improving the structure without altering any public interfaces or methods.

Changes

File Change Summary
abci/src/server/codec.rs - Added async fn process_worker_queues<L: AsyncRead + AsyncWrite + Unpin>(...) to Codec.
- Updated send method's error handling to return Cancelled instead of closed channel error.
abci/tests/kvstore.rs - Renamed _td to td for clarity.
- Introduced next_client variable for server.next_client() result and updated assertions for improved readability.

Suggested reviewers

  • shumkov

🐰 In the land of code, where changes abound,
A new method hops in, with messages found.
With clearer tests and error flows bright,
The Codec now dances, in day and in night!
So let’s celebrate this code, oh so fine,
With a twitch of our noses, and a hop in our line! 🐇✨


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

🧹 Outside diff range and nitpick comments (3)
abci/src/server/codec.rs (2)

100-111: Fix incomplete documentation.

The documentation comment for the error handling section appears to be incomplete. The last sentence "and forwards to" is hanging.

-    /// and forwards to
+    /// and forwards them back to the ABCI client.

Line range hint 231-321: Consider adding tests for error handling scenarios.

While the existing test covers message processing, consider adding tests for:

  1. Verifying that send() returns Error::Cancelled() when the worker terminates
  2. Testing various worker termination scenarios (client disconnect, errors, cancellation)

Example test structure:

#[tokio::test]
async fn test_send_after_worker_termination() {
    let (request_tx, _) = mpsc::channel::<abci::Request>(1);
    let (response_tx, response_rx) = mpsc::channel::<abci::Response>(1);
    let cancel = CancellationToken::new();
    
    // Set up codec
    let (client, server) = tokio::io::duplex(1024);
    let codec = tokio_util::codec::Framed::new(server, super::Coder {});
    
    // Start worker
    let worker_cancel = cancel.clone();
    let hdl = tokio::spawn(super::Codec::process_worker_queues(
        codec,
        request_tx,
        response_rx,
        worker_cancel,
    ));
    
    // Cancel worker
    cancel.cancel();
    hdl.await.unwrap();
    
    // Verify send returns Error::Cancelled
    match response_tx.blocking_send(create_test_response()) {
        Err(e) => assert_matches!(e, Error::Cancelled()),
        _ => panic!("Expected Error::Cancelled"),
    }
}
abci/tests/kvstore.rs (1)

68-71: LGTM: Proper error handling test

The changes correctly test the new error behavior where Codec worker returns Error::Cancelled(). The added debug logging will help with test diagnostics.

Consider adding a comment explaining why we expect Error::Cancelled() here, to make the test's intention clearer:

+    // We expect Error::Cancelled() because the server is terminated by the cancel token
     assert!(matches!(
         next_client,
         Err(tenderdash_abci::Error::Cancelled())
     ));
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4fba1f8 and b178ca8.

📒 Files selected for processing (2)
  • abci/src/server/codec.rs (2 hunks)
  • abci/tests/kvstore.rs (1 hunks)
🔇 Additional comments (4)
abci/src/server/codec.rs (2)

Line range hint 112-163: LGTM! Robust error handling implementation.

The implementation shows excellent attention to detail:

  • Proper backpressure handling by checking request_tx capacity
  • Consistent error handling with cancellation
  • Clear handling of connection termination cases
  • Well-structured concurrent processing using tokio::select!

164-167: LGTM! Improved error handling clarity.

The change from a generic error to Error::Cancelled() provides better context about why the send operation failed. The comment clearly explains that this occurs when the worker has either errored or been cancelled.

abci/tests/kvstore.rs (2)

66-66: LGTM: Proper variable naming

Removing the unused variable prefix _ is appropriate as the td variable is now explicitly used for cleanup.


79-80: LGTM: Proper resource cleanup

The explicit drop of the TenderdashDocker instance ensures proper cleanup of resources. The added empty line improves readability by separating the cleanup from the test assertions.

@lklimek lklimek modified the milestones: 1.4, 1.3 Nov 28, 2024
@lklimek lklimek merged commit c8f8e6d into develop Nov 28, 2024
10 checks passed
@lklimek lklimek deleted the fix/codec-termination-error branch November 28, 2024 11:26
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