Skip to content

Propagate deserialization errors for IpcSharedMemory #392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::platform::{
};

use bincode;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use std::cell::RefCell;
use std::cmp::min;
use std::error::Error as StdError;
Expand Down Expand Up @@ -579,13 +579,15 @@ impl<'de> Deserialize<'de> for IpcSharedMemory {

let os_shared_memory = OS_IPC_SHARED_MEMORY_REGIONS_FOR_DESERIALIZATION.with(
|os_ipc_shared_memory_regions_for_deserialization| {
// FIXME(pcwalton): This could panic if the data was corrupt and the index was out
// of bounds. We should return an `Err` result instead.
os_ipc_shared_memory_regions_for_deserialization.borrow_mut()[index]
.take()
.unwrap()
let mut regions = os_ipc_shared_memory_regions_for_deserialization.borrow_mut();
let Some(region) = regions.get_mut(index) else {
return Err(format!("Cannot consume shared memory region at index {index}, there are only {} regions available", regions.len()));
};

region.take().ok_or_else(|| format!("Shared memory region {index} has already been consumed"))
},
);
).map_err(D::Error::custom)?;

Ok(IpcSharedMemory {
os_shared_memory: Some(os_shared_memory),
})
Expand Down
5 changes: 4 additions & 1 deletion src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ impl RouterProxy {
// Router proxy takes both sending ends.
let (msg_sender, msg_receiver) = crossbeam_channel::unbounded();
let (wakeup_sender, wakeup_receiver) = ipc::channel().unwrap();
thread::spawn(move || Router::new(msg_receiver, wakeup_receiver).run());
thread::Builder::new()
.name("router-proxy".to_string())
.spawn(move || Router::new(msg_receiver, wakeup_receiver).run())
.expect("Failed to spawn router proxy thread");
Comment on lines +50 to +53
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave the router proxy thread a name to make it easier to identify when it panics.

RouterProxy {
comm: Mutex::new(RouterProxyComm {
msg_sender,
Expand Down