diff --git a/src/platform/inprocess/mod.rs b/src/platform/inprocess/mod.rs index 3fcbd6c72..d447dbcec 100644 --- a/src/platform/inprocess/mod.rs +++ b/src/platform/inprocess/mod.rs @@ -22,10 +22,11 @@ use std::usize; use uuid::Uuid; +#[derive(Clone)] struct ServerRecord { sender: OsIpcSender, conn_sender: mpsc::Sender, - conn_receiver: Mutex>, + conn_receiver: Arc>>, } impl ServerRecord { @@ -34,7 +35,7 @@ impl ServerRecord { ServerRecord { sender: sender, conn_sender: tx, - conn_receiver: Mutex::new(rx), + conn_receiver: Arc::new(Mutex::new(rx)), } } @@ -138,7 +139,7 @@ impl OsIpcSender { } pub fn connect(name: String) -> Result { - let record = ONE_SHOT_SERVERS.lock().unwrap().remove(&name).unwrap(); + let record = ONE_SHOT_SERVERS.lock().unwrap().get(&name).unwrap().clone(); record.connect(); Ok(record.sender) } @@ -282,7 +283,9 @@ impl OsIpcOneShotServer { Vec, Vec),MpscError> { - ONE_SHOT_SERVERS.lock().unwrap().get(&self.name).unwrap().accept(); + let record = ONE_SHOT_SERVERS.lock().unwrap().get(&self.name).unwrap().clone(); + record.accept(); + ONE_SHOT_SERVERS.lock().unwrap().remove(&self.name).unwrap(); let receiver = self.receiver.borrow_mut().take().unwrap(); let (data, channels, shmems) = receiver.recv().unwrap(); Ok((receiver, data, channels, shmems)) diff --git a/src/platform/test.rs b/src/platform/test.rs index f376da69d..9681d61ad 100644 --- a/src/platform/test.rs +++ b/src/platform/test.rs @@ -15,7 +15,6 @@ use std::thread; #[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))] use libc; -#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))] use platform::{OsIpcSender, OsIpcOneShotServer}; #[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))] use test::{fork, Wait}; @@ -396,10 +395,27 @@ fn receiver_set() { } } -#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))] +#[cfg(not(any(feature = "force-inprocess", target_os = "android")))] +#[test] +fn server_accept_first() { + let (server, name) = OsIpcOneShotServer::new().unwrap(); + let data: &[u8] = b"1234567"; + + thread::spawn(move || { + thread::sleep(Duration::from_millis(30)); + let tx = OsIpcSender::connect(name).unwrap(); + tx.send(data, vec![], vec![]).unwrap(); + }); + + let (_, mut received_data, received_channels, received_shared_memory_regions) = + server.accept().unwrap(); + received_data.truncate(7); + assert_eq!((&received_data[..], received_channels, received_shared_memory_regions), + (data, vec![], vec![])); +} + #[test] -//XXXjdm This hangs indefinitely with inprocess impl and warrants further investigation. -fn server() { +fn server_connect_first() { let (server, name) = OsIpcOneShotServer::new().unwrap(); let data: &[u8] = b"1234567"; @@ -408,6 +424,7 @@ fn server() { tx.send(data, vec![], vec![]).unwrap(); }); + thread::sleep(Duration::from_millis(30)); let (_, mut received_data, received_channels, received_shared_memory_regions) = server.accept().unwrap(); received_data.truncate(7);