# \#ND2 Connection reset by peer on second pass

**URL:** <https://forum.codecrafters.io/t/nd2-connection-reset-by-peer-on-second-pass/10412>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [May 12, 2025, 11:10am UTC](https://forum.codecrafters.io/t/nd2-connection-reset-by-peer-on-second-pass/10412 "2025-05-12T11:10:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptrhcpang](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ptrhcpang/32/1054_2.png) [@ptrhcpang](https://forum.codecrafters.io/u/ptrhcpang)\
**Post date:** [May 12, 2025, 11:10am UTC](https://forum.codecrafters.io/t/nd2-connection-reset-by-peer-on-second-pass/10412/1 "2025-05-12T11:10:28Z")

</div>

I’m stuck on Stage #ND2.

This is a problem with my connection being reset by peer and then timing out as the tester tries to download a second piece. A number of other posts have discussed this problem, but it appears to have been two primary problems — one with tester, which has been corrected, and one with the GET request URL in which the peer\_id part of the URL is static.

I have addressed this static peer\_id problem by randomising the peer\_id. But I seem always to get stuck with a connection reset when the tester tries to download a second piece.

In my code, I printed out the download piece hash and the known piece hash, showing that the first piece download is successful.

Here are my logs:

 ![Screenshot from 2025-05-12 12-54-46](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/e/e08cb4cd100aacc54750cd3eec15c6c3c4a904bc.png)

Would truly appreciate ideas as to what can be going wrong. I am not even sure which part of the code I ought to share! (Though I should also mention the multiple peer upload/download mechanism is built with epoll.) Thank you.

---

<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:** [May 12, 2025, 10:47pm UTC](https://forum.codecrafters.io/t/nd2-connection-reset-by-peer-on-second-pass/10412/5 "2025-05-12T22:47:29Z")

</div>

Hey @ptrhcpang, the issue seems to be that the code isn’t accounting for this edge case:

- The **final block** in a piece can be **smaller** than the preceding ones.

For example, in the case below, block 3 is only 34,460 bytes instead of 262,144:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/b/b96c24901929679fce548f09e54d6aad8184cd8f.jpeg)

Let me know if you’d like further clarification!

---

<div class="post-metadata">

**Author:** ![ptrhcpang](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ptrhcpang/32/1054_2.png) [@ptrhcpang](https://forum.codecrafters.io/u/ptrhcpang)\
**Post date:** [May 13, 2025, 2:39am UTC](https://forum.codecrafters.io/t/nd2-connection-reset-by-peer-on-second-pass/10412/7 "2025-05-13T02:39:23Z")

</div>

Ah, I see – thank you so much! My primary mistake was that in some parts of the code (though not others), I thought the piece index started with 1 instead of 0, like every other count does. (But yes, though I did especially calculate the length of the final block of the final piece, that calculation was slightly incorrect as well…) Thank you again!

---

<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:** [May 25, 2025, 7:27am UTC](https://forum.codecrafters.io/t/nd2-connection-reset-by-peer-on-second-pass/10412/8 "2025-05-25T07:27:05Z")

</div>

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