anacrolix / torrent

Full-featured BitTorrent client package and utilities
Mozilla Public License 2.0
5.39k stars 615 forks source link

Race from peerconn on data read of closed torrent with torrent@v1.52.5 #863

Closed tsynik closed 10 months ago

tsynik commented 10 months ago

We have this panic report from TorrServer user:

...
180 lines:
[2023-08-25 17:46:56 +0000  WRN] *torrent.PeerConn 0xb934eb00: error reading chunk for peer Request {749 {1179648 16384}}: EOF [github.com/anacrolix/torrent peerconn.go:627]
120 lines:
[2023-08-25 17:48:36 +0000  WRN] *torrent.PeerConn 0xb8e13700: error reading chunk for peer Request {768 {2359296 16384}}: EOF [github.com/anacrolix/torrent peerconn.go:627]
190 lines:
[2023-08-25 17:53:50 +0000  WRN] *torrent.PeerConn 0xb9acd080: error reading chunk for peer Request {826 {4177920 16384}}: EOF [github.com/anacrolix/torrent peerconn.go:627]
2023/08/25 17:56:09 UTC0 Torrent close by timeout c28499c6d3c88cff43b19bcfb56676325a4e1ca4
2023/08/25 17:56:09 UTC0 Close cache for: c28499c6d3c88cff43b19bcfb56676325a4e1ca4
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x1c pc=0xb2d187ac]

goroutine 4660507 [running]:
github.com/anacrolix/torrent/storage.Piece.ReadAt({{0x0, 0x0}, {0xb4ca7590, 0x35a}}, {0xb6b22000, 0x4000, 0x4000}, 0x15c000)
    /Users/yourok/go/pkg/mod/github.com/anacrolix/torrent@v1.52.5/storage/wrappers.go:84 +0x210
github.com/anacrolix/torrent.(*Torrent).readAt(0xb4fe8600, {0xb6b22000, 0x4000, 0x4000}, 0xd695c000)
    /Users/yourok/go/pkg/mod/github.com/anacrolix/torrent@v1.52.5/torrent.go:1433 +0x17c
github.com/anacrolix/torrent.(*PeerConn).readPeerRequestData(0xb987c100, {0x35a, {0x15c000, 0x4000}})
    /Users/yourok/go/pkg/mod/github.com/anacrolix/torrent@v1.52.5/peerconn.go:661 +0x94
github.com/anacrolix/torrent.(*PeerConn).peerRequestDataReader(0xb987c100, {0x35a, {0x15c000, 0x4000}}, 0xb95f20b0)
    /Users/yourok/go/pkg/mod/github.com/anacrolix/torrent@v1.52.5/peerconn.go:601 +0x200
created by github.com/anacrolix/torrent.(*PeerConn).onReadRequest in goroutine 4658458
    /Users/yourok/go/pkg/mod/github.com/anacrolix/torrent@v1.52.5/peerconn.go:586 +0x764

and also many "error reading chunk for peer Request XXX: EOF" warnings before crash, which must be on Debug level, but they don't. According this commit https://github.com/anacrolix/torrent/commit/08c403ea52153a22d6f599e03741fbfce4a0e492 probably it must depend on Torrent closure in peerRequestDataReader or later to avoid crashes.

another report:

peerc

anacrolix commented 10 months ago

Hi @tsynik. There may be an issue around closing, but the immediate cause of the issue above is https://github.com/YouROK/TorrServer/blob/34634649024b06921e7f2bb068da4a4ad35fabe4/server/torr/storage/torrstor/cache.go#L86. It's not valid to return nil here to anacrolix/torrent.

tsynik commented 10 months ago

Hi @anacrolix. Thx for point to TorrServer code, I'll dig into it. And to mute read chunks errors logging we need add Capacity like so: https://github.com/YouROK/TorrServer/commit/e480ecb09388042fe13485678ba8b3d8d6ddb383?

anacrolix commented 10 months ago

Yes I do recall something about masking an error if the storage knows there's a limited capacity. That way it doesn't get confused when pieces go missing.

tsynik commented 10 months ago

@anacrolix looks like we fixed this returning fake piece instead of nil: https://github.com/YouROK/TorrServer/commit/dd26ff3d5f762a946fe3bafef9ea3ae6d47526cf Thanks for suggestion!