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(providers): remove locks on requests #7156

Merged
merged 5 commits into from
Feb 17, 2024

Conversation

klkvr
Copy link
Member

@klkvr klkvr commented Feb 17, 2024

Motivation

Closes #7131

We are basically blocking all async requests through provider rn by aquiring write lock before making a request.

Solution

Use read lock

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

nit

InnerTransport::Http(http) => http.call(req),
InnerTransport::Ws(ws) => ws.call(req),
InnerTransport::Ipc(ipc) => ipc.call(req),
match this.inner.read().await.as_ref().unwrap().clone() {
Copy link
Member

Choose a reason for hiding this comment

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

we're acquiring this twice now we could do the this combined in an if let else

by checking this.inner.read().await.cloned()

also the write check still has a race condition, so we also need to check for is_none there again

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we even need to clone since Service is implemented for &Http and &Pubsub

Copy link
Member Author

Choose a reason for hiding this comment

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

ah right, it works as long as we can obtain &mut &T

match this.inner.read().await.as_ref().unwrap() {
    InnerTransport::Http(http) => {
        let mut http = http;
        http.call(req)
    },
}

is there a cleaner way to do this without that strange let?

Copy link
Member

Choose a reason for hiding this comment

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

Don't think so

Copy link
Member Author

Choose a reason for hiding this comment

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

refactored it, now we don't clone and check second time after aquiring write lock

we can't use if let some there anymore I believe because we don't clone

Copy link
Member

Choose a reason for hiding this comment

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

can do InnerTransport::Http(mut http)?

Copy link
Member

@DaniPopes DaniPopes Feb 20, 2024

Choose a reason for hiding this comment

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

can do InnerTransport::Http(mut http)?

mut http removes the reference

error[E0507]: cannot move out of a shared reference
   --> crates/common/src/provider/runtime_transport.rs:228:19
    |
228 |             match inner.as_ref().expect("must've been initialized") {
    |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
229 |                 InnerTransport::Http(mut http) => http.call(req),
    |                                      --------
    |                                      |
    |                                      data moved here
    |                                      move occurs because `http` has type `alloy_transport_http::Http<reqwest::Client>`, which does not implement the `Copy` trait
    |
help: consider borrowing the pattern binding
    |
229 |                 InnerTransport::Http(ref mut http) => http.call(req),
    |                                      +++

And ref mut creates &mut T when we want &mut &T. This looks like a weird case that's not covered by patterns ig

crates/common/src/provider/runtime_transport.rs Outdated Show resolved Hide resolved
Copy link
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

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

Amazing catch

crates/common/src/provider/runtime_transport.rs Outdated Show resolved Hide resolved
@mattsse mattsse merged commit c631cf3 into foundry-rs:master Feb 17, 2024
19 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.

Performance regression between 2bcb4a1 and 293fad7
5 participants