I’m stuck on Stage #ND2.
I’m encountering intermittent issues while working on Stage #ND2 (“Download a piece”) of my BitTorrent client. The download process often stops at different offsets when fetching blocks from the second file.
The issue doesn’t consistently occur at the same block offset—it varies with each run.
Here are my logs:
Initiating test run...
⚡ This is a turbo test run. https://codecrafters.io/turbo
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/torrents2434277296/piece-9 /tmp/torrents2434277296/itsworking.gif.torrent 9
[your_program] (node:360) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
[your_program] (Use `node --trace-deprecation ...` to show where the warning was created)
[your_program] file length: 2549700
[your_program] piece length: 262144
[your_program] number of pieces: 10
[your_program] peers [
[your_program] { host: '165.232.41.73', port: 51418 },
[your_program] { host: '165.232.38.164', port: 51450 },
[your_program] { host: '165.232.35.114', port: 51419 }
[your_program] ]
[your_program] Sending handshake message
[your_program] Connected to 165.232.38.164:51450
[your_program] Response received: 68 bytes
[your_program] Handshake successful
[your_program] Sending interested message
[your_program] Response received: 7 bytes
[your_program] Unknown message ID from peer: 5
[your_program] Response received: 5 bytes
[your_program] Unchoke received
[your_program] Initialised communication with peer in 250 ms
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 0, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 16384, Block size: 16384
[your_program] Sending 2 request messages to peer
[your_program] Pausing for 400 ms to avoid being rate limited
[your_program] Response received: 13680 bytes
[your_program] Response received: 19114 bytes
[your_program] Successfully fetched block. Piece index: 9, Block offset: 0, Block size: 16384
[your_program] Successfully fetched block. Piece index: 9, Block offset: 16384, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 32768, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 49152, Block size: 16384
[your_program] Sending 2 request messages to peer
[your_program] Pausing for 400 ms to avoid being rate limited
[your_program] Response received: 23256 bytes
[your_program] Successfully fetched block. Piece index: 9, Block offset: 32768, Block size: 16384
[your_program] Response received: 9538 bytes
[your_program] Successfully fetched block. Piece index: 9, Block offset: 49152, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 65536, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 81920, Block size: 16384
[your_program] Sending 2 request messages to peer
[your_program] Pausing for 400 ms to avoid being rate limited
[your_program] Response received: 23256 bytes
[your_program] Successfully fetched block. Piece index: 9, Block offset: 65536, Block size: 16384
[your_program] Response received: 9538 bytes
[your_program] Successfully fetched block. Piece index: 9, Block offset: 81920, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 98304, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 114688, Block size: 16384
[your_program] Sending 2 request messages to peer
[your_program] Pausing for 400 ms to avoid being rate limited
[your_program] Response received: 27360 bytes
[your_program] Successfully fetched block. Piece index: 9, Block offset: 98304, Block size: 16384
[your_program] Response received: 5434 bytes
[your_program] Successfully fetched block. Piece index: 9, Block offset: 114688, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 131072, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 147456, Block size: 16384
[your_program] Sending 2 request messages to peer
[your_program] Pausing for 400 ms to avoid being rate limited
[your_program] Response received: 30096 bytes
[your_program] Successfully fetched block. Piece index: 9, Block offset: 131072, Block size: 16384
[your_program] Response received: 2698 bytes
[your_program] Successfully fetched block. Piece index: 9, Block offset: 147456, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 163840, Block size: 16384
[your_program] Adding block request to outgoing buffer. Piece index 9, Block offset: 180224, Block size: 16384
[your_program] Sending 2 request messages to peer
[your_program] Pausing for 400 ms to avoid being rate limited
[your_program] Connection end
[your_program] Connection closed after 2255 ms
[tester::#ND2] timed out, test exceeded 10 seconds
[tester::#ND2] Test failed
I’ve pushed my latest changes to the Git repository.