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

fix: continue reading ipc on data error #605

Merged
merged 1 commit into from
Apr 23, 2024
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ http-body-util = "0.1"
tokio = "1"
tokio-util = "0.7"
tokio-stream = "0.1"
tokio-test = "0.4"
tower = { version = "0.4", features = ["util"] }

# tracing
Expand Down
3 changes: 3 additions & 0 deletions crates/transport-ipc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ interprocess = { version = "1.2.1", features = ["tokio", "tokio_support"] }
serde = { workspace = true, optional = true }
tempfile = { workspace = true, optional = true }

[dev-dependencies]
tokio-test.workspace = true

[features]
default = []
mock = ["dep:serde", "dep:tempfile"]
71 changes: 69 additions & 2 deletions crates/transport-ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ impl IpcBackend {
}
}

/// Default capacity for the IPC buffer.
const CAPACITY: usize = 4096;

/// A stream of JSON-RPC items, read from an [`AsyncRead`] stream.
#[derive(Debug)]
#[pin_project::pin_project]
Expand All @@ -127,7 +130,7 @@ where
T: AsyncRead,
{
fn new(reader: T) -> Self {
Self { reader: reader.compat(), buf: BytesMut::with_capacity(4096), drained: true }
Self { reader: reader.compat(), buf: BytesMut::with_capacity(CAPACITY), drained: true }
}
}

Expand Down Expand Up @@ -170,7 +173,21 @@ where
return Ready(Some(response));
}
Some(Err(err)) => {
if err.is_eof() {
if err.is_data() {
trace!(
buffer = %String::from_utf8_lossy(this.buf.as_ref()),
"IPC buffer contains invalid JSON data",
);

if this.buf.len() > CAPACITY {
// buffer is full, we can't decode any more
error!("IPC response too large to decode");
return Ready(None);
}

// this happens if the deserializer is unable to decode a partial object
*this.drained = true;
} else if err.is_eof() {
trace!("partial object in IPC buffer");
// nothing decoded
*this.drained = true;
Expand Down Expand Up @@ -213,3 +230,53 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::future::poll_fn;
use tokio_util::compat::TokioAsyncReadCompatExt;

#[tokio::test]
async fn test_partial_stream() {
let mock = tokio_test::io::Builder::new()
// partial object
.read(b"{\"jsonrpc\":\"2.0\",\"method\":\"eth_subscription\"")
// trigger pending read
.wait(std::time::Duration::from_millis(1))
// complete object
.read(r#", "params": {"subscription": "0xcd0c3e8af590364c09d0fa6a1210faf5", "result": {"difficulty": "0xd9263f42a87", "uncles": []}} }"#.as_bytes())
.build();

let mut reader = ReadJsonStream::new(mock.compat());
poll_fn(|cx| {
let res = reader.poll_next_unpin(cx);
assert!(res.is_pending());
Ready(())
})
.await;
let _obj = reader.next().await.unwrap();
}

#[tokio::test]
async fn test_large_invalid() {
let mock = tokio_test::io::Builder::new()
// partial object
.read(b"{\"jsonrpc\":\"2.0\",\"method\":\"eth_subscription\"")
// trigger pending read
.wait(std::time::Duration::from_millis(1))
// complete object
.read(vec![b"a"[0]; CAPACITY].as_ref())
Copy link
Member

Choose a reason for hiding this comment

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

b'a' 🐱

.build();

let mut reader = ReadJsonStream::new(mock.compat());
poll_fn(|cx| {
let res = reader.poll_next_unpin(cx);
assert!(res.is_pending());
Ready(())
})
.await;
let obj = reader.next().await;
assert!(obj.is_none());
}
}
Loading