canndrew / netsim

Network simulation in Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't run my own Tokio reactor in netsim

povilasb opened this issue · comments

So with the latest tokio integrated (#25) now it won't allow to spawn another tokio reactor when netsim is running. So smth like this is not possible:

use futures::future;
use netsim::{Network, node, Ipv4Range};
use tokio::runtime::current_thread::Runtime;
use unwrap::unwrap;

fn main() {
    let network = Network::new();
    let network_handle = network.handle();

    let node1 = node::ipv4::machine(move |ip| {
        println!("node1: {}", ip);

        let fut = future::ok::<_, ()>(());
        let mut evloop = unwrap!(Runtime::new());
        unwrap!(evloop.block_on(fut));

        future::ok(())
    });

    let fut = future::lazy(move || {
        let (spawn_complete, _ip_plug) = network_handle.spawn_ipv4_tree(Ipv4Range::global(), node1);
        spawn_complete
    });
    let mut evloop = unwrap!(Runtime::new());
    unwrap!(evloop.block_on(fut));
}

and results with an error:

thread '<unnamed>' panicked at 'Multiple executors at once: EnterError { reason: "attempted to run an executor while another executor is already running" }', src/libcore/result.rs:997:5

In most cases when we have futures code this should not be a problem. Although, I can see 2 cases where it will be:

  1. when we have a piece of code that internally runs tokio reactor;
  2. when we have futures that do not implement Send trait.

The first thing we could do here is at least document the situation.
Another thing to consider is to provide a netsim machine node that runs on a forked process instead of a thread.