# Challenge stage #XV6: cannot figure out the problem (C++)

**URL:** <https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389>\
**Category:** Challenges\
**Tags:** challenge:redis\
**Created:** [October 12, 2025, 2:50pm UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389 "2025-10-12T14:50:20Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 12, 2025, 2:50pm UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/1 "2025-10-12T14:50:20Z")

</div>

Hello there,

I’ve been tackling the Redis challenge for the past few days, and I have been tearing my hair out because multiple stages seem to decide when they want to pass or not, and I have no idea why.

Here are the stages seemingly randomly causing fails:

- ACKs with no commands
- XREAD (blocking with $)
- Command propagation in replicas
- BLPOP (this one I know why, but am not sure how to handle)

If anyone could take a look at this, I would REALLY appreciate it! I will be publishing the code to GitHub so people can try and test it.

Here is the link: [https://github.com/fortwoone/cpp\_redis](https://github.com/fortwoone/cpp_redis)

Thanks in advance for answers, they would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 12, 2025, 5:12pm UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/4 "2025-10-12T17:12:19Z")

</div>

In an effort to fix command propagation, I deduced from the error and received commands that some commands were sent in one TCP segment, instead of one segment per command.  
However, inconsistencies showed up later on (a test failing even when no change was made in the code it was targeting, most often) and it went downhill from there, and I don’t know why, even though I tried to figure it out by myself for a good while before seeking help here

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 13, 2025, 5:37am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/5 "2025-10-13T05:37:59Z")

</div>

Hey @fortwoone, great catch! As you correctly deduced, a single TCP read can include multiple commands:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/1/9/19dc649ebc4839e92392f34ca012fdd9f393a1ce.png)

For instance, the PSYNC response and the RDB payload can arrive in the same read:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/c/5/c5ec9b565225d6259d638abe259168ac1eae80e2.jpeg)

You’ll want to make sure your code can correctly handle such cases.

Let me know if you’d like any further clarification!

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 13, 2025, 6:29am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/7 "2025-10-13T06:29:13Z")

</div>

Thanks for the heads-up! Thing is, I did actually implement a workaround for this, but other stages seem to fail afterwards, and which one makes it fail seems to vary, and I can’t figure out why. I’ve been tearing my hair out on this for the past day

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 13, 2025, 6:34am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/8 "2025-10-13T06:34:51Z")

</div>

@fortwoone There might be multiple issues at play. Let’s tackle them one at a time.

From the screenshot, it looks like your code is attempting to read the RDB payload even when it has already been consumed:

[![](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/c/5/c5ec9b565225d6259d638abe259168ac1eae80e2.jpeg) ](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/c/5/c5ec9b565225d6259d638abe259168ac1eae80e2.jpeg)

Could you try addressing this part first?

* * *

> I can’t figure out why

The reason is simply “a single TCP read can include multiple commands”. Once your code handles that case properly, things should start falling into place.

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 13, 2025, 6:37am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/9 "2025-10-13T06:37:27Z")

</div>

Well, honestly, I’m a bit stumped on this portion. If I comment out the RDB-consuming code, in some instances it works fine, but in others, it just ends up trying to interpret the RDB file as a command (which it isn’t) and throws an error (I did add this exception to ensure commands were actually parsed correctly)

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 13, 2025, 6:51am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/11 "2025-10-13T06:51:22Z")

</div>

@fortwoone Let’s take a step back and walk through it together.

**Problem:** A single TCP read can include multiple commands

**Implication:** We shouldn’t assume each TCP read maps to exactly one command. Namely, we should not hardcode the exact steps.

**Solution:** Making the request/response handling more flexible.

One approach is designing a general-purpose function that processes responses after **every** request. This function should contain a loop that handles an arbitrary number of commands.

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 13, 2025, 6:53am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/12 "2025-10-13T06:53:51Z")

</div>

Okay, so my attempt to resolve this was to memorise the last parsed command’s size so I could then advance from the starting position in the received buffer to parse the next one

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 13, 2025, 7:51am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/13 "2025-10-13T07:51:02Z")

</div>

At least that does seem to work

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 13, 2025, 8:33am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/14 "2025-10-13T08:33:28Z")

</div>

But then other problems creep in and so I get confused. I don’t think this one is the root cause, though

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 13, 2025, 8:48am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/15 "2025-10-13T08:48:17Z")

</div>

@fortwoone Yes, there might be a few different things at play. What errors are you seeing now?

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 13, 2025, 8:54am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/16 "2025-10-13T08:54:28Z")

</div>

XREAD blocking with $ fails, and sometimes command propagation fails even though the code is identical

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 13, 2025, 8:59am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/17 "2025-10-13T08:59:00Z")

</div>

> XREAD blocking with $ fails,

This might be a separate issue introduced by the recent fix.

> and sometimes command propagation fails even though the code is identical

If it’s non-deterministic, it’s likely due to the same issue: multiple commands arriving in a single TCP read.

* * *

@fortwoone If you push your latest code, I’ll try taking another look tomorrow.

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 13, 2025, 9:06am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/18 "2025-10-13T09:06:58Z")

</div>

There

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 14, 2025, 6:51am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/20 "2025-10-14T06:51:39Z")

</div>

@andy1li code was pushed

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [October 15, 2025, 11:59am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/21 "2025-10-15T11:59:57Z")

</div>

@andy1li were you able to find something?

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 10, 2025, 6:27am UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/23 "2025-11-10T06:27:14Z")

</div>

@fortwoone Looks like the response to the `XREAD BLOCK` command wasn’t sent after `XADD` was processed.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/8/0/8074c8b97f471f48563fbd124c214da56ba46b0a.png)

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 16, 2025, 1:41pm UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/24 "2025-11-16T13:41:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [November 21, 2025, 1:41pm UTC](https://forum.codecrafters.io/t/challenge-stage-xv6-cannot-figure-out-the-problem-c/15389/25 "2025-11-21T13:41:58Z")

</div>

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