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

[PLA-2087] Catch all handlers #68

Merged
merged 5 commits into from
Nov 26, 2024
Merged

[PLA-2087] Catch all handlers #68

merged 5 commits into from
Nov 26, 2024

Conversation

leonardocustodio
Copy link
Contributor

@leonardocustodio leonardocustodio commented Nov 26, 2024

PR Type

enhancement, bug fix


Description

  • Enhanced job execution by using tokio::select! for concurrent execution and added error handling for subscription job failures.
  • Modified start methods in SubscriptionJob, TransactionJob, and DeriveWalletJob to return JoinHandle for better task management.
  • Updated .env file to clear the RUST_LOG environment variable.
  • Bumped the package version to 2.0.3 and added anyhow as a new dependency in Cargo.toml.
  • Enabled debug options in the release profile for better debugging capabilities.

Changes walkthrough 📝

Relevant files
Enhancement
main.rs
Concurrent job execution with error handling                         

src/main.rs

  • Replaced sequential job starts with tokio::select! for concurrent
    execution.
  • Added error handling for subscription job failure.
  • +10/-9   
    subscription.rs
    Improved subscription job concurrency and error logging   

    src/subscription.rs

  • Changed start method to return JoinHandle.
  • Used tokio::select! for concurrent block and runtime subscription.
  • Added error logging for unexpected stream endings.
  • +12/-22 
    transaction.rs
    Return `JoinHandle` for transaction job start                       

    src/transaction.rs

    • Modified start method to return JoinHandle.
    +2/-2     
    wallet.rs
    Return `JoinHandle` for wallet job start                                 

    src/wallet.rs

    • Modified start method to return JoinHandle.
    +2/-2     
    Configuration changes
    .env
    Update logging configuration in environment file                 

    .env

    • Cleared RUST_LOG environment variable.
    +1/-1     
    Dependencies
    Cargo.toml
    Version bump and dependency update                                             

    Cargo.toml

  • Bumped package version to 2.0.3.
  • Added anyhow dependency.
  • Enabled debug options in release profile.
  • +7/-1     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling
    The error handling in the tokio::select! block uses unwrap_err() which will panic if the result is Ok. Consider handling Ok cases or using a safer method to access errors.

    Concurrency Handling
    The new implementation using tokio::select! might not ensure that both block_subscription and runtime_subscription tasks are fully executed as intended due to premature termination of one task stopping the other.

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Improve error handling to prevent runtime panics

    Handle potential errors from the unwrap_err() method to prevent panics in production
    code.

    src/main.rs [74-75]

    -let err = r.unwrap_err();
    -tracing::error!("Subscription job failed: {:?}", err);
    +if let Err(err) = r {
    +    tracing::error!("Subscription job failed: {:?}", err);
    +}
    Suggestion importance[1-10]: 8

    Why: The suggestion correctly identifies a potential runtime panic due to the use of unwrap_err() without checking if the result is an error. Implementing this change would significantly enhance the robustness of the error handling.

    8
    Add error handling for all tasks in the tokio::select! macro

    Ensure that the tokio::select! macro handles the case where all tasks complete
    without errors.

    src/main.rs [68-76]

     tokio::select! {
         _ = transaction_poller.start() => {}
         _ =  transaction_processor.start() => {}
         _ = wallet_poller.start() => {}
         _ = wallet_processor.start() => {}
         r = subscription_job.start() => {
    -        let err = r.unwrap_err();
    -        tracing::error!("Subscription job failed: {:?}", err);
    +        if let Err(err) = r {
    +            tracing::error!("Subscription job failed: {:?}", err);
    +        }
         }
     }
    Suggestion importance[1-10]: 8

    Why: This suggestion improves upon the first by applying error handling to all tasks within the tokio::select! macro, not just the subscription job. This is a critical enhancement for preventing unhandled errors from all concurrent tasks.

    8
    Improve robustness by handling errors during subscription to finalized blocks

    Replace the direct use of unwrap() with error handling to prevent potential panics
    when the stream ends unexpectedly.

    src/subscription.rs [92]

    -let mut blocks_sub = self.rpc.blocks().subscribe_finalized().await.unwrap();
    +let mut blocks_sub = match self.rpc.blocks().subscribe_finalized().await {
    +    Ok(sub) => sub,
    +    Err(e) => {
    +        tracing::error!("Failed to subscribe to finalized blocks: {:?}", e);
    +        return;
    +    }
    +};
    Suggestion importance[1-10]: 7

    Why: Replacing unwrap() with proper error handling in the subscription to finalized blocks prevents potential panics and improves the service's reliability, especially in edge cases where the subscription might fail.

    7
    Add error handling for subscription methods to improve application stability

    Implement error handling for the runtime_subscription and block_subscription methods
    to manage unexpected stream terminations.

    src/subscription.rs [89-118]

    -tracing::error!("Runtime update stream ended unexpectedly");
    -tracing::error!("Block subscription stream ended unexpectedly");
    +if let Err(e) = runtime_sub.runtime_subscription(updater).await {
    +    tracing::error!("Runtime subscription failed: {:?}", e);
    +}
    +if let Err(e) = block_sub.block_subscription().await {
    +    tracing::error!("Block subscription failed: {:?}", e);
    +}
    Suggestion importance[1-10]: 7

    Why: The suggestion to add error handling for both runtime_subscription and block_subscription methods is crucial for managing unexpected terminations and ensuring the application remains stable under various conditions.

    7

    @leonardocustodio leonardocustodio merged commit 44534cb into master Nov 26, 2024
    12 checks passed
    @leonardocustodio leonardocustodio deleted the PLA-2087v2 branch November 26, 2024 05:54
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Development

    Successfully merging this pull request may close these issues.

    2 participants