I’m stuck on challenge #zu2. I refactored from regular Unix net to using Eio. It passes the ZU2 tests just fine but it fails the WY1 test that runs right after it.
From the logs, it looks like it is running ./your_program.sh
twice.. My app doesn’t quit after the ZU2 tests are run, so I’m getting:
[tester::#WY1] $ ./your_program.sh
[tester::#WY1] client-1: $ redis-cli PING
[your_program] +server: Running server
[tester::#WY1] Received: "" (no content received)
[tester::#WY1] ^ error
[tester::#WY1] Error: Expected start of a new RESP2 value (either +, -, :, $ or *)
[tester::#WY1] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)
Can someone familiar with Eio for Ocaml take a look at my code and see where I might not be cleaning up?
open Eio.Std
(* Prefix all trace output with "server: " *)
let traceln fmt = traceln ("server: " ^^ fmt)
module Read = Eio.Buf_read
(* Read one line from [client] and respond with "OK". *)
let rec handle_client flow addr =
traceln "Reading line from %a" Eio.Net.Sockaddr.pp addr;
(* We use a buffered reader because we may need to combine multiple reads
to get a single line (or we may get multiple lines in a single read,
although here we only use the first one). *)
let from_client = Read.of_flow flow ~max_size:1024 in
let line = Read.line from_client in
traceln "Received: %S" line;
Eio.Flow.copy_string "+PONG\r\n" flow;
handle_client flow addr
(* Accept incoming client connections on [socket].
We can handle multiple clients at the same time.
Never returns (but can be cancelled). *)
let run socket =
traceln "Running server";
Eio.Net.run_server socket handle_client
~on_error:(traceln "Error handling connection: %a" Fmt.exn)
~max_connections:1000
Here’s the log for the passing ZU2 test in case it helps:
tester::#ZU2] Running tests for Stage #ZU2 (Handle concurrent clients)
[tester::#ZU2] $ ./your_program.sh
[your_program] +server: Running server
[your_program] +server: Reading line from tcp:127.0.0.1:36966
[tester::#ZU2] client-1: $ redis-cli PING
[your_program] +server: Received: "*1"
[your_program] +server: Reading line from tcp:127.0.0.1:36966
[tester::#ZU2] Received "PONG"
[tester::#ZU2] client-2: $ redis-cli PING
[your_program] +server: Reading line from tcp:127.0.0.1:36972
[your_program] +server: Received: "*1"
[your_program] +server: Reading line from tcp:127.0.0.1:36972
[tester::#ZU2] Received "PONG"
[tester::#ZU2] client-1: > PING
[your_program] +server: Received: "*1"
[your_program] +server: Reading line from tcp:127.0.0.1:36966
[tester::#ZU2] Received "PONG"
[tester::#ZU2] client-1: > PING
[your_program] +server: Received: "*1"
[your_program] +server: Reading line from tcp:127.0.0.1:36966
[tester::#ZU2] Received "PONG"
[tester::#ZU2] client-2: > PING
[your_program] +server: Received: "*1"
[your_program] +server: Reading line from tcp:127.0.0.1:36972
[tester::#ZU2] Received "PONG"
[your_program] +server: Error handling connection: End_of_file
[tester::#ZU2] client-3: $ redis-cli PING
[your_program] +server: Reading line from tcp:127.0.0.1:36988
[your_program] +server: Received: "*1"
[your_program] +server: Reading line from tcp:127.0.0.1:36988
[tester::#ZU2] Received "PONG"
[tester::#ZU2] Test passed.