Buffered reader line iterator hangs with no errors

I have a TCP server that tries to replicate a Redis client, which responds to

~ ➤ echo -ne '*1rn$4rnPINGrn' | nc localhost 6379                                                                                                                                                                                                          
PONG%   

correctly.

When I do

redis-cli PING

there is no response.

Step by step,

  1. I execute the command.
  2. Line 16, Tokio spawns a task for handling the stream.
  3. Line 38, recv.lines() can be iterated over with no problem.
  4. We start iterating, line is equal to (after each loop)
  • *1
  • $4 indicating that next commands length is 4 characters.
  • PING indicating the command.
  1. Loop is over, and it hangs, with no errors and no response at all? Breakpoint at line 54 match command never gets hit and task suddenly hangs after iterating all lines in loop.
  2. Redis hangs because there is literally no response.
  3. Tokio console shows the task as never yielded (didnt include the tracer code).
  4. CTRL+C redis-cli PING command.
  5. Breakpoint at line 54 gets hit! So it hangs until I SIGSEGV the redis-cli?

??

main file:

mod commands;
use std::{
    io::{BufRead, BufReader, BufWriter, Read, Write},
    net::SocketAddr,
    net::{TcpListener, TcpStream},
};

use commands::{RedisCommand, RedisCommandResponse};

#[tokio::main]
pub async fn main() {
    let listener = TcpListener::bind("127.0.0.1:6379").unwrap();
    for stream in listener.incoming() {
        match stream {
            Ok(mut _stream) => {
                tokio::task::spawn(handle_stream(_stream.try_clone().unwrap()));
            }
            Err(e) => {
                println!("error: {}", e);
            }
        }
    }
}

#[tracing::instrument]
async fn handle_stream(_stream: TcpStream) {
    // 0 => type of the input coming in
    // 1 => length of the first parameter
    // 2 => first parameter, command.
    // 3 => length of the second parameter
    // 4 => second paramter, if exists.
    let mut n: u8 = 0;
    let mut command: RedisCommand = RedisCommand::UNKNOWN;
    let mut out: String = String::new();
    let recv = BufReader::new(&_stream);
    let mut deb_buf: String = String::new();
    println!("{}", deb_buf);
    for l in recv.lines() {
        let line: String = match l {
            Ok(line) => line,
            Err(error) => {
                write_string_to_stream(format!("Invalid line: {}", error), _stream);
                return;
            }
        };
        if n == 4 && command == RedisCommand::ECHO {
            out = line.clone();
        }
        if let Ok(_cmd) = RedisCommand::from_str(&line) {
            command = _cmd;
        }
        n += 1;
    }
    match command {
        RedisCommand::UNKNOWN => write_string_to_stream(String::from("Invalid command"), _stream),
        RedisCommand::ECHO => write_string_to_stream(out, _stream),
        RedisCommand::PING => write_string_to_stream(String::from("PONG"), _stream),
    }
}

fn write_string_to_stream(out: String, _stream: TcpStream) {
    let mut writer = BufWriter::new(&_stream);
    match writer.write_all(out.as_bytes()) {
        Ok(_) => {}
        Err(_) => {
            _stream.shutdown(std::net::Shutdown::Both);
            return;
        }
    };
}

and the commands:

use std::cmp::PartialEq;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum RedisCommandError {
    #[error("invalid command")]
    Invalid(),
    #[error("unknown command")]
    Unknown,
}

#[derive(Error, Debug)]
pub enum RedisResponseCommandError {
    #[error("invalid response command")]
    Invalid(),
    #[error("unknown command")]
    Unknown,
}

#[derive(PartialEq, Debug)]
pub enum RedisCommand {
    UNKNOWN,
    PING,
    ECHO,
}
impl RedisCommand {
    pub fn from_str(s: &str) -> Result<RedisCommand, RedisCommandError> {
        match s {
            "PING" => Ok(RedisCommand::PING),
            "ECHO" => Ok(RedisCommand::ECHO),

            _ => Err(RedisCommandError::Invalid()),
        }
    }
}

#[derive(PartialEq)]
pub enum RedisCommandResponse {
    PONG,
}
impl RedisCommandResponse {
    pub fn to_string(self) -> String {
        match self {
            RedisCommandResponse::PONG => "+PONGrn".to_owned(),
        }
    }
}

only tokio is necessary for cargo.

<3

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật