Tokio runtime inside crate code

⚓ Rust    📅 2025-11-04    👤 surdeus    👁️ 8      

surdeus

Hello !

I'm discovering and trying gpui-component. During my proof of concept project, I need to instantiate gitlab objects. I use async version of the gitlab object (as gpui-component use async and sync code freeze the ui).

I have an error when the gitlab async object is created:

thread 'main' panicked at src/xxx/xxx.rs:xx:xx:
there is no reactor running, must be called from the context of a Tokio 1.x runtime

And, I can reproduce with a minimal code:

[package]
name = "demo"
version = "0.1.0"
edition = "2021"

[dependencies]
gpui = "*"
gpui-component = "*"
anyhow = "*"
tokio = { version = "1.48.0" }
use gpui::*;
use gpui_component::*;

pub struct Example;
impl Render for Example {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div().child("Hello world")
    }
}

fn main() {
    let app = Application::new();

    app.run(move |cx| {
        gpui_component::init(cx);

        cx.spawn(async move |cx| {
            cx.open_window(WindowOptions::default(), |window, cx| {
                let view = cx.new(|_| Example);
                cx.spawn(async move |_cx| {
                    // There is the code extracted from gitlab (and used crates code) crate (in my original code)
                    tokio::task::spawn_blocking(move || {
                        //
                    });
                })
                .detach();
                cx.new(|cx| Root::new(view.into(), window, cx))
            })?;

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

I'm not very familiar with async in rust. I have done a lot of async in Python, so I understand the basics. But, I don't know yet how a code from a crate (here tokio::task::spawn_blocking) can share the "loop" (Python world naming, here it seems "reactor") with another crate, here gpui-component.

Also, I'm not sure about how use gpui-component, as its young crate with almost no doc.

I'm open to any help :slight_smile:

Thanks !

4 posts - 3 participants

Read full topic

🏷️ Rust_feed