# Unable to receive bitfield message

**URL:** <https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [October 7, 2024, 10:12am UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232 "2024-10-07T10:12:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![malay44](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/malay44/32/2194_2.png) [@malay44](https://forum.codecrafters.io/u/malay44)\
**Post date:** [October 7, 2024, 10:12am UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/1 "2024-10-07T10:12:28Z")

</div>

I’m stuck on Stage #ND2.

code runs smoothly on my machine, but fails during test.  
This was not the case few days back, when I was debugging the issue with hash matching of downloaded piece

Here are my logs:

# **Test logs**

\codecrafters-bittorrent-go 🍣 master 📦 📝 ×1​:railway\_track: ×1�� 💨 ×2via 🐹 v1.22.0 🐏 5GiB/8GiB took 1m37s  
❯ codecrafters test  
Initiating test run…

Running tests. Logs should appear shortly…

[compile] Compilation successful.

Debug = true

[tester::#ND2] Running tests for Stage #ND2 (Download a piece)  
[tester::#ND2] Running ./your\_bittorrent.sh download\_piece -o /tmp/torrents329709923/piece-9 /tmp/torrents329709923/itsworking.gif.torrent 9  
[your\_program] Starting retrieval for piece 9 from torrent file /tmp/torrents329709923/itsworking.gif.torrent  
[your\_program] Parsed torrent file successfully  
[your\_program] Tracker URL: [http://bittorrent-test-tracker.codecrafters.io/announce](http://bittorrent-test-tracker.codecrafters.io/announce)  
[your\_program] Length: 2549700  
[your\_program] Info Hash: 70edcac2611a8829ebf467a6849f5d8408d9d8f4  
[your\_program] Piece Length: 262144  
[your\_program] Piece Hashes:  
[your\_program] 01cc17bbe60fa5a52f64bd5f5b64d99286d50aa5  
[your\_program] 838f703cf7f6f08d1c497ed390df78f90d5f7566  
[your\_program] 45bf10974b5816491e30628b78a382ca36c4e05f  
[your\_program] 84be4bd855b34bcedc0c6e98f66d3e7c63353d1e  
[your\_program] 86427ac94d6e4f21a6d0d6c8b7ffa4c393c3b131  
[your\_program] 7c70cd5f44d1ac5505cb855d526ceb0f5f1cd5e3  
[your\_program] 3796ab05af1fa874173a0a6c1298625ad47b4fe6  
[your\_program] 272a8ff8fc865b053d974a78681414b38077d7b1  
[your\_program] b07128d3a6018062bfe779db96d3a93c05fb81d4  
[your\_program] 7affc94f0985b985eb888a36ec92652821a21be4  
[your\_program] Retrieved peer list: [161.35.46.221:51520 159.65.84.183:51423 167.172.57.188:51544]  
[your\_program] Handshake with peer successful  
[your\_program] Waiting for message with ID 5  
[your\_program] Error reading length prefix: EOF  
[your\_program] Error waiting for bitfield message: EOF  
[your\_program] connection closed  
[your\_program] EOF  
[tester::#ND2] Connection reset by peer or EOF type of errors can be transient issues (try again), but they can also happen when peers receive unexpected messages (sending a REQUEST message with an incorrect offset/length, block size larger than 16 kb etc.)  
[tester::#ND2] Last piece of the file can be less than piece length specified in torrent metadata. For instance, the length of this piece is 190404  
[tester::#ND2] File does not exist: /tmp/torrents329709923/piece-9  
[tester::#ND2] Test failed

# **Local logs**

codecrafters-bittorrent-go 🍣 master 📦 📝 ×1​:railway\_track: ×1�� 💨 ×2via 🐹 v1.22.0 🐏 6GiB/8GiB took 10s  
❯ ./your\_bittorrent.sh download\_piece -o ./tmp/piece-9 ./itsworking.gif.torrent 9  
Starting retrieval for piece 9 from torrent file ./itsworking.gif.torrent  
Parsed torrent file successfully  
Tracker URL: [http://bittorrent-test-tracker.codecrafters.io/announce](http://bittorrent-test-tracker.codecrafters.io/announce)  
Length: 2549700  
Info Hash: 70edcac2611a8829ebf467a6849f5d8408d9d8f4  
Piece Length: 262144  
Piece Hashes:  
01cc17bbe60fa5a52f64bd5f5b64d99286d50aa5  
838f703cf7f6f08d1c497ed390df78f90d5f7566  
45bf10974b5816491e30628b78a382ca36c4e05f  
84be4bd855b34bcedc0c6e98f66d3e7c63353d1e  
86427ac94d6e4f21a6d0d6c8b7ffa4c393c3b131  
7c70cd5f44d1ac5505cb855d526ceb0f5f1cd5e3  
3796ab05af1fa874173a0a6c1298625ad47b4fe6  
272a8ff8fc865b053d974a78681414b38077d7b1  
b07128d3a6018062bfe779db96d3a93c05fb81d4  
7affc94f0985b985eb888a36ec92652821a21be4  
Retrieved peer list: [161.35.46.221:51520 159.65.84.183:51423 167.172.57.188:51544]  
Handshake with peer successful  
Waiting for message with ID 5  
Received payload with ID 5, length 2  
Received bitfield message  
Sending interested message to peer  
Waiting for message with ID 1  
Received payload with ID 1, length 0  
Received unchoke message  
payload: [0 0 0 9 0 0 0 0 0 0 64 0]  
Sending request for block at offset 0 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 0 64 0 0 0 64 0]  
Sending request for block at offset 16384 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 0 128 0 0 0 64 0]  
Sending request for block at offset 32768 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 0 192 0 0 0 64 0]  
Sending request for block at offset 49152 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 1 0 0 0 0 64 0]  
Sending request for block at offset 65536 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 1 64 0 0 0 64 0]  
Sending request for block at offset 81920 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 1 128 0 0 0 64 0]  
Sending request for block at offset 98304 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 1 192 0 0 0 64 0]  
Sending request for block at offset 114688 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 2 0 0 0 0 64 0]  
Sending request for block at offset 131072 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 2 64 0 0 0 64 0]  
Sending request for block at offset 147456 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 2 128 0 0 0 64 0]  
Sending request for block at offset 163840 (length: 16384)  
Created message with ID 6 and payload length 12  
payload: [0 0 0 9 0 2 192 0 0 0 39 196]  
Sending request for block at offset 180224 (length: 10180)  
Created message with ID 6 and payload length 12  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 0–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 16384–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 32768–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 49152–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 65536–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 81920–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 98304–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 114688–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 131072–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 147456–  
Waiting for message with ID 7  
Received payload with ID 7, length 16392  
–Received block data for offset 163840–  
Waiting for message with ID 7  
Received payload with ID 7, length 10188  
–Received block data for offset 180224–  
Piece hash: 7affc94f0985b985eb888a36ec92652821a21be4  
Torrent hash: 7affc94f0985b985eb888a36ec92652821a21be4  
Piece hash verified successfully, writing data to file  
Data written to file successfully at ./tmp/piece-9  
Piece retrieval successful  
connection closed

---

<div class="post-metadata">

**Author:** ![malay44](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/malay44/32/2194_2.png) [@malay44](https://forum.codecrafters.io/u/malay44)\
**Post date:** [October 7, 2024, 10:13am UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/2 "2024-10-07T10:13:02Z")

</div>

And here’s my github repo link:- [GitHub - malay44/ocTorrent](https://github.com/malay44/ocTorrent)

---

<div class="post-metadata">

**Author:** ![malay44](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/malay44/32/2194_2.png) [@malay44](https://forum.codecrafters.io/u/malay44)\
**Post date:** [October 7, 2024, 10:14am UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/3 "2024-10-07T10:14:42Z")

</div>

# Local Run Logs

 ![Screenshot 2024-10-07 at 3.31.59 PM](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/7/7ddf03de71b8e78e63ed38d8474e3aeef5029ae9.png)

---

<div class="post-metadata">

**Author:** ![malay44](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/malay44/32/2194_2.png) [@malay44](https://forum.codecrafters.io/u/malay44)\
**Post date:** [October 7, 2024, 10:15am UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/4 "2024-10-07T10:15:14Z")

</div>

# Test Logs

 ![Screenshot 2024-10-07 at 3.44.25 PM](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/6/6d06c59ff8c076c642fb0c7cd3f0e01362cd75ba.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:** [October 7, 2024, 11:48am UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/5 "2024-10-07T11:48:18Z")

</div>

@malay44 I added a log as shown below and ran your code:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/0/0208829da4228056dd9ea01a6ac59c842c983bc5.png)

It seems that the handshake did not succeed:

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

We’re currently investigating the handshake issue. While it may take some time, I’ll keep you updated once we have a resolution.

---

<div class="post-metadata">

**Author:** ![sarp](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/sarp/32/1082_2.png) [@sarp](https://forum.codecrafters.io/u/sarp)\
**Post date:** [October 8, 2024, 4:57pm UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/7 "2024-10-08T16:57:41Z")

</div>

Hi malay44,  
There are currently two issues that might be causing this:

1. Peers will not accept multiple connections with the same `peer_id`

Can you try again using a random peer\_id instead of `00112233445566778899` to reduce the likelihood of colliding with another Codecrafters user.

1. Peers will not accept multiple connections with the same IP address

Our test runners can share same outgoing IP address, we’re working on a fix to address this issue. If it’s working locally but not on codecrafters, this might be related. As Andy mentioned, we’ll update once we have a resolution.

---

<div class="post-metadata">

**Author:** ![malay44](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/malay44/32/2194_2.png) [@malay44](https://forum.codecrafters.io/u/malay44)\
**Post date:** [October 9, 2024, 1:41pm UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/8 "2024-10-09T13:41:28Z")

</div>

tried using other id (abzxbwkqvglwhrpvfnmx) but it still fails in test

---

<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 11, 2024, 4:03am UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/9 "2024-10-11T04:03:42Z")

</div>

@malay44 Could you give it another try and please let me know how it goes?

> [@Cannot finish the last stage (handshake fails for each peer)](https://forum.codecrafters.io/t/cannot-finish-the-last-stage-handshake-fails-for-each-peer/1274/22):
>
> Hey everyone! We’ve pushed [an update](https://github.com/codecrafters-io/rain/pull/1) that should remove the IP-based rate-limiting. Thanks to @sarp for making this change!
> 
> I’ve also changed our [E2E tests](https://github.com/codecrafters-io/bittorrent-test-tracker/actions) to specifically test for this - earlier they weren’t trying to download the same file in parallel, so we weren’t hitting this failure case.

---

<div class="post-metadata">

**Author:** ![malay44](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/malay44/32/2194_2.png) [@malay44](https://forum.codecrafters.io/u/malay44)\
**Post date:** [October 13, 2024, 7:13am UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/11 "2024-10-13T07:13:19Z")

</div>

it’s working, thanks a lot guys

---

<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:** [October 18, 2024, 7:13am UTC](https://forum.codecrafters.io/t/unable-to-receive-bitfield-message/2232/12 "2024-10-18T07:13:45Z")

</div>

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