Skip to content

Commit db1f5a5

Browse files
antrikemilio
authored andcommitted
platform/inprocess: Drop server record after accept(), not on connect()
Invoking `accept()` after `connect()` used to fail, because the server record was being dropped while doing the `connect()`. Now dropping it after the `accept()` instead -- which is safe for either invocation order, because `accept()` is blocking, and thus never finishes before `connect()` is done. Also adding a separate `server_connect_first` test case to explicitly test this situation. (And modifying the original test case to ensure it always tests the other situation.)
1 parent 7881984 commit db1f5a5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/platform/inprocess/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl OsIpcSender {
139139
}
140140

141141
pub fn connect(name: String) -> Result<OsIpcSender,MpscError> {
142-
let record = ONE_SHOT_SERVERS.lock().unwrap().remove(&name).unwrap();
142+
let record = ONE_SHOT_SERVERS.lock().unwrap().get(&name).unwrap().clone();
143143
record.connect();
144144
Ok(record.sender)
145145
}
@@ -285,6 +285,7 @@ impl OsIpcOneShotServer {
285285
{
286286
let record = ONE_SHOT_SERVERS.lock().unwrap().get(&self.name).unwrap().clone();
287287
record.accept();
288+
ONE_SHOT_SERVERS.lock().unwrap().remove(&self.name).unwrap();
288289
let receiver = self.receiver.borrow_mut().take().unwrap();
289290
let (data, channels, shmems) = receiver.recv().unwrap();
290291
Ok((receiver, data, channels, shmems))

src/platform/test.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,12 @@ fn receiver_set() {
397397

398398
#[cfg(not(any(feature = "force-inprocess", target_os = "android")))]
399399
#[test]
400-
fn server() {
400+
fn server_accept_first() {
401401
let (server, name) = OsIpcOneShotServer::new().unwrap();
402402
let data: &[u8] = b"1234567";
403403

404404
thread::spawn(move || {
405+
thread::sleep(Duration::from_millis(30));
405406
let tx = OsIpcSender::connect(name).unwrap();
406407
tx.send(data, vec![], vec![]).unwrap();
407408
});
@@ -413,6 +414,24 @@ fn server() {
413414
(data, vec![], vec![]));
414415
}
415416

417+
#[test]
418+
fn server_connect_first() {
419+
let (server, name) = OsIpcOneShotServer::new().unwrap();
420+
let data: &[u8] = b"1234567";
421+
422+
thread::spawn(move || {
423+
let tx = OsIpcSender::connect(name).unwrap();
424+
tx.send(data, vec![], vec![]).unwrap();
425+
});
426+
427+
thread::sleep(Duration::from_millis(30));
428+
let (_, mut received_data, received_channels, received_shared_memory_regions) =
429+
server.accept().unwrap();
430+
received_data.truncate(7);
431+
assert_eq!((&received_data[..], received_channels, received_shared_memory_regions),
432+
(data, vec![], vec![]));
433+
}
434+
416435
#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))]
417436
#[test]
418437
fn cross_process() {

0 commit comments

Comments
 (0)