#ND2 Connection reset by peer on second pass

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:

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.

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:

Let me know if you’d like further clarification!

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!

1 Like