From fed765b2a058a5c3b2dc8905edd07638cbcfe3a5 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 25 Dec 2022 19:23:07 +1100 Subject: [PATCH] Fix write error handling Fixes https://github.com/anacrolix/torrent/issues/798. Prior to this fix, it looks like the writer would just keep writing chunks of the front buffer (incorrectly if there was an error), until presumably the writer would be killed by read hangup elsewhere. --- peer-conn-msg-writer.go | 3 +++ tests/issue-798/main.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/issue-798/main.go diff --git a/peer-conn-msg-writer.go b/peer-conn-msg-writer.go index 0dbc4ead..1bacc59d 100644 --- a/peer-conn-msg-writer.go +++ b/peer-conn-msg-writer.go @@ -104,6 +104,9 @@ func (cn *peerConnMsgWriter) run(keepAliveTimeout time.Duration) { if err == nil && n != len(next) { panic("expected full write") } + if err != nil { + break + } } if err != nil { cn.logger.WithDefaultLevel(log.Debug).Printf("error writing: %v", err) diff --git a/tests/issue-798/main.go b/tests/issue-798/main.go new file mode 100644 index 00000000..3b9ad645 --- /dev/null +++ b/tests/issue-798/main.go @@ -0,0 +1,18 @@ +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() +} \ No newline at end of file