Spawned future dropped without executing?


#1

Consider the following code:

extern crate futures;
extern crate tokio;

use futures::future::{err, ok, Future};
use futures::stream::{once, Stream};
use tokio::executor::current_thread;

fn main() {
    current_thread::block_on_all(
        once::<(), ()>(Ok(()))
            .map(|()| {
                let fut = ok::<(), ()>(()).map(|()| println!("spawned future completed"));
                current_thread::spawn(fut);
            })
            .fold((), |_, _| if true { ok(()) } else { err(()) }),
    );
}

It’s a toy example, but it’s a boiled-down example of some real-world code that I have that listens for new incoming TLS connections and, for each one, spawns a future that handles the new connection.

The issue that I’m running into is that while the inner map executes and spawns fut, the future fut itself is dropped without executing. Unfortunately I haven’t managed to get a working example yet (if you run that code snippet, it will behave properly) because I’m not sure what parts of my code are the root cause, and I can’t post the code I’m working on publicly. But has anyone seen an issue like that or have a hunch as to what’s going on?

To be more concrete about the symptoms:

  • I expect the spawned future fut to execute because of the contract of block_on_all, which says that it only returns once there are no outstanding futures
  • My server continues running, but the TLS clients that connect report that their connections are immediately closed
  • In the debug logging from my server process, I can see that the future is getting dropped, as Tokio logs tokio_reactor: dropping I/O source: 1 at the same time as the client reports the connection has been closed

#2

Are you sure your background spawned futures that handle a connection aren’t encountering errors?


#3

Yep, turns out that’s exactly what it was - good call! I feel particularly dumb because I spent an hour in GDB convinced that I’d found a resource leak in tokio to find it :joy:


#4

:smile: This one seems a bit too vanilla to have been a leak (granted I don’t know the real code) but you never know in general …