Getting EOF after requesting 12 blocks successfully

For some reason I’m getting an EOF error when trying to read from the connection after several successful blocks.

The actual number of blocks I’m able to fetch is consistent and dependent on the actual piece, for example for {Length:2549700 Name:itsworking.gif PieceLength:262144 I manage 12 blocks.

Logs look like this:

[your_program] Fetching block 1 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 2 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 3 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 4 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 5 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 6 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 7 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 8 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 9 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 10 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 11 of 16 of length: 16384
[your_program] Received piece message with length: 16393
[your_program] Fetching block 12 of 16 of length: 16384
[your_program] panic: EOF
[your_program] 
[your_program] goroutine 1 [running]:
[your_program] main.check(...)
[your_program] 	/app/cmd/mybittorrent/common.go:5
[your_program] main.(*Peer).ReadMessage(0x47514a?)
[your_program] 	/app/cmd/mybittorrent/peers.go:115 +0x274
[your_program] main.(*Peer).fetchBlock(0xc0000ddaa0, 0x86020?, 0xc0?, 0x6c963b?)
[your_program] 	/app/cmd/mybittorrent/peers.go:148 +0xc5
[your_program] main.(*Peer).fetchBlocks(0xc0000ddaa0, 0x9, 0x40000)
[your_program] 	/app/cmd/mybittorrent/peers.go:165 +0x26a
[your_program] main.(*Peer).DownloadPiece(0xc0000ddaa0, {0x7386a0, 0xc000044020}, 0x9, 0x40000)
[your_program] 	/app/cmd/mybittorrent/peers.go:193 +0x2f4
[your_program] main.downloadPiece({0x7ffd6364f831?, 0x1?}, {0x7ffd6364f811, 0x1f}, 0x9)
[your_program] 	/app/cmd/mybittorrent/download.go:23 +0x205
[your_program] main.main()
[your_program] 	/app/cmd/mybittorrent/main.go:139 +0x976
[tester::#ND2] Application didn't terminate successfully without errors. Expected 0 as exit code, got: 2

Here’s my code: codecrafters-bittorrent-go/cmd/mybittorrent/peers.go at master · arashout/codecrafters-bittorrent-go · GitHub

I’ll take a look shortly.

1 Like

Hi @arashout, there might be multiple issues at play here. Let’s address the first two I noticed:

  1. Message Length Prefix: The value of the message length prefix should not include itself. Therefore, it should be calculated as length - 4.

  1. Last Piece Length: The code doesn’t account for the fact that the length of the last piece could be shorter than the standard piece length.
  • Example: If the file length is 10 bytes and the piece length is 3 bytes, the last piece would only contain 1 byte instead of the full 3.

Okay @andy1li I implemented the first fix since it’s simple.

For the second fix, I actually hadn’t even got that far. Right now the issue is downloading the blocks themselves.

Even after making the changes I still have issues downloading past the 12th block for example in the itsworking.gif example.

[your_program] Received piece message with length: 16393
[your_program] Fetching block 12 of 16 of length: 16384
[your_program] panic: EOF
[your_program] 
[your_program] goroutine 1 [running]:

Like I’m only downloading a single piece right now

Oh I see what you are saying, even in the DownloadPiece command you could get the last index for example and that would require re-calculating?

Yep, if the last piece is shorter, you don’t need to download as many blocks as for standard pieces.

Let me know if you need further assistance.

Thanks for the help! I got it working but calculating if “it is actually the last piece”

I didn’t realize there would be chance we would get the last piece index in the command, I thought that would be something handled in the next step

Thanks!

1 Like

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