Skip to content
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

Using IpcSender<T> as a field and implementing Deserialize/Serialize causes panic. #238

Open
gatoWololo opened this issue Jul 18, 2019 · 1 comment

Comments

@gatoWololo
Copy link

@asajeffrey

With the latest ipc-channel commit, the following code panics when ran:

// main.rs
use serde::{Serialize, Deserialize};
use ipc_channel::ipc;

#[derive(Serialize, Deserialize, Debug)]
struct IpcWrapper<T> {
    s: ipc::IpcSender<T>
}

fn main() {
    let (sender, receiver) = ipc::channel::<i32>().unwrap();
    let wrapper = IpcWrapper { s: sender };

    let ser = serde_json::to_string(&wrapper).unwrap();
    let deser: IpcWrapper<i32> = serde_json::from_str(&ser).unwrap();
}

Error: thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0'

Cargo.toml:

[dependencies]
ipc-channel = {git = "https://github.com/servo/ipc-channel"}
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

All you need is to "wrap" IpcSender<T> in your own type and derive Serialize/Deserialize. The error happens during deserialization.

From the backtrace. The error is happening here:

fn deserialize_os_ipc_sender<'de, D>(deserializer: D)
                                -> Result<OsIpcSender, D::Error> where D: Deserializer<'de> {
    let index: usize = Deserialize::deserialize(deserializer)?;
    OS_IPC_CHANNELS_FOR_DESERIALIZATION.with(|os_ipc_channels_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.
        Ok(os_ipc_channels_for_deserialization.borrow_mut()[index].to_sender())
    })
}

I'm not sure why though.

@jdm
Copy link
Member

jdm commented Jul 22, 2019

I see the problem. The ipc channels expect that they will only be serialized as part of being sent over an IPC channel, so the code that prepares os_ipc_channels_for_deserialization to contain the expected values is never called because the deserialization does not occur as part of receiving on an IPC channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants