Skip to content

Commit

Permalink
fix: continue reading ipc on large data (alloy-rs#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
publicqi authored Jun 22, 2024
1 parent 5eadbf1 commit 61921bc
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions crates/transport-ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,6 @@ impl<T: AsyncRead> futures::stream::Stream for ReadJsonStream<T> {
"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() {
Expand Down Expand Up @@ -254,4 +248,36 @@ mod tests {
let obj = reader.next().await;
assert!(obj.is_none());
}

#[tokio::test]
async fn test_large_valid() {
let header = b"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x";
let filling_zeros = header
.iter()
.chain(vec![b'0'; CAPACITY - header.len()].iter())
.copied()
.collect::<Vec<_>>();

let first_page = filling_zeros.as_ref();
let second_page = b"\"}";

let mock = tokio_test::io::Builder::new()
// partial object
.read(first_page)
// trigger pending read
.wait(std::time::Duration::from_millis(1))
// complete object
.read(second_page)
.build();

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

0 comments on commit 61921bc

Please sign in to comment.