Execution timed out

I’m stuck on Stage #(change to your stage, ex. #PV1).

Here are my logs:

include relevant logs here (please make sure to keep the backticks around this!)

And here’s a snippet of my code:

include relevant code here (please make sure to keep the backticks around this!)
def main():
    print("Logs from your program will appear here!")
    with socket.create_server(("localhost", 9092), reuse_port=True) as server:
        while True:
            conn, raddr = server.accept() # wait for client
            message_len = int.from_bytes(conn.recv(4), byteorder="big", signed=True)
            print(message_len)
            data = conn.recv(message_len + 8)
            print(f"data {data}")
            request_api_key = int.from_bytes(data[:2], byteorder="big", signed=True)
            request_api_version = int.from_bytes(data[2:4], byteorder="big", signed=True)
            correlation_id = int.from_bytes(data[4:8], byteorder="big", signed=True)
            print("correlation_id, request_api_key====")
            print(correlation_id, request_api_key)
            client_id = bytes.decode(data[8:], "utf-8")
            tag_buffer = b"\x00"
            # correlation , error code, num_api_keys, api_key, min_version, max_version, tag_buffer, throttle_time_ms, tag_buffer
            message_bytes = correlation_id.to_bytes(4, byteorder="big", signed=True)
            message_bytes += int(0).to_bytes(2, byteorder="big", signed=True)
            message_bytes += int(2).to_bytes(1, byteorder="big", signed=True)
            message_bytes += int(request_api_key).to_bytes(2, byteorder="big", signed=True)
            message_bytes += int(0).to_bytes(2, byteorder="big", signed=True)
            message_bytes += int(4).to_bytes(2, byteorder="big", signed=True)
            message_bytes += tag_buffer
            message_bytes += int(0).to_bytes(4, byteorder="big", signed=True)
            message_bytes += tag_buffer
            req_len = len(message_bytes).to_bytes(4, byteorder="big", signed=True)
            response = req_len + message_bytes
            conn.sendall(response)
            # conn.close()

if __name__ == "__main__":
    main()

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

Seems like you have passed the stage #PV1 and are actually failing on the stage #NH4, which deals with accepting multiple requests from a same client.

To handle the same you would need to continuously check the connection if any new data has been sent, and process it accordingly!

In the current code:

while True:
    conn, raddr = server.accept() # wait for client

in each iteration of the for loop, you are expecting 1 new client to connect to the server, and then you are just processing one request from it. Maybe try creating another loop for each conn that continuously reads data from it?

1 Like

No, the code I have shared and ss I have shared are not working for PV1 also. #PV1
Please help me with that.

Can you add the screenshot of the logs for the same? The original screenshot posted shows the logs for the stage #NH4.

My bad. I was under the impression that I’m submitting to PV1. can we submit to any previous levels also? I’m not sure.

You can run codecrafters test --previous to run all the previous tests as well in a serial manner to find the first test where your application is crashing.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

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