Mongodb and tokio : there is no timer running, must be called from the context of Tokio runtime

Hi,

i have tried the async example from

result:
there is no timer running, must be called from the context of Tokio runtime

content of cargo.toml file

mongodb = { version = "1.1.1" }
#for async
tokio = {version = "0.3.6", features = ["full"]}

According doc tokio is used as default async.

to get the example working i need to use async-std-runtime

[dependencies]
mongodb = { version = "1.1.1", default-features = false, features = ["async-std-runtime"] }
#for async
tokio = {version = "0.3.6", features = ["full"]}

Any idea why?

Kr

The mongodb crate depends on Tokio 0.2, but you are using Tokio 0.3. This panic can occur if you try to use a timer from Tokio 0.2 on a Tokio 0.3 runtime.

It works when you enabled async-std because, unlike Tokio runtimes, the async-std runtime is silently started in the background on first use.

Hi Alice,

thanks for the update. Changing tokio version to 0.2 makes it working.
I see that in the cargo.toml file from mongo this
[dependencies.tokio]
version = "0.2.18"
features = ["io-util", "sync", "macros"]

So why do i need to add the tokio dependency in my cargo.toml file for making the main function async?

10 | #[tokio::main]
| ^^^^^ use of undeclared crate or module tokio

I guess not all features of tokio are included and needed for mongodb. So it seems you need to find it back. What if 2 crate are using different tokio versions?

I'm quite new to RUST and don't know all the rules about crate dependencies.

The only dependencies that are available in your own code are those you directly mention in your Cargo.toml. Just because mongodb imports the dependency doesn't mean you can use it directly.

As for Tokio versions, there are some limitied ways to use both Tokio 0.2 and 0.3 in one application, but generally it is not easy to do so.

3 Likes

ok, clear, seems compatibility issue
thanks a lot for the quick replies

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.