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
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions crates/common/src/provider/runtime_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,11 @@ impl RuntimeTransport {
*inner = Some(this.connect().await.map_err(TransportErrorKind::custom)?)
}

let mut inner = this.inner.write().await;
// SAFETY: We just checked that the inner transport exists.
let inner_mut = inner.as_mut().expect("We should have an inner transport.");

match inner_mut {
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

InnerTransport::Http(mut http) => http.call(req),
InnerTransport::Ws(mut ws) => ws.call(req),
InnerTransport::Ipc(mut ipc) => ipc.call(req),
}
.await
})
Expand Down
Loading