Skip to content

Sync method + tokio::spawn fails to locate runtime #2811

@pronebird

Description

@pronebird

Hi,

I initialize runtime by calling a rust method that inits runtime and puts it into static OnceLock:

static TOKIO_RUNTIME: OnceLock<Runtime> = OnceLock::new();

/// Initialize tokio runtime once before interacting with nym-vpn-lib.
/// Repeat calls do nothing.
#[allow(non_snake_case)]
#[uniffi::export]
pub fn initializeTokioRuntime() {
    let _rt = TOKIO_RUNTIME.get_or_init(|| {
        tokio::runtime::Builder::new_multi_thread()
            .worker_threads(10)
            .enable_all()
            .build()
            .expect("failed to initialize tokio runtime")
    });
}

However some of the calls result in:

rustPanic("there is no reactor running, must be called from the context of a Tokio 1.x runtime")

This impacts our application. After migrating to uniffi objects and async_runtime, nothing really works anymore. How can I share the same runtime across all threads in Swift?

It seems that the issue manifests itself particularly when using Swift actors which use cooperative queue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions