Submission issues in blocking retrieval #ec3

I’m stuck on Stage Blocking retrieval #ec3.

Hey all, Having an issue on submission here where the redis-cli in the runner isn’t reporting any output.

Locally my redis-cli works with the same commands, it’s semi inconsistant (sometimes it fails on an earlier stage). Submitted over several days to try and clear any build cache on CodeCrafters side.

Any ideas would be appreciated :slight_smile:

Here are my logs:

remote: [tester::#EC3] Running tests for Stage #EC3 (Lists - Blocking retrieval) remote: [tester::#EC3] $ ./your_program.sh remote: [tester::#EC3] [client-1] Connected (port 49124 -> port 6379) 
remote: [tester::#EC3] [client-2] Connected (port 49136 -> port 6379) 
remote: [tester::#EC3] [client-3] Connected (port 49150 -> port 6379) 
remote: [tester::#EC3] [client-2] $ redis-cli BLPOP blueberry 0 remote: [tester::#EC3] [client-2] Sent bytes: "*3\r\n$5\r\nBLPOP\r\n$9\r\nblueberry\r\n$1\r\n0\r\n" 
remote: [tester::#EC3] [client-3] $ redis-cli BLPOP blueberry 0 
remote: [tester::#EC3] [client-3] Sent bytes: "*3\r\n$5\r\nBLPOP\r\n$9\r\nblueberry\r\n$1\r\n0\r\n" 
remote: [tester::#EC3] [client-1] $ redis-cli RPUSH blueberry mango 
remote: [tester::#EC3] [client-1] Sent bytes: "*3\r\n$5\r\nRPUSH\r\n$9\r\nblueberry\r\n$5\r\nmango\r\n"
remote: [your_program] Sending output to client: :1 
remote: [your_program] 
remote: [your_program]  stream status: canwrite True 
remote: [your_program] Sending output to client: *2 
remote: [your_program] $9 
remote: [your_program] blueberry 
remote: [your_program] $5 
remote: [your_program] mango 
remote: [your_program] 
remote: [your_program]  stream status: canwrite True 
remote: [tester::#EC3] [client-1] Received bytes: ":1\r\n" 
remote: [tester::#EC3] [client-1] Received RESP integer: 1 
remote: [tester::#EC3] [client-1] ✔︎ Received 1 
remote: [tester::#EC3] [client-2] Expecting response of BLPOP command 
remote: [tester::#EC3] Received: "" (no content received) 
remote: [tester::#EC3]            ^ error remote: [tester::#EC3] Error: Expected start of a new RESP2 value (either +, -, :, $ or *) 
remote: [tester::#EC3] Test failed remote: [tester::#EC3] Terminating program remote: [tester::#EC3] Program terminated successfully remote:

Same code failing eleswhere (but less log statements sorry)

remote: [tester::#EC3] [client-1] Connected (port 36236 -> port 6379)                                                                remote: [tester::#EC3] [client-2] Connected (port 36238 -> port 6379)                                                                remote: [tester::#EC3] [client-3] Connected (port 36240 -> port 6379)                                                                remote: [tester::#EC3] [client-2] $ redis-cli BLPOP strawberry 0                                                                     remote: [tester::#EC3] [client-2] Sent bytes: "*3\r\n$5\r\nBLPOP\r\n$10\r\nstrawberry\r\n$1\r\n0\r\n"                                remote: [tester::#EC3] [client-3] $ redis-cli BLPOP strawberry 0
remote: [tester::#EC3] [client-3] Sent bytes: "*3\r\n$5\r\nBLPOP\r\n$10\r\nstrawberry\r\n$1\r\n0\r\n"                                remote: [tester::#EC3] [client-1] $ redis-cli RPUSH strawberry grape                                                                 remote: [tester::#EC3] [client-1] Sent bytes: "*3\r\n$5\r\nRPUSH\r\n$10\r\nstrawberry\r\n$5\r\ngrape\r\n"                            remote: [tester::#EC3] [client-1] Received bytes: ":1\r\n"
remote: [tester::#EC3] [client-1] Received RESP integer: 1
remote: [tester::#EC3] [client-1] ✔︎ Received 1
remote: [tester::#EC3] [client-2] Expecting response of BLPOP command                                                                remote: [tester::#EC3] [client-2] Received bytes: "*2\r\n$10\r\nstrawberry\r\n$5\r\ngrape\r\n"                                       remote: [tester::#EC3] [client-2] Received RESP array: ["strawberry", "grape"]                                                       remote: [tester::#EC3] [client-2] ✔︎ Received ["strawberry", "grape"]                                                                 remote: [tester::#EC3] [client-3] Expecting no response
remote: [tester::#EC3] [client-3] ✔︎ No response received yet
remote: [tester::#EC3] Test passed.
remote: [tester::#EC3] Terminating program
remote: [tester::#EC3] Program terminated successfully
remote:
remote: [tester::#JP1] Running tests for Stage #JP1 (Lists - Remove multiple elements)                                               remote: [tester::#JP1] $ ./your_program.sh
remote: [tester::#JP1] [client] $ redis-cli RPUSH banana strawberry pineapple blueberry mango grape banana pear                      remote: [tester::#JP1] [client] Sent bytes: "*9\r\n$5\r\nRPUSH\r\n$6\r\nbanana\r\n$10\r\nstrawberry\r\n$9\r\npineapple\r\n$9\r\nblueberry\r\n$5\r\nmango\r\n$5\r\ngrape\r\n$6\r\nbanana\r\n$4\r\npear\r\n"                                                                remote: [tester::#JP1] [client] Received bytes: ":7\r\n"
remote: [tester::#JP1] [client] Received RESP integer: 7
remote: [tester::#JP1] [client] ✔︎ Received 7
remote: [tester::#JP1] [client] > LPOP banana 2
remote: [tester::#JP1] [client] Sent bytes: "*3\r\n$4\r\nLPOP\r\n$6\r\nbanana\r\n$1\r\n2\r\n"                                        remote: [tester::#JP1] Received: "" (no content received)                                                                            remote: [tester::#JP1]            ^ error                                                                                            remote: [tester::#JP1] Error: Expected start of a new RESP2 value (either +, -, :, $ or *)                                           remote: [tester::#JP1] Test failed                                                                                                   remote: [tester::#JP1] Terminating program                                                                                           remote: [tester::#JP1] Program terminated successfully   

And here’s a snippet of my code:

            Console.WriteLine($"Sending output to client: {output_string} \n stream status: canwrite {stream.CanWrite}");
            stream.Write(Encoding.UTF8.GetBytes(output_string.ToString()));
            await stream.FlushAsync();
            stream.Close();

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

Sure, GitHub - PaulLocksley/-codecrafters-redis-csharp: Not worth reading.

Hey @PaulLocksley, sorry for the delayed response!

There might be a few issues at play. The first one I noticed is that handleConnection no longer handles multiple commands from the same connection.

Suggestions:

  1. Use our CLI to test against previous stages by running:
codecrafters test --previous
  1. Focus on fixing the early stages first, as later stages depend on them.

Once you’ve got this sorted out, we can keep going from there.

Thanks @andy1li , yeah I removed the loop when I was testing if not exiting the stream properly was causing the issue.
Added it back to pass previous tests :slight_smile:

Back to failing only on #EC3 :slight_smile:

1 Like

Hmm, looks like the pop result was sent to an unexpected client (client-3):

Although client-2 initiated the connection before client-3, the logs show client-3 as the first to connect.

I’m opening a PR to enforce the correct order and will update here once the fix is merged.

1 Like

@PaulLocksley The root cause is the use of AcceptTcpClientAsync. A simple fix is to switch to the synchronous version:

    private static void Main(string[] args)
    {
        var cache = new RedisCache();
        var server = new TcpListener(IPAddress.Any, 6379);
        server.Start();

        try
        {
            while (true)
            {
                var client = server.AcceptTcpClient();
                Task.Run(() => handleConnection(client, cache));
            }
        }
        finally
        {
            server.Stop();
        }
    }

BTW, it’s generally not ideal to keep everything in Main. You might want to move most of the logic into separate methods or classes for better clarity and maintainability.

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.