paritytech / jsonrpsee

Rust JSON-RPC library on top of async/await
MIT License
636 stars 170 forks source link

client: support other async runtimes than `tokio` #1335

Open gcp-development opened 6 months ago

gcp-development commented 6 months ago

Hello,

I am having this weird error a due the use of tokio out of context for the "WsTransportClientBuilder".

thread 'main' panicked at /.cargo/git/checkouts/jsonrpsee-b50257996090af44/658afa5/client/transport/src/ws/mod.rs:480:19:
there is no reactor running, must be called from the context of a Tokio 1.x runtime
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Is this because of the async_io ?

fn main() -> anyhow::Result<()> {
    esp_idf_svc::sys::link_patches();
    esp_idf_svc::log::EspLogger::initialize_default();

    let mut root_store = rustls::RootCertStore::empty();
    root_store.roots = webpki_roots::TLS_SERVER_ROOTS.iter().cloned().collect();
    let app_config = CONFIG;

    async_io::block_on(async move {
        let _wifi = connect_wifi(app_config.wifi_ssid, app_config.wifi_psk);
        let (sender, receiver) = WsTransportClientBuilder::default()
            .use_webpki_rustls()
            .build(Url::parse("wss://rococo-contracts-rpc.polkadot.io:443").unwrap())
            .await.expect("TODO: panic message");

        Result::<_, anyhow::Error>::Ok(())
    });

    Ok(())
}

cargo.toml

(...)
jsonrpsee = { version = "0.22.3", default-features = false, features = ["client-ws-transport-webpki-tls"] }
async-io = "2.3.2"
(...)
niklasad1 commented 6 months ago

Is this because of the async_io ?

Yes, jsonrpsee is tightly-coupled to tokio and requires the tokio runtime to be initialized because it's using tokio::spawn under the hood to spawn the background task.

Can't use tokio on your platform or what's the reason to use async-io crate?

gcp-development commented 6 months ago

Hi @niklasad1,

Thank you for your answer.

when you work in embedded systems "every little byte helps". Tokio is a very good crate but is too fat when compared with async-io.

For instance for this case above(STD/ESP-IDF framework) we would need to use OpenThread with unsafe code instructions like "esp_vfs_eventfd_register()". And we would need to reorganize the partition-tables in order to work with Tokio.

Cheers,