"cycle detected when type-checking" when using `tokio::spawn`


use std::time::{Instant, Duration};

struct Hello {}

impl Hello {
    async fn one(&self) {
        let mut interval = tokio::time::interval(Duration::from_millis(10));
        loop {
            // the calls to rand::random represent some network task
            let r1 = rand::random();
            let r2 = rand::random();
            let r3 = rand::random();
            if r1 && r2 {
                println!("first condition");
            if r2 && r3 {
                println!("second condition");
    async fn two(&self) {
        // some asynchronous tasks are executed here...
        tokio::spawn(async move {


  1. In the above code, the calls to rand::random represent some network tasks
  2. the method one will (always) eventually return, either by calling the method two or otherwise(some base case).
  3. the method two will always execute method one within tokio::spawn.

When I try to run this, I receive an error

error[E0391]: cycle detected when type-checking `<impl at src/lib.rs:5:1: 32:2>::two

(this is just the first line of the error message). I am unable to figure out the way to solve it. Can anyone please help?
Thanks. :slight_smile:

The easiest is to spawn it from a non async function.

Thanks, Alice. That works. But i am not able to understand the reason for this error. I have seen tokio::spawn calls being made from within async functions before. So what makes this different?

They both call each other.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.