anacrolix / torrent

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

Panic cause by a specific magnet. #798

Closed Kidsunbo closed 1 year ago

Kidsunbo commented 1 year ago

Hi, I use a magnet to download ubuntu for test purpose. But it panics and shows the following traceback.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x400 pc=0x41cef85]

goroutine 142 [running]:
crypto/rc4.(*Cipher).XORKeyStream(0x45d02ed?, {0xc000ab2000?, 0x0?, 0xc0007e7b38?}, {0xc001bb1fff?, 0x0?, 0xc0007e7b50?})
        /usr/local/opt/go/libexec/src/crypto/rc4/rc4.go:68 +0x65
github.com/anacrolix/torrent/mse.(*cipherWriter).Write(0xc0005b3920, {0xc001bb1fff?, 0xffff, 0x2a3f7?})
        /Users/kid/go/pkg/mod/github.com/anacrolix/torrent@v1.47.0/mse/mse.go:136 +0xdd
github.com/anacrolix/torrent.(*peerConnMsgWriter).run(0xc000cd0440, 0xdf8475800)
        /Users/kid/go/pkg/mod/github.com/anacrolix/torrent@v1.47.0/peer-conn-msg-writer.go:103 +0x4f2
github.com/anacrolix/torrent.(*PeerConn).messageWriterRunner(0xc0000da000?)
        /Users/kid/go/pkg/mod/github.com/anacrolix/torrent@v1.47.0/peer-conn-msg-writer.go:47 +0xd8
created by github.com/anacrolix/torrent.(*PeerConn).startMessageWriter
        /Users/kid/go/pkg/mod/github.com/anacrolix/torrent@v1.47.0/peer-conn-msg-writer.go:40 +0x5b

I've tried dozens of times and most of time it occurs. I simplify the code and I think the following code could reproduce it.

package main

import (
    "fmt"
    "github.com/anacrolix/torrent"
)

func main() {
    config := torrent.NewDefaultClientConfig()
    config.DataDir = "./output"
    c, _ := torrent.NewClient(config)
    defer c.Close()
    t, _ := c.AddMagnet("magnet:?xt=urn:btih:99c82bb73505a3c0b453f9fa0e881d6e5a32a0c1&tr=https%3A%2F%2Ftorrent.ubuntu.com%2Fannounce&tr=https%3A%2F%2Fipv6.torrent.ubuntu.com%2Fannounce")
    <-t.GotInfo()
    fmt.Println("start downloading")
    t.DownloadAll()
    c.WaitAll()
}

I notice that this only occurs when the file has been downloaded first. Then almost all the time I run the app, it panics. And BTW, is it safe to put c.WaitAll() into another goroutine?

anacrolix commented 1 year ago

Thanks, I can reproduce this. It looks like it was introduced by 54665f160ab37ffdd5625b8b0c4e9599150f8ce6.

anacrolix commented 1 year ago

Sorry yes, it is safe to put WaitAll in another goroutine.