-
Notifications
You must be signed in to change notification settings - Fork 115
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
Encoding error doesn't show up in tutorial example #290
Comments
Seems this issues related to tokio-io, I think you can get some logs like |
@kamyuentse nope, I didn't get anything new output using |
@khaledkbadr Can you provide your code snippet and the current |
@kamyuentse this is my snippet main.rs extern crate bytes;
extern crate futures;
extern crate tokio_io;
extern crate tokio_proto;
extern crate tokio_service;
use tokio_proto::TcpServer;
use std::io;
use std::str;
use bytes::BytesMut;
use tokio_io::codec::{Decoder, Encoder};
use tokio_proto::pipeline::ServerProto;
use tokio_service::Service;
use futures::{future, Future};
struct LineCodec;
impl Decoder for LineCodec {
type Item = String;
type Error = io::Error;
fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<String>> {
if let Some(i) = buf.iter().position(|&b| b == b'\n') {
// remove the serialized frame from the buffer.
let line = buf.split_to(i);
// Also remove the '\n'
buf.split_to(1);
// Turn this data into a UTF string and return it in a Frame.
match str::from_utf8(&line) {
Ok(_) => Err(io::Error::new(io::ErrorKind::ConnectionAborted, "invalid UTF-8")),
Err(_) => Err(io::Error::new(io::ErrorKind::Other, "invalid UTF-8")),
}
} else {
Ok(None)
}
}
}
impl Encoder for LineCodec {
type Item = String;
type Error = io::Error;
fn encode(&mut self, msg: String, buf: &mut BytesMut) -> io::Result<()> {
buf.extend(msg.as_bytes());
buf.extend(b"\n");
Ok(())
}
}
struct LineProto;
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_io::codec::Framed;
impl<T: AsyncRead + AsyncWrite + 'static> ServerProto<T> for LineProto {
// For this protocol style, `Request` matches the `Item` type of the codec's `Decoder`
type Request = String;
// For this protocol style, `Response` matches the `Item` type of the codec's `Encoder`
type Response = String;
// A bit of boilerplate to hook in the codec:
type Transport = Framed<T, LineCodec>;
type BindTransport = Result<Self::Transport, io::Error>;
fn bind_transport(&self, io: T) -> Self::BindTransport {
Ok(io.framed(LineCodec))
}
}
struct Echo;
impl Service for Echo {
// These types must match the corresponding protocol types:
type Request = String;
type Response = String;
// For non-streaming protocols, service errors are always io::Error
type Error = io::Error;
// The future for computing the response; box it for simplicity.
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
// Produce a future for computing a response from a request.
fn call(&self, req: Self::Request) -> Self::Future {
// In this case, the response is immediate.
Box::new(future::ok(req))
}
}
fn main() {
// Specify the localhost address
let addr = "0.0.0.0:12345".parse().unwrap();
// The builder requires a protocol and an address
let server = TcpServer::new(LineProto, addr);
// We provide a way to *instantiate* the service for each new
// connection; here, we just immediately return a new instance.
server.serve(|| Ok(Echo));
} But when I run the app with |
Guide to enable logger:
|
This is the logs result when I made the request that returned the error
|
Notice those lines: If succeed in decoding something, you can see the following pair:
Refer https://github.com/tokio-rs/tokio-io/blob/master/src/framed_read.rs#L274 for more detail. |
yup, I reached the same point debugging. And it didn't reach the second trace fn bind_transport(&self, io: T) -> Self::BindTransport {
Ok(io.framed(LineCodec))
} It always returns an |
If you return an error in |
the silently part, shouldn't it log the error message or return it. Or should I change something to allow that to happen? |
In my opinion, if your If you want to keep the incoming bytes, I think you can buffer them (do not consume those bytes in |
no no, I agree with you. it should close the connection alright (which I want). but in the example, the |
You can add Should we add |
Thanks for the report! Unfortunately I believe this is a bug with tokio-proto which at the framing level currently drops some errors on the floor. Is there an isolated tokio-core/tokio-io example though that exhibits this behavior? |
In the first example of Tokio's tutorial, the
decode
function returns an error if it has invalid UTF-8. I hacked the decode function a bit to return that error no matter what, but when I ran the server and simulated the error the connection was dropped (which I surely want) but that error showed up neither in thestderr
nor in the response.I don't really know if it's omitted in the example for simplicity but I went through documentation to find how an answer to how to show that error but couldn't.
The text was updated successfully, but these errors were encountered: