Stuck at JZ6, tests passed a few times but when submitting it errors and can't connect to the server

I ran codecrafters test –previous and the tests passed. I added a ton of debugging in the main.ts to see what’s happening because sometimes it runs and sometimes it doesn’t. When the tests passed I saw this for JZ6:
[tester::#JZ6] Running tests for Stage #JZ6 (RDB Persistence - Read a key)
[tester::#JZ6] Created RDB file with a single key: [“blueberry”]
[tester::#JZ6] Hexdump of RDB file contents:
[tester::#JZ6] Idx | Hex | ASCII
[tester::#JZ6] -----±------------------------------------------------±----------------
[tester::#JZ6] 0000 | 52 45 44 49 53 30 30 31 31 fa 09 72 65 64 69 73 | REDIS0011..redis
[tester::#JZ6] 0010 | 2d 76 65 72 05 37 2e 32 2e 30 fa 0a 72 65 64 69 | -ver.7.2.0..redi
[tester::#JZ6] 0020 | 73 2d 62 69 74 73 c0 40 fe 00 fb 01 00 00 09 62 | s-bits.@…b
[tester::#JZ6] 0030 | 6c 75 65 62 65 72 72 79 09 72 61 73 70 62 65 72 | lueberry.raspber
[tester::#JZ6] 0040 | 72 79 ff e7 32 68 28 2c c5 26 d7 | ry..2h(,.&.
[tester::#JZ6]
[tester::#JZ6] $ ./spawn_redis_server.sh --dir /tmp/rdb-3965 --dbfilename banana.rdb
[your_program] About to import modules…
[your_program] Imported net
[your_program] Imported fs
[your_program] Imported path
[your_program] Imported DataParser
[your_program] Imported RedisStore
[your_program] Imported GlobalDispatcher
[your_program] Imported EventEmitter
[your_program] Imported CommandHandler
[your_program] Imported serverInfo
[your_program] Imported MasterConnectionHandler
[your_program] Imported detectServerRole
[your_program] Imported RDBParser
[your_program] All imports completed
[your_program] === STARTUP DEBUG ===
[your_program] process.argv: [ “/usr/local/bin/bun”, “/app/app/main.ts”, “–dir”, “/tmp/rdb-3965”, “–dbfilename”,
[your_program] “banana.rdb”
[your_program] ]
[your_program] Process error handlers set
[your_program] Created basic objects
[your_program] Created command handler
[your_program] Created dispatcher
[your_program] Started expiration check task
[your_program] About to call detectServerRole…
[your_program] detectServerRole succeeded: {
[your_program] port: 6379,
[your_program] role: “master”,
[your_program] masterHost: null,
[your_program] masterPort: null,
[your_program] dir: “/tmp/rdb-3965”,
[your_program] dbfilename: “banana.rdb”,
[your_program] }
[your_program] Setting serverInfo…
[your_program] serverInfo set: {
[your_program] role: “master”,
[your_program] masterHost: null,
[your_program] masterPort: null,
[your_program] master_replid: “8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb”,
[your_program] master_repl_offset: 0,
[your_program] replicas: ,
[your_program] dir: “/tmp/rdb-3965”,
[your_program] dbfilename: “banana.rdb”,
[your_program] }
[your_program] Creating server…
[your_program] Server created, about to listen…
[your_program] Server listen call made, waiting for callback…
[your_program] Server is listening on :6379
[your_program] Loading RDB file from /tmp/rdb-3965/banana.rdb
[your_program] Starting RDB parse. Buffer length: 75
[your_program] RDB version: 0011
[your_program] Main loop: cursor=9, remaining=66
[your_program] Reading opcode: 0xfa at position 9
[your_program] Processing metadata section
[your_program] Parsing metadata at cursor 9
[your_program] parseStringEncoded: firstByte=0x9, firstTwoBits=0b0, cursor=10
[your_program] String length: 9
[your_program] Parsed string: “redis-ver” (length=9)
[your_program] Parsed metadata key: “redis-ver”
[your_program] parseStringEncoded: firstByte=0x5, firstTwoBits=0b0, cursor=20
[your_program] String length: 5
[your_program] Parsed string: “7.2.0” (length=5)
[your_program] Parsed metadata value: “7.2.0”
[your_program] Metadata: redis-ver = 7.2.0, cursor now at 26
[your_program] Main loop: cursor=26, remaining=49
[your_program] Reading opcode: 0xfa at position 26
[your_program] Processing metadata section
[your_program] Parsing metadata at cursor 26
[your_program] parseStringEncoded: firstByte=0xa, firstTwoBits=0b0, cursor=27
[your_program] String length: 10
[your_program] Parsed string: “redis-bits” (length=10)
[your_program] Parsed metadata key: “redis-bits”
[your_program] parseStringEncoded: firstByte=0xc0, firstTwoBits=0b11, cursor=38
[your_program] Special encoding type: 0
[your_program] Parsed 8-bit integer: 64
[your_program] Parsed metadata value: “64”
[your_program] Metadata: redis-bits = 64, cursor now at 40
[your_program] Main loop: cursor=40, remaining=35
[your_program] Reading opcode: 0xfe at position 40
[your_program] Processing database section
[your_program] Parsing database section at cursor 40
[your_program] Selected database: 0, cursor now at 42
[your_program] Next opcode after DB selection: 0xfb
[your_program] DB resize hints: ht_size=1, exp_ht_size=0, cursor now at 45
[your_program] In KV loop: opcode=0x0 at cursor=45
[your_program] Parsing key-value pair at cursor 45
[your_program] Initial opcode: 0x0
[your_program] Processing value type: 0x0
[your_program] Cursor after consuming value type: 46
[your_program] parseStringEncoded: firstByte=0x9, firstTwoBits=0b0, cursor=46
[your_program] String length: 9
[your_program] Parsed string: “blueberry” (length=9)
[your_program] Parsed key: “blueberry”, cursor now at 56
[your_program] parseStringEncoded: firstByte=0x9, firstTwoBits=0b0, cursor=56
[your_program] String length: 9
[your_program] Parsed string: “raspberry” (length=9)
[your_program] Parsed value: “raspberry”, cursor now at 66
[your_program] Storing key-value pair: blueberry = raspberry
[your_program] In KV loop: opcode=0xff at cursor=66
[your_program] Found section marker, exiting KV loop
[your_program] Main loop: cursor=66, remaining=9
[your_program] Reading opcode: 0xff at position 66
[your_program] Reached EOF.
[your_program] RDB parsing completed successfully
[your_program] Server startup completed successfully
[your_program] Server ‘listening’ event fired
[tester::#JZ6] [client] $ redis-cli KEYS *
[tester::#JZ6] [client] Sent bytes: “2\r\n$4\r\nKEYS\r\n$1\r\n\r\n”
[your_program] New connection established
[your_program] globalCommandHandler response: *1
[your_program] $9
[your_program] blueberry
[your_program]
[tester::#JZ6] [client] Received bytes: “*1\r\n$9\r\nblueberry\r\n”
[tester::#JZ6] [client] Received RESP array: [“blueberry”]
[tester::#JZ6] [client] Received [“blueberry”]
[tester::#JZ6] Test passed.
[tester::#JZ6] Terminating program
[tester::#JZ6] Program terminated successfully

Then codecrafters submit
[tester::#JZ6] Running tests for Stage #JZ6 (RDB Persistence - Read a key)
[tester::#JZ6] Created RDB file with a single key: [“raspberry”]
[tester::#JZ6] Hexdump of RDB file contents:
[tester::#JZ6] Idx | Hex | ASCII
[tester::#JZ6] -----±------------------------------------------------±----------------
[tester::#JZ6] 0000 | 52 45 44 49 53 30 30 31 31 fa 09 72 65 64 69 73 | REDIS0011..redis
[tester::#JZ6] 0010 | 2d 76 65 72 05 37 2e 32 2e 30 fa 0a 72 65 64 69 | -ver.7.2.0..redi
[tester::#JZ6] 0020 | 73 2d 62 69 74 73 c0 40 fe 00 fb 01 00 00 09 72 | s-bits.@…r
[tester::#JZ6] 0030 | 61 73 70 62 65 72 72 79 05 6d 61 6e 67 6f ff 39 | aspberry.mango.9
[tester::#JZ6] 0040 | 8e d7 b7 76 71 e4 fa | …vq..
[tester::#JZ6]
[tester::#JZ6] $ ./spawn_redis_server.sh --dir /tmp/rdb-6342 --dbfilename strawberry.rdb
[your_program] About to import modules…
[your_program] Imported net
[your_program] Imported fs
[your_program] Imported path
[your_program] Imported DataParser
[your_program] Imported RedisStore
[your_program] Imported GlobalDispatcher
[your_program] Imported EventEmitter
[your_program] Imported CommandHandler
[your_program] Imported serverInfo
[your_program] Imported MasterConnectionHandler
[your_program] Imported detectServerRole
[your_program] Imported RDBParser
[your_program] All imports completed
[your_program] === STARTUP DEBUG ===
[your_program] process.argv: [ “/usr/local/bin/bun”, “/app/app/main.ts”, “–dir”, “/tmp/rdb-6342”, “–dbfilename”,
[your_program] “strawberry.rdb”
[your_program] ]
[your_program] Process error handlers set
[your_program] Created basic objects
[your_program] Created command handler
[your_program] Created dispatcher
[your_program] Started expiration check task
[your_program] About to call detectServerRole…
[tester::#JZ6] dial tcp [::1]:6379: connect: connection refused
[tester::#JZ6] Test failed
[tester::#JZ6] Terminating program
[tester::#JZ6] Program terminated successfully

And that’s all. I’m at a loss. I can run it fine locally. This is my standard server.listen function
server.listen(port, "127.0.0.1", () => {...

I also have

server.on("error", (err: NodeJS.ErrnoException) => {
  console.error("Server listen error:", err);
  if (err.code === 'EADDRINUSE') {
    console.error(`Port ${port} is already in use`);
  }
  process.exit(1);
});

But it doesn’t get here. I’m using import * as net from "net"; and creating the server via const server: net.Server = net.createServer((connection: net.Socket) => {...

I just don’t understand why it doesn’t even run sometimes. Is it something in the way that I parse the .rdb? But if it is, then why doesn’t it even get to code that executes the parsing? Why does it pass one time and then it doesn’t? I tried recreating the same .rdb provided in one the failing tests using

cat <<'EOF' > ./redis.hex
524544495330303131fa0a72656469732d62697473c040fa0972656469732d76657205372e322e30fe00fb010000056d616e676f0470656172ffc1f94f6989fc3617
EOF

and then

xxd -r -p /tmp/redis.hex > ./redis.rdb 

and it works when I run it with bun run dev -dir . --dbfilename redis.rdb as well as

./spawn_redis_server.sh --dir . --dbfilename redis.rdb and it worked again.

Any advice would be greatly appreciated. I’m stuck here since last night and I have no idea if the error is caused by something I do wrong in the server connection, in the RDB Parser, or somewhere else in the code. I just tried again with codecrafters test –previous and the last part is successful again, yet running codecrafters test fails with
[tester::#JZ6] dial tcp [::1]:6379: connect: connection refused
[tester::#JZ6] Test failed

This is the last result of codecrafters test –previous
[tester::#JZ6] Running tests for Stage #JZ6 (RDB Persistence - Read a key)
[tester::#JZ6] Created RDB file with a single key: [“banana”]
[tester::#JZ6] Hexdump of RDB file contents:
[tester::#JZ6] Idx | Hex | ASCII
[tester::#JZ6] -----±------------------------------------------------±----------------
[tester::#JZ6] 0000 | 52 45 44 49 53 30 30 31 31 fa 09 72 65 64 69 73 | REDIS0011..redis
[tester::#JZ6] 0010 | 2d 76 65 72 05 37 2e 32 2e 30 fa 0a 72 65 64 69 | -ver.7.2.0..redi
[tester::#JZ6] 0020 | 73 2d 62 69 74 73 c0 40 fe 00 fb 01 00 00 06 62 | s-bits.@…b
[tester::#JZ6] 0030 | 61 6e 61 6e 61 05 67 72 61 70 65 ff 1e 69 a3 d5 | anana.grape..i..
[tester::#JZ6] 0040 | 74 71 d6 23 | tq.#
[tester::#JZ6]
[tester::#JZ6] $ ./spawn_redis_server.sh --dir /tmp/rdb-2128 --dbfilename apple.rdb
[your_program] All imports completed
[your_program] === STARTUP DEBUG ===
[your_program] process.argv: [ “/usr/local/bin/bun”, “/app/app/main.ts”, “–dir”, “/tmp/rdb-2128”, “–dbfilename”,
[your_program] “apple.rdb”
[your_program] ]
[your_program] Process error handlers set
[your_program] Created basic objects
[your_program] Created command handler
[your_program] Created dispatcher
[your_program] Started expiration check task
[your_program] About to call detectServerRole…
[your_program] detectServerRole succeeded: {
[your_program] port: 6379,
[your_program] role: “master”,
[your_program] masterHost: null,
[your_program] masterPort: null,
[your_program] dir: “/tmp/rdb-2128”,
[your_program] dbfilename: “apple.rdb”,
[your_program] }
[your_program] Setting serverInfo…
[your_program] serverInfo set: {
[your_program] role: “master”,
[your_program] masterHost: null,
[your_program] masterPort: null,
[your_program] master_replid: “8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb”,
[your_program] master_repl_offset: 0,
[your_program] replicas: ,
[your_program] dir: “/tmp/rdb-2128”,
[your_program] dbfilename: “apple.rdb”,
[your_program] }
[your_program] Creating server…
[your_program] Server created, about to listen…
[your_program] Server listen call made, waiting for callback…
[your_program] Server is listening on :6379
[your_program] Loading RDB file from /tmp/rdb-2128/apple.rdb
[your_program] Starting RDB parse. Buffer length: 68
[your_program] RDB version: 0011
[your_program] Main loop: cursor=9, remaining=59
[your_program] Reading opcode: 0xfa at position 9
[your_program] Processing metadata section
[your_program] Parsing metadata at cursor 9
[your_program] parseStringEncoded: firstByte=0x9, firstTwoBits=0b0, cursor=10
[your_program] String length: 9
[your_program] Parsed string: “redis-ver” (length=9)
[your_program] Parsed metadata key: “redis-ver”
[your_program] parseStringEncoded: firstByte=0x5, firstTwoBits=0b0, cursor=20
[your_program] String length: 5
[your_program] Parsed string: “7.2.0” (length=5)
[your_program] Parsed metadata value: “7.2.0”
[your_program] Metadata: redis-ver = 7.2.0, cursor now at 26
[your_program] Main loop: cursor=26, remaining=42
[your_program] Reading opcode: 0xfa at position 26
[your_program] Processing metadata section
[your_program] Parsing metadata at cursor 26
[your_program] parseStringEncoded: firstByte=0xa, firstTwoBits=0b0, cursor=27
[your_program] String length: 10
[your_program] Parsed string: “redis-bits” (length=10)
[your_program] Parsed metadata key: “redis-bits”
[your_program] parseStringEncoded: firstByte=0xc0, firstTwoBits=0b11, cursor=38
[your_program] Special encoding type: 0
[your_program] Parsed 8-bit integer: 64
[your_program] Parsed metadata value: “64”
[your_program] Metadata: redis-bits = 64, cursor now at 40
[your_program] Main loop: cursor=40, remaining=28
[your_program] Reading opcode: 0xfe at position 40
[your_program] Processing database section
[your_program] Parsing database section at cursor 40
[your_program] Selected database: 0, cursor now at 42
[your_program] Next opcode after DB selection: 0xfb
[your_program] DB resize hints: ht_size=1, exp_ht_size=0, cursor now at 45
[your_program] In KV loop: opcode=0x0 at cursor=45
[your_program] Parsing key-value pair at cursor 45
[your_program] Initial opcode: 0x0
[your_program] Processing value type: 0x0
[your_program] Cursor after consuming value type: 46
[your_program] parseStringEncoded: firstByte=0x6, firstTwoBits=0b0, cursor=46
[your_program] String length: 6
[your_program] Parsed string: “banana” (length=6)
[your_program] Parsed key: “banana”, cursor now at 53
[your_program] parseStringEncoded: firstByte=0x5, firstTwoBits=0b0, cursor=53
[your_program] String length: 5
[your_program] Parsed string: “grape” (length=5)
[your_program] Parsed value: “grape”, cursor now at 59
[your_program] Storing key-value pair: banana = grape
[your_program] In KV loop: opcode=0xff at cursor=59
[your_program] Found section marker, exiting KV loop
[your_program] Main loop: cursor=59, remaining=9
[your_program] Reading opcode: 0xff at position 59
[your_program] Reached EOF.
[your_program] RDB parsing completed successfully
[your_program] Server startup completed successfully
[your_program] Server ‘listening’ event fired
[tester::#JZ6] [client] $ redis-cli KEYS *
[tester::#JZ6] [client] Sent bytes: “2\r\n$4\r\nKEYS\r\n$1\r\n\r\n”
[your_program] New connection established
[your_program] globalCommandHandler response: *1
[your_program] $6
[your_program] banana
[your_program]
[tester::#JZ6] [client] Received bytes: “*1\r\n$6\r\nbanana\r\n”
[tester::#JZ6] [client] Received RESP array: [“banana”]
[tester::#JZ6] [client] Received [“banana”]
[tester::#JZ6] Test passed.
[tester::#JZ6] Terminating program
[tester::#JZ6] Program terminated successfully

edit: I was trying again and again and again and it finally worked. Now I’m at #GC6 and it’s the same error. Could it be because maybe my parser is failing and it’s killing the whole thing? But I’ve got error handling everywhere, in case something fails during parsing it should log it.

Basically what I’d like to know if is there’s something wrong with the tester at the moment or not so that I focus on refactoring my codebase. It all runs perfectly when I run it locally and with –previous flag. It only fails on submit and test. Is this a “me” issue or not?

Hey @giraffe-529, looks like Bun 1.1 was starting up too slowly for some reason.

I’ve upgraded the buildpack for your repo to use Bun 1.2 and pushed a commit to it.

You can run git pull to sync the changes. Let me know if you’re still running into issues!

1 Like

Thank you! I’ve been absent for quite a while, totally forgot I might need to upgrade the Bun version.

1 Like

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