Skip to content

Commit 4f8750c

Browse files
authored
Propagate deserialization errors for IpcSharedMemory (#392)
* Fail serialization if shmem region index is out of bounds Signed-off-by: Simon Wülker <[email protected]> * Give the router proxy thread a name This makes debugging easier when panics occur inside the thread. Signed-off-by: Simon Wülker <[email protected]> --------- Signed-off-by: Simon Wülker <[email protected]>
1 parent 82f6c49 commit 4f8750c

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/ipc.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::platform::{
1313
};
1414

1515
use bincode;
16-
use serde::{Deserialize, Deserializer, Serialize, Serializer};
16+
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
1717
use std::cell::RefCell;
1818
use std::cmp::min;
1919
use std::error::Error as StdError;
@@ -579,13 +579,15 @@ impl<'de> Deserialize<'de> for IpcSharedMemory {
579579

580580
let os_shared_memory = OS_IPC_SHARED_MEMORY_REGIONS_FOR_DESERIALIZATION.with(
581581
|os_ipc_shared_memory_regions_for_deserialization| {
582-
// FIXME(pcwalton): This could panic if the data was corrupt and the index was out
583-
// of bounds. We should return an `Err` result instead.
584-
os_ipc_shared_memory_regions_for_deserialization.borrow_mut()[index]
585-
.take()
586-
.unwrap()
582+
let mut regions = os_ipc_shared_memory_regions_for_deserialization.borrow_mut();
583+
let Some(region) = regions.get_mut(index) else {
584+
return Err(format!("Cannot consume shared memory region at index {index}, there are only {} regions available", regions.len()));
585+
};
586+
587+
region.take().ok_or_else(|| format!("Shared memory region {index} has already been consumed"))
587588
},
588-
);
589+
).map_err(D::Error::custom)?;
590+
589591
Ok(IpcSharedMemory {
590592
os_shared_memory: Some(os_shared_memory),
591593
})

src/router.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ impl RouterProxy {
4747
// Router proxy takes both sending ends.
4848
let (msg_sender, msg_receiver) = crossbeam_channel::unbounded();
4949
let (wakeup_sender, wakeup_receiver) = ipc::channel().unwrap();
50-
thread::spawn(move || Router::new(msg_receiver, wakeup_receiver).run());
50+
thread::Builder::new()
51+
.name("router-proxy".to_string())
52+
.spawn(move || Router::new(msg_receiver, wakeup_receiver).run())
53+
.expect("Failed to spawn router proxy thread");
5154
RouterProxy {
5255
comm: Mutex::new(RouterProxyComm {
5356
msg_sender,

0 commit comments

Comments
 (0)