tokio: Failed to spawn tasks in dynamic library in plugins system
Version
tokio = { version = “0.2.4”, features = [“full”] }
Platform
macOS, v10.15.1
Subcrates
runtime
Description
- Make a crate with crate-type = [“dylib”]
- Export a function return an async function with tokio::spawn call in it.
- Load the dylib in another crate and call the exported function.
- Exception raised as below:
thread '<unnamed>' panicked at 'must be called from the context of Tokio runtime configured with either `basic_scheduler` or `threaded_scheduler`', /Users/gary/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/global.rs:50:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
If switch the runtime to async-std and call async_std::task::spawn, it works fine.
Please check test project: https://github.com/garyhai/async-plugins-test.git
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 17 (4 by maintainers)
Looking into it, it does look like you would need to instantiate a runtime… probably statically as I suggested above. The dylib isolates its symbols so it cannot access the runtime from the caller. This will result in each plugin having its own runtime.
I’m running into the same issue.
I’ve noticed that if I print
std::thread::current(), theidis the same, but thethread_nameisSome(“main”)vsNonewhen inside the loaded plugin code.