Stuck at stage #WA6 - Parse CorrelationId

I dont know what is wrong with the code, I can even test it locally.

Initiating test run...

⚡ This is a turbo test run. https://codecrafters.io/turbo

Running tests. Logs should appear shortly...

[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.

Debug = true

[tester::#WA6] Running tests for Stage #WA6 (Parse Correlation ID)
[tester::#WA6] $ ./your_program.sh /tmp/server.properties
[tester::#WA6] Connecting to broker at: localhost:9092
[your_program] Logs from your program will appear here!
[tester::#WA6] Connection to broker at localhost:9092 successful
[tester::#WA6] Sending "ApiVersions" (version: 4) request (Correlation id: 125036779)
[tester::#WA6] Hexdump of sent "ApiVersions" request: 
[tester::#WA6] Idx  | Hex                                             | ASCII
[tester::#WA6] -----+-------------------------------------------------+-----------------
[tester::#WA6] 0000 | 00 00 00 23 00 12 00 04 07 73 e8 eb 00 09 6b 61 | ...#.....s....ka
[tester::#WA6] 0010 | 66 6b 61 2d 63 6c 69 00 0a 6b 61 66 6b 61 2d 63 | fka-cli..kafka-c
[tester::#WA6] 0020 | 6c 69 04 30 2e 31 00                            | li.0.1.
[tester::#WA6] 
[tester::#WA6] error reading from connection: read tcp [::1]:57512->[::1]:9092: read: connection reset by peer
[tester::#WA6] Test failed
[tester::#WA6] Terminating program
[your_program] Received message [7 115 232 235] (125036779)Sending response:  [7 115 232 235]
[tester::#WA6] Program terminated successfully

View our article on debugging test failures: https://codecrafters.io/debug

Here is my code

package main

import (
	"encoding/binary"
	"fmt"
	"net"
	"os"
)

// Ensures gofmt doesn't remove the "net" and "os" imports in stage 1 (feel free to remove this!)
var _ = net.Listen
var _ = os.Exit

func main() {
	// You can use print statements as follows for debugging, they'll be visible when running tests.
	fmt.Println("Logs from your program will appear here!")

	// Uncomment this block to pass the first stage

	l, err := net.Listen("tcp", "0.0.0.0:9092")
	if err != nil {
		fmt.Println("Failed to bind to port 9092")
		os.Exit(1)
	}
	conn, err := l.Accept()
	defer conn.Close()
	if err != nil {
		fmt.Println("Error accepting connection: ", err.Error())
		os.Exit(1)
	}
	// Get the correlation ID from the request
	buf := make([]byte, 12)
	_, err = conn.Read(buf)
	if err != nil {
		fmt.Println("Error reading from connection: ", err.Error())
		os.Exit(1)
	}
	// Send the correlation ID back to the client
	fmt.Printf("Received message %v (%d)", buf[8:12], int32(binary.BigEndian.Uint32(buf[8:12])))
	resp := make([]byte, 8)
	copy(resp[0:4], []byte{0, 0, 0, 0})
	copy(resp[4:8], buf[8:12])
	fmt.Println("Sending response: ", resp[4:8])
	_, err = conn.Write(resp)
	conn.Close()
}

Hey @GenjiM1n4moto, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Hi, @andy1li , here is the link.

@GenjiM1n4moto The issue is that the buffer size is too small. When the broker sends a response before the full request is read, it can lead to a connection reset.

Try increasing the buffer size to ensure it can hold the full request.

1 Like

Sry for my late response, thanks, @andy1li .

The solution worked for me!

1 Like

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