-
Notifications
You must be signed in to change notification settings - Fork 589
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
Check runtime dynamically #1135
Conversation
63250ab
to
905badc
Compare
This also solves #1056 as I tested. |
crates/revm/src/db/ethersdb.rs
Outdated
match Handle::try_current() { | ||
Ok(handle) => handle.block_on(f), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will still panic if called from the main Runtime::block_on thread.
what we want here is
tokio::task::block_in_place(move || {
handle.block_on(f)
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I just noticed the problem. Actually, I think it looks impossible to run a future in a non-async function within a tokio runtime (?.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I tried this works indeed. But block_in_place
panic for new_current_thread
therefore we have to handle that too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I investigated a bit. It looks like we can't support current_thread
due to not being able to blocking_in_place
without changing the signature of the Database(Ref)
traits. I have two options here:
- Document clearly that,
EthersDB
can't be used withcurrent_thread
. - Less preferably, we start another thread and create another runtime to do it.
Which one do you prefer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done with 2 so we can support all use cases, could you have a look? @mattsse
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is issue related to this, not sure if you have looked at it: #1056
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tested this addresses #1056 cleanly.
897e2ae
to
03a0463
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work
Thanks! |
* Check runtime dynamically * Drop futures as clippy suggests * Fix panic * Format * Support current_thread runtime * Revert unnecessary changes * Add a few documents * derive Clone thanks to removing runtime field
This PR removes the improper initialization of the runtime.
Explanation:
Users may create an
EthersDB
in a sync context but later use it in an async environment and vice versa. In this case, two runtimes will conflict. In addition, the current code, by default, creates a multithreaded runtime, which could be unexpected for users. For example, users may want to run many instances in parallel with separate cores pinned but the runtime created could interfere with this design.Therefore, reasonable behavior is always checking if we are in an
async
environment byHandle::try_current.
If we are inasync
, we will pick the runtime created by users. Or else, we simply create acurrent_thread
runtime to get things done. Note this is the recommended way by tokio docs.For
current_thread
runtime, a new thread is spawned to complete the futures astokio
doesn't allow us toblock_in_place
, which essentially doesspawn_blocking
. This could be removed once we have aasync
equivalent trait.No. Because the current implementation only has concurrency but not parallelism. Specifically, see:
Either a
current_thread
or amulti_thread
runtime will execute the three rpc calls one by one. Refer to tokio::join! documents for details.No, as explained above.