-
Notifications
You must be signed in to change notification settings - Fork 85
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: read error leads to message skip for blocking API #292
base: master
Are you sure you want to change the base?
Conversation
This seems to be an issue worth fixing. But I think this does not seem to compleatly fix the issue at least as I understand it. struct BlockyReader {
block_next_read: bool,
index: usize,
}
impl Read for BlockyReader {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
if self.block_next_read {
self.block_next_read = false;
Result::Err(Error::new(ErrorKind::WouldBlock, "Test Block"))
} else {
let read = HEARTBEAT_V2.get(self.index).ok_or(Error::new(
ErrorKind::UnexpectedEof,
"EOF",
));
buf[0] = *read?;
self.index += 1;
self.block_next_read = true;
Ok(1)
}
}
}
#[test]
fn test_read_error() {
let mut reader = PeekReader::new(BlockyReader {
block_next_read: true,
index: 0,
});
loop {
match read_v2_msg::<mavlink::common::MavMessage, _>(&mut reader) {
Ok((header, _)) => {
assert_eq!(header, crate::test_shared::COMMON_MSG_HEADER);
break;
},
Err(MessageReadError::Io(err)) if err.kind() == ErrorKind::WouldBlock => (),
Err(err) => panic!("{err}"),
}
}
} which fails. The problem seems to be that impl Read for BlockyReader {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
if self.block_next_read {
self.block_next_read = false;
Result::Err(Error::new(ErrorKind::WouldBlock, "Test Block"))
} else {
let read = HEARTBEAT_V2.get(self.index).ok_or(Error::new(
ErrorKind::UnexpectedEof,
"EOF",
));
buf[0] = *read?;
self.index += 1;
if self.index <= 1 {
self.block_next_read = true;
}
Ok(1)
}
}
} but if at any other position the message still gets trown out. |
Hi, thank you for response. Yeah, I've not tested case where reader can read less data than expect. I've modified |
f801bea seems to have broken the while !buf.is_empty() {
match this.read(buf) {
Ok(0) => break,
...
}
}
if !buf.is_empty() { Err(Error::READ_EXACT_EOF) } else { Ok(()) } |
Yeah, I've added case for zero check on read, and now seems tests with features listed in workflow work well. |
ISSUE: Read error leads to message skip. It happens due consuming STX byte of message, even if message parsing isn't completed yet.
GOAL: I have custom readers that could return WouldBlock IO error at any time and code shall interrupt message parsing and return to this later.