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: removed try_join to await futures #337

Merged
merged 6 commits into from
Aug 12, 2024

Conversation

frolvanya
Copy link
Collaborator

This PR fixes a misconception behind try_join method. It doesn't await until the finish state of all futures, but rather kills other futures if one one them returns an error. You can try it yourself:

use anyhow::Result;

async fn a() -> Result<()> {
    println!("Throwing an error");
    anyhow::bail!("This is an error");
}

async fn b() -> Result<()> {
    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    println!("Returning OK");
    Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
    futures::try_join!(a(), b())?;

    Ok(())
}

As you can see it doesn't print "Returning OK" to the stdout:
image

Now let's modify our code to use regular join_all instead and handle possible errors afterwards:

use anyhow::Result;
use futures::FutureExt;

async fn a() -> Result<()> {
    println!("Throwing an error");
    anyhow::bail!("This is an error");
}

async fn b() -> Result<()> {
    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    println!("Returning OK");
    Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
    futures::future::join_all([a().boxed(), b().boxed()])
        .await
        .into_iter()
        .collect::<Result<_>>()?;

    Ok(())
}

And here's the new ouput which waits for both function to finish their execution before handling an error:
image

@frolvanya
Copy link
Collaborator Author

@race-of-sloths

@race-of-sloths
Copy link

race-of-sloths commented Aug 9, 2024

@frolvanya Thank you for your contribution! Your pull request is now a part of the Race of Sloths!
Great job! Usain Bolt jealous of your results!

Shows profile picture for the author of the PR

Current status: executed
Reviewer Score
@race-of-sloths 2

Your contribution is much appreciated with a final score of 2!
You have received 40 (20 base + 20 weekly bonus) Sloth points for this contribution

Another weekly streak completed, well done @frolvanya! To keep your weekly streak and get another bonus make pull request next week! Looking forward to see you in race-of-sloths

What is the Race of Sloths

Race of Sloths is a friendly competition where you can participate in challenges and compete with other open-source contributors within your normal workflow

For contributors:

  • Tag @race-of-sloths inside your pull requests
  • Wait for the maintainer to review and score your pull request
  • Check out your position in the Leaderboard
  • Keep weekly and monthly streaks to reach higher positions
  • Boast your contributions with a dynamic picture of your Profile

For maintainers:

  • Score pull requests that participate in the Race of Sloths
  • Engage contributors with fair scoring and fast responses so they keep their streaks
  • Promote the Race to the point where the Race starts promoting you
  • Grow the community of your contributors

Feel free to check our website for additional details!

Bot commands
  • For contributors
    • Include a PR: @race-of-sloths include to enter the Race with your PR
  • For maintainers:
    • Invite contributor @race-of-sloths invite to invite the contributor to participate in a race or include it, if it's already a runner.
    • Assign points: @race-of-sloths score [1/2/3/5/8/13] to award points based on your assessment.
    • Reject this PR: @race-of-sloths exclude to send this PR back to the drawing board.
    • Exclude repo: @race-of-sloths pause to stop bot activity in this repo until @race-of-sloths unpause command is called

@@ -596,13 +596,13 @@ pub enum ShadowDataConsistencyError {
#[derive(Debug)]
pub enum DataMismatchReason {
/// ReadRPC returns success result and NEAR RPC returns success result but the results mismatch
ReadRpcSuccessNearRpcSuccess,
SuccessNearRpcSuccess,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes bad naming practice, but it can cause some issues with metrics if it relies on old name

Copy link
Member

@kobayurii kobayurii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks good. Left a couple of comments. Thanks

rpc-server/src/modules/blocks/mod.rs Outdated Show resolved Hide resolved
rpc-server/src/modules/queries/methods.rs Outdated Show resolved Hide resolved
rpc-server/src/modules/queries/methods.rs Outdated Show resolved Hide resolved
@frolvanya
Copy link
Collaborator Author

Overall it looks good. Left a couple of comments. Thanks

Fixed. Thanks for the review!

Copy link
Member

@kobayurii kobayurii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@frolvanya frolvanya merged commit fe2bc82 into near:database-new Aug 12, 2024
3 checks passed
@race-of-sloths
Copy link

🥁 Score it!

@kobayurii, please score the PR with @race-of-sloths score [1/2/3/5/8/13]. The contributor deserves it.
If no scoring is provided within 24 hours, this PR will be scored as 2 🦥

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.

3 participants