Cannot finish the last stage (handshake fails for each peer)

Hi,
I have some strange problem on last stage JV8. My client cannot finish handshake with any of available peers (connection is reset by peer when I try to read from it after handshake message is sent)
It happens only during testing. I found torrent file, which is used for testing and successfully downloaded gif locally with same command used for testing. I am not sure why it fails. Maybe some problem with random ports? I try to make handshake with each found peer by peers command. When I run download locally by go run cmd/mybittorrent/main.go download -o codercat.gif codercat.torrent it works for each discovered peer [178.62.85.20:51426 165.232.33.77:51429 178.62.82.89:51431]. I did not have any other problem in previous stages and that’s why it seems strange to me.

Here are actually only logs I got from test runner:

[compile] Compilation successful.
Debug = true
[tester::#JV8] Running tests for Stage #JV8 (Download the whole file)
[tester::#JV8] Running ./your_bittorrent.sh download -o /tmp/torrents2026873641/itsworking.gif /tmp/torrents2026873641/itsworking.gif.torrent
[your_program] [178.62.85.20:51493 165.232.33.77:51495 178.62.82.89:51415]
[your_program] Error reading from connection: read tcp 172.19.139.130:40216->178.62.85.20:51493: read: connection reset by peer
[your_program] Handshake unsuccessful: read tcp 172.19.139.130:40216->178.62.85.20:51493: read: connection reset by peer
[your_program] Error reading from connection: read tcp 172.19.139.130:50310->165.232.33.77:51495: read: connection reset by peer
[your_program] Handshake unsuccessful: read tcp 172.19.139.130:50310->165.232.33.77:51495: read: connection reset by peer
[your_program] Error reading from connection: read tcp 172.19.139.130:58778->178.62.82.89:51415: read: connection reset by peer
[your_program] Handshake unsuccessful: read tcp 172.19.139.130:58778->178.62.82.89:51415: read: connection reset by peer
[your_program] Cannot establish handshake with any peer
[tester::#JV8] Application didn't terminate successfully without errors. Expected 0 as exit code, got: 1
[tester::#JV8] Test failed
5 Likes

I have the same error =(

Handshake fails for the previous stage too (#ND2), same error.

The test case for the handshake stage #CA4 connects to a local peer so it passes using the same code.

1 Like

Have same error. Wonder if it’s an error with the challenge since I pass all the previous tests. I’m on #ND2 (Download a Piece). Can download pieces locally using the sample torrent. But when running the tests it fails

@thelizri @sjsanc @yehor-dykhov @Babihac thanks for reporting this!

Could you please try the files here: bittorrent-tester/torrents at main · codecrafters-io/bittorrent-tester · GitHub, and see if it is still not replicable locally? The original post here mentions that codercat.gif works locally, but the test failure is using itsworking.gif. This’ll help us rule out whether it’s a case that specific files end up triggering.

1 Like

Thanks for tip! itsworking.gif.torrent also works well locally.Then I tried to rerun tests and it already passed :sweat_smile:. However, there still could be some problem, because some peers are still closing connections, but easy workaround is just iterate over all peers and find one for which handshake is successful, so it’s nothing critical.

I can torrent all those locally, but the codecrafters test still fail.

I’m having exactly same error, I’m able to run the commands and download files when I try it locally with provided .torrent files

However, there still could be some problem, because some peers are still closing connections,

This isn’t expected, the peers we provision aren’t configured to fail but they will close connections if they receive invalid messages / inputs (we use off-the-shelf OSS trackers/peers for this).

@thelizri & @lukaaduashvili could you share your code + logs please? We’ll take a look at this. Here’s how you can publish your code to GitHub: Publish to GitHub - CodeCrafters

Did you guys fix it? I passed the test when I ran it just now. I haven’t made any changes.

I can still publish my code to github if you need it

We haven’t pushed any changes, it’s possible that this is a bug that is intermittent and works sometimes and not otherwise. Once we have code access we’ll try to figure it out for you!

Okay, cool. I have published my code to github now

Hi, I am having this same issue on testing step 9. The peer is always closing the connection before sending back the handshake. However, when running it on my end with go run cmd/mybittorrent/main.go download_piece -o out itsworking.gif.torrent 0 it works and the piece is correctly downloaded after checking against the original files, for both sample.torrent and itsworking.gif.torrent

Update: Before I was only seeing connection reset by peer errors however, the last time I tried running the test it was simply EOF received while reading the handshake. The code was still the same (I am almost certain)

@thelizri we tried testing your code published on GitHub and that seemed to pass the download_piece stage (we tried multiple times). The last stage isn’t implemented, so I’m assuming all’s good on your end.

@gazev mind sharing your code? We’ll take a look at this for you.

(Same to the others - please share your code!)

Thank you for the help.
Here’s the repo GitHub - gazev/tmp

Hi @rohitpaulk I’m still having the issue on second to last stage.
Here’s the repo: GitHub - lukaaduashvili/temp

I have the same issue. Locally I am able to download pieces individually as well as a whole file and everything hashes correctly, however one of the public peers (161.35.46.221:51414) refuses to handshake. Using wireshark I can see that the TCP connection is reset by the peer after my handshake is sent.

On the CI tests my code passes Stage #ND2 (Download a piece) and then fails Stage #CA4 (Peer handshake).

Hello @rohitpaulk , I’m also failing to make #ND2 pass, however it works when I run the torrents locally.

Here’s my repo https://github.com/afresquet/codecrafters-bittorrent-rust

Hi afresquet,
Peers will not accept multiple connections with the same peer_id.
You’re using a constant ID 00112233445566778899 instead of a random id, so you’re more likely to collide with another Codecrafters user using this peer_id. Apologies for this, course description will be updated soon.

There’s another issue at the moment where peers don’t accept multiple connections from the same IP address and multiple test runner instances can share the same IP address. We’re putting out a fix for this issue soon.

I wonder if your public peers are also doing some kind of IP-banning as well, since I still can’t handshake to 161.35.46.221:51414… but if I use a VPN it works!

Doesn’t explain why the localhost tracker used by the test runner doesn’t work though.