Prevent future from being stopped by calling forget on handle

This commit is contained in:
Daniel Karzel 2021-02-16 15:19:55 +11:00
parent 77ec7e502f
commit 2ced9ddba4

View File

@ -20,8 +20,8 @@ use libp2p::{
}; };
use rand::rngs::OsRng; use rand::rngs::OsRng;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::{broadcast, mpsc}; use tokio::sync::{broadcast, mpsc, mpsc::error::SendError};
use tracing::{debug, error, trace, warn}; use tracing::{debug, error, trace};
use uuid::Uuid; use uuid::Uuid;
// TODO: Use dynamic // TODO: Use dynamic
@ -246,14 +246,16 @@ impl EventLoop {
.build() .build()
.await?; .await?;
let (remote, remote_handle) = alice::run(swap).remote_handle(); let (swap, swap_handle) = alice::run(swap).remote_handle();
tokio::spawn(remote); tokio::spawn(swap);
let _ = self // For testing purposes the handle is currently sent via a channel so we can
.swap_handle_sender // await it. If a remote handle is dropped, the future of the swap is
.send(remote_handle) // also stopped. If we error upon sending the handle through the channel
.await // we have to call forget to detach the handle from the swap future.
.map_err(|err| warn!("Could not send swap handle over channel: {:?}", err)); if let Err(SendError(handle)) = self.swap_handle_sender.send(swap_handle).await {
handle.forget();
}
Ok(()) Ok(())
} }