diff --git a/ofborg/src/asynccmd.rs b/ofborg/src/asynccmd.rs index a6c29fe..b457d4a 100644 --- a/ofborg/src/asynccmd.rs +++ b/ofborg/src/asynccmd.rs @@ -122,48 +122,7 @@ impl AsyncCmd { child_wait(WaitTarget::Child, monitor_tx, child), ); - let head_waiter = thread::spawn(move || { - let mut return_status: Option> = None; - - for (id, interior_result) in monitor_rx.iter() { - match waiters.remove(&id) { - Some(handle) => { - info!("Received notice that {:?} finished", id); - let waiter_result = handle.join(); - - info!("waiter status: {:?}", waiter_result); - info!("interior status: {:?}", interior_result); - - match interior_result { - WaitResult::Thread(t) => { - debug!("thread result: {:?}", t); - } - WaitResult::Process(t) => { - return_status = Some(t); - } - } - } - None => { - error!( - "Received notice that {:?} finished, but it isn't being waited on?", - id - ); - } - } - - if waiters.is_empty() { - debug!("Closing up the waiter receiver thread, no more waiters."); - break; - } - } - - info!( - "Out of the child waiter recv, with {:?} remaining waits", - waiters.len() - ); - - return_status - }); + let head_waiter = thread::spawn(move || block_on_waiters(monitor_rx, waiters)); SpawnedAsyncCmd { waiter: head_waiter, @@ -190,6 +149,52 @@ impl SpawnedAsyncCmd { } } +fn block_on_waiters( + monitor_rx: mpsc::Receiver<(WaitTarget, WaitResult<()>)>, + mut waiters: HashMap>, +) -> Option> { + let mut status = None; + + for (id, interior_result) in monitor_rx.iter() { + match waiters.remove(&id) { + Some(handle) => { + info!("Received notice that {:?} finished", id); + let waiter_result = handle.join(); + + info!("waiter status: {:?}", waiter_result); + info!("interior status: {:?}", interior_result); + + match interior_result { + WaitResult::Thread(t) => { + debug!("thread result: {:?}", t); + } + WaitResult::Process(t) => { + status = Some(t); + } + } + } + None => { + error!( + "Received notice that {:?} finished, but it isn't being waited on?", + id + ); + } + } + + if waiters.is_empty() { + debug!("Closing up the waiter receiver thread, no more waiters."); + break; + } + } + + info!( + "Out of the child waiter recv, with {:?} remaining waits", + waiters.len() + ); + + status +} + #[cfg(test)] mod tests { use super::AsyncCmd;