Stuck on Replication (#YD3) but it fails when testing Replication (#XC1) connection refused error

Hi, I’m trying to figure out what’s going on but as of now all I can think of is that the tester is trying to start the replica node without having started the master node first. Or else, how should I handle this?

Thanks!

This is my Server.cs code:

using System.Net;
using codecrafters_redis;
using codecrafters_redis.Receivers;
using codecrafters_redis.Servers;

var port = args.Length > 0 && (args[0] == "--port" || args[0] == "-p")
    ? int.Parse(args[1])
    : Constants.DefaultRedisPort;

var masterHost = args.Length > 2 && args[2] == "--replicaof"
    ? args[3]
    : null;

int? masterPort = args.Length > 2 && args[2] == "--replicaof"
    ? int.Parse(args[4])
    : null;

ServerInfo.IsMaster = masterHost == null;

try
{
    if (ServerInfo.IsMaster)
    {
        new MasterNode(IPAddress.Any, port, new MasterReceiver())
            .Start();
    }
    else
    {
        new ReplicaNode(IPAddress.Any, port, masterHost!, masterPort!.Value, new ReplicaReceiver())
            .Handshake()
            .Start();
    }
}
catch (Exception ex)
{
    Console.WriteLine($"{ex.Message}, stack: {ex.StackTrace}");
    throw;
}

Eventually I’m getting the connection refused error

...
[replication-6] Running tests for Replication > Stage #6: Send handshake (2/3)
[replication-6] Master is running on port 6379
[replication-6] $ ./spawn_redis_server.sh --port 6380 --replicaof localhost 6379
[your_program] [replica-node-6380] Master node: localhost:6379
[replication-6] master: Received bytes: "*1\r\n$4\r\nPING\r\n"
[replication-6] master: Received RESP value: ["PING"]
[replication-6] Received ["PING"]
[replication-6] master: Sent "PONG"
[replication-6] master: Sent bytes: "+PONG\r\n"
[your_program] [replica-node-6380] Sending REPLCONF listening-port 6380
[replication-6] master: Received bytes: "*3\r\n$8\r\nREPLCONF\r\n$14\r\nlistening-port\r\n$4\r\n6380\r\n"
[replication-6] master: Received RESP value: ["REPLCONF", "listening-port", "6380"]
[replication-6] Received ["REPLCONF", "listening-port", "6380"]
[replication-6] master: Sent "OK"
[replication-6] master: Sent bytes: "+OK\r\n"
[replication-6] master: Received bytes: "*3\r\n$8\r\nREPLCONF\r\n$4\r\ncapa\r\n$6\r\npsync2\r\n"
[replication-6] master: Received RESP value: ["REPLCONF", "capa", "psync2"]
[replication-6] Received ["REPLCONF", "capa", "psync2"]
[replication-6] master: Sent "OK"
[replication-6] master: Sent bytes: "+OK\r\n"
[replication-6] Test passed.
[replication-6] Terminating program
[replication-6] Program terminated successfully
[replication-5] Running tests for Replication > Stage #5: Send handshake (1/3)
[replication-5] Master is running on port 6379.
[replication-5] $ ./spawn_redis_server.sh --port 6380 --replicaof localhost 6379
[your_program] [replica-node-6380] Master node: localhost:6379
[replication-5] master: Received bytes: "*1\r\n$4\r\nPING\r\n"
[replication-5] master: Received RESP value: ["PING"]
[replication-5] Received ["PING"]
[replication-5] master: Sent "PONG"
[replication-5] master: Sent bytes: "+PONG\r\n"
[replication-5] Test passed.
[replication-5] Terminating program
[your_program] [replica-node-6380] Sending REPLCONF listening-port 6380
[replication-5] Program terminated successfully
[replication-4] Running tests for Replication > Stage #4: Initial Replication ID and Offset
[replication-4] $ ./spawn_redis_server.sh
[your_program] [replica-node-6380] Master node: localhost:6379
[your_program] Handshake failed: Connection refused [::ffff:127.0.0.1]:6379, stack:    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
[your_program]    at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
[your_program]    at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program] --- End of stack trace from previous location ---
[your_program]    at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program]    at System.Net.Sockets.Socket.Connect(String host, Int32 port)
[your_program]    at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
[your_program]    at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
[your_program]    at codecrafters_redis.Servers.ReplicaNode.Handshake() in /app/src/Servers/ReplicaNode.cs:line 37
[your_program] Connection refused [::ffff:127.0.0.1]:6379, stack:    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
[your_program]    at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
[your_program]    at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program] --- End of stack trace from previous location ---
[your_program]    at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program]    at System.Net.Sockets.Socket.Connect(String host, Int32 port)
[your_program]    at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
[your_program]    at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
[your_program]    at codecrafters_redis.Servers.ReplicaNode.Handshake() in /app/src/Servers/ReplicaNode.cs:line 37
[your_program]    at Program.<Main>$(String[] args) in /app/src/Server.cs:line 29
[your_program] Unhandled exception. System.Net.Sockets.SocketException (111): Connection refused [::ffff:127.0.0.1]:6379
[your_program]    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
[your_program]    at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
[your_program]    at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program] --- End of stack trace from previous location ---
[your_program]    at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program]    at System.Net.Sockets.Socket.Connect(String host, Int32 port)
[your_program]    at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
[your_program]    at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
[your_program]    at codecrafters_redis.Servers.ReplicaNode.Handshake() in /app/src/Servers/ReplicaNode.cs:line 37
[your_program]    at Program.<Main>$(String[] args) in /app/src/Server.cs:line 29
[replication-4] dial tcp [::1]:6379: connect: connection refused
[replication-4] Test failed
[replication-4] Terminating program
[replication-4] Program terminated successfully
```strong text

@inner in replication-4, your program isn’t booted as a replica – it’s booted as a master (there’s no --replicaof flag being passed).

Thanks for the reply.

Hmm… it is really strange then as the error above is coming from when the replica tries to handshake with the master, hence the error: “Handshake failed: Connection refused”. So it’s like as if the master node wasn’t started…

@inner I don’t think your code needs to be connecting to a master at all in replication-4. Your code IS the master (—replicaof is NOT present as an arg). Is it possible that your program has some kind of default value set for the master address host and port and it’s attempting to connect to that? (I.e it’s behaving as a replica when it should just be a master?)

Once again, thank you for the reply! It does have some default behaviour, which may be incorrect. I will check my code asap.

Sneaky visual studio… I usually use Rider for dotnet projects, but I had opened this project with visual studio and did some configuration via UI, which created the launchSettings.json with the following contents. Then, when I ran the project using the command “dotnet run” I could reproduce the issue as it was grabbing all the parameters from the json file, which made the project to run as a replica. Fixed :slight_smile:

Thank you very much

what a relief!

launchSettings.json

{
  "profiles": {
    "codecrafters-redis": {
      "commandName": "Project",
      "commandLineArgs": "--port 6380 --replicaof localhost 6379",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:15072;http://localhost:15073"
    }
  }
}
1 Like

Ha! Okay that’s something I would’ve never guessed :joy:

1 Like

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

Note: I’ve updated the title of this post to include the stage ID (#YD3). You can learn about the stages rename here: Upcoming change: Stages overhaul.