# Stuck on stage #NC5 with rust

**URL:** <https://forum.codecrafters.io/t/stuck-on-stage-nc5-with-rust/15334>\
**Category:** Bug Reports\
**Tags:** challenge:kafka\
**Created:** [September 24, 2025, 6:23am UTC](https://forum.codecrafters.io/t/stuck-on-stage-nc5-with-rust/15334 "2025-09-24T06:23:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flyingsand](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/flyingsand/32/17539_2.png) [@flyingsand](https://forum.codecrafters.io/u/flyingsand)\
**Post date:** [September 24, 2025, 6:23am UTC](https://forum.codecrafters.io/t/stuck-on-stage-nc5-with-rust/15334/1 "2025-09-24T06:23:20Z")

</div>

I’m stuck on Stage #nc5 with rust.

The tester seems rather confusing. If I write all zeros to the first four bytes of tcpstream, tester will not read the subsequent bytes and output “Expected INT32 length to be 4 bytes, got 0 bytes”. However, if the first four bytes are not all zeros, tester will not read any bytes and will directly output “error reading from connection: unexpected EOF”. This situation also occurs when I use the passed Code Examples. I wonder if this is a bug.

My code is available here:[flyingsand/codecrafters-kafka-rust](https://github.com/flyingsand/codecrafters-kafka-rust)

Here are my logs:

zeros to the first four bytes situation:

```auto
remote: [tester::#NC5] Running tests for Stage #NC5 (Parse API Version)
remote: [tester::#NC5] $ ./your_program.sh /tmp/server.properties
remote: [tester::#NC5] Connecting to broker at: localhost:9092
remote: [your_program] Logs from your program will appear here!
remote: [tester::#NC5] Connection to broker at localhost:9092 successful
remote: [tester::#NC5] [Encoder] - ApiVersionsRequest
remote: [tester::#NC5] [Encoder] - Header
remote: [tester::#NC5] [Encoder] - APIKey (18)
remote: [tester::#NC5] [Encoder] - APIVersion (1080)
remote: [tester::#NC5] [Encoder] - CorrelationID (1667803575)
remote: [tester::#NC5] [Encoder] - ClientID (kafka-tester)
remote: [tester::#NC5] [Encoder] - Body
remote: [tester::#NC5] [Encoder] - ClientSoftwareName (kafka-cli)
remote: [tester::#NC5] [Encoder] - ClientSoftwareVersion (0.1)
remote: [tester::#NC5] Sending "ApiVersions" request
remote: [tester::#NC5] Hexdump of sent "ApiVersions" request: 
remote: [tester::#NC5] Idx | Hex | ASCII
remote: [tester::#NC5] -----+-------------------------------------------------+-----------------
remote: [tester::#NC5] 0000 | 00 00 00 26 00 12 04 38 63 68 a9 b7 00 0c 6b 61 | ...&...8ch....ka
remote: [tester::#NC5] 0010 | 66 6b 61 2d 74 65 73 74 65 72 00 0a 6b 61 66 6b | fka-tester..kafk
remote: [tester::#NC5] 0020 | 61 2d 63 6c 69 04 30 2e 31 00 | a-cli.0.1.
remote: [tester::#NC5] 
remote: [tester::#NC5] Hexdump of received "ApiVersions" response: 
remote: [tester::#NC5] Idx | Hex | ASCII
remote: [tester::#NC5] -----+-------------------------------------------------+-----------------
remote: [tester::#NC5] 0000 | 00 00 00 00 | ....
remote: [tester::#NC5] 
remote: [tester::#NC5] [Decoder] - ApiVersionsResponse
remote: [tester::#NC5] [Decoder] - Header
remote: [tester::#NC5] [Decoder] X CorrelationID (decode error)
remote: [tester::#NC5] Received bytes:
remote: [tester::#NC5] Hex (bytes 0--1) | ASCII
remote: [tester::#NC5] ------------------------------------------------+------------------
remote: [tester::#NC5] 
remote: [tester::#NC5] ^ ^
remote: [your_program] accepted new connection
remote: [tester::#NC5] Expected INT32 length to be 4 bytes, got 0 bytes
remote: [tester::#NC5] Test failed
remote: [tester::#NC5] Terminating program
remote: [tester::#NC5] Program terminated successfully

```

first four bytes are not all zeros situation:

```auto
remote: [tester::#NC5] Running tests for Stage #NC5 (Parse API Version)
remote: [tester::#NC5] $ ./your_program.sh /tmp/server.properties
remote: [tester::#NC5] Connecting to broker at: localhost:9092
remote: [your_program] Logs from your program will appear here!
remote: [tester::#NC5] Connection to broker at localhost:9092 successful
remote: [tester::#NC5] [Encoder] - ApiVersionsRequest
remote: [tester::#NC5] [Encoder] - Header
remote: [tester::#NC5] [Encoder] - APIKey (18)
remote: [tester::#NC5] [Encoder] - APIVersion (1564)
remote: [tester::#NC5] [Encoder] - CorrelationID (350636842)
remote: [tester::#NC5] [Encoder] - ClientID (kafka-tester)
remote: [tester::#NC5] [Encoder] - Body
remote: [tester::#NC5] [Encoder] - ClientSoftwareName (kafka-cli)
remote: [tester::#NC5] [Encoder] - ClientSoftwareVersion (0.1)
remote: [tester::#NC5] Sending "ApiVersions" request
remote: [tester::#NC5] Hexdump of sent "ApiVersions" request: 
remote: [tester::#NC5] Idx | Hex | ASCII
remote: [tester::#NC5] -----+-------------------------------------------------+-----------------
remote: [tester::#NC5] 0000 | 00 00 00 26 00 12 06 1c 14 e6 4b 2a 00 0c 6b 61 | ...&......K*..ka
remote: [tester::#NC5] 0010 | 66 6b 61 2d 74 65 73 74 65 72 00 0a 6b 61 66 6b | fka-tester..kafk
remote: [tester::#NC5] 0020 | 61 2d 63 6c 69 04 30 2e 31 00 | a-cli.0.1.
remote: [tester::#NC5] 
remote: [your_program] accepted new connection
remote: [tester::#NC5] error reading from connection: unexpected EOF
remote: [tester::#NC5] Test failed
remote: [tester::#NC5] Terminating program
remote: [tester::#NC5] Program terminated successfully

```

And here’s a snippet of my code:

```rust
//main.rs
fn main() -> std::io::Result<()>{
    println!("Logs from your program will appear here!");

    // Uncomment this block to pass the first stage
    //
     let listener = TcpListener::bind("127.0.0.1:9092")?;
    
     for stream in listener.incoming() {
         match stream {
             Ok(mut _stream) => {
                let input_protocol = InputProtocol::parse_from_stream(&mut _stream)?;
                let output_protocol = OutputProtocol::build_from_input_protocol(&input_protocol);

                output_protocol.write_protocol(&mut _stream)?;
                let _ = _stream.flush()?;

             }
             Err(e) => {
                 println!("error: {}", e);
             }
         }
    }
    Ok(())
}

//protocol.rs
#[derive(Debug)]
pub struct InputProtocol{
    pub message_size: u32,
    pub request_api_key: u16,
    pub request_api_version: u16,
    pub correlation_id: u32
}
#[derive(Debug)]
pub struct OutputProtocol {
    pub message_size: u32,
    pub correlation_id: u32,
    pub error_code: u16
}
impl InputProtocol {
    pub fn parse_from_stream(stream:&mut TcpStream) -> io::Result<Self> {
        let mut bytes = [0u8;4];
        stream.read_exact(&mut bytes)?;
        let len = u32::from_be_bytes(bytes);
        let mut buffer = vec![0u8;len as usize];
        stream.read_exact(&mut buffer)?;

        let bytes = &buffer[0..2];
        let api_key = u16::from_be_bytes(bytes.try_into().unwrap());
        let bytes = &buffer[2..4];
        let api_version = u16::from_be_bytes(bytes.try_into().unwrap());
        let bytes = &buffer[4..8];
        let correlation_id = u32::from_be_bytes(bytes.try_into().unwrap());
        Ok(
            InputProtocol {
                message_size: len,
                request_api_key: api_key,
                request_api_version: api_version,
                correlation_id
            }
        )
        
    }
}

impl OutputProtocol {
    pub fn build_from_input_protocol(input_protocol: & InputProtocol) -> Self{
        let correlation_id = input_protocol.correlation_id;
        let error_code = Self::build_error_code(input_protocol);
        let message_size = 0; 
        OutputProtocol {
            message_size,
            correlation_id,
            error_code
        }
    }
    
    fn build_error_code(input_protocol: & InputProtocol) -> u16 {
        if input_protocol.request_api_version <=4 {
            0
        }
        else{
            35
        }
    }
    
    pub fn write_protocol(&self, stream:&mut TcpStream) -> io::Result<()>{
        // Write message size
        stream.flush()?;
        stream.write_all(&self.message_size.to_be_bytes())?;
        // Write correlation ID
        stream.write_all(&self.correlation_id.to_be_bytes())?;
        // Write error code
        stream.write_all(&self.error_code.to_be_bytes())?;
        stream.flush()?;
        Ok(())
    }
}

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [September 24, 2025, 6:56am UTC](https://forum.codecrafters.io/t/stuck-on-stage-nc5-with-rust/15334/4 "2025-09-24T06:56:25Z")

</div>

Hey @flyingsand, thanks for highlighting the issue!

We’re currently looking into it and will update you as soon as we have more details.

---

<div class="post-metadata">

**Author:** ![flyingsand](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/flyingsand/32/17539_2.png) [@flyingsand](https://forum.codecrafters.io/u/flyingsand)\
**Post date:** [September 25, 2025, 2:07pm UTC](https://forum.codecrafters.io/t/stuck-on-stage-nc5-with-rust/15334/5 "2025-09-25T14:07:34Z")

</div>

I didn’t modify the code just now, but the test suddenly passed. So I’m quite sure this is a bug.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [September 25, 2025, 4:11pm UTC](https://forum.codecrafters.io/t/stuck-on-stage-nc5-with-rust/15334/6 "2025-09-25T16:11:22Z")

</div>

> I didn’t modify the code just now, but the test suddenly passed. So I’m quite sure this is a bug.

We’ve shipped a fix in [this PR](https://github.com/codecrafters-io/kafka-tester/pull/126). Let us know if the issue comes up again!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [October 2, 2025, 4:12pm UTC](https://forum.codecrafters.io/t/stuck-on-stage-nc5-with-rust/15334/8 "2025-10-02T16:12:45Z")

</div>

Closed on October 2, 2025, 4:12pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
