tomusdrw / rust-web3

Ethereum JSON-RPC multi-transport client. Rust implementation of web3 library. ENS address: rust-web3.eth
MIT License
1.45k stars 465 forks source link

Requests randomly hang and never return when using `futures::executor::block_on` and `futures::future::join_all` #634

Closed Bobface closed 2 years ago

Bobface commented 2 years ago

When making requests in quick succession in combination with futures::executor::block_on, it can happen that they never return and infinitely block.

Example code:

#[tokio::main]
async fn main() {
    let web3 = web3::Web3::new(web3::transports::Http::new("http://localhost:8545").unwrap());
    loop {
        // Get the block number
        println!("requesting");
        futures::executor::block_on(futures::future::join_all(vec![web3.eth().block_number()]));
        println!("done");
        // Sleep for 10 ms
        thread::sleep(time::Duration::from_millis(10));
    }
}

For me, after a short period of time the last requesting is printed and the output never continues. When I replace the futures::executor::block_on with .await, this never seems to happen.

elpiel commented 2 years ago

block_on should be used only if you're running the requests without an executor. In your case do not run block_on with tokio::main.

Bobface commented 2 years ago

Thanks for the info, I've also figured that out in the meantime!