From 8a43910e2d81a5202373bab8e2cbc3cbbf2ceb23 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 2 Feb 2018 13:36:18 +1100 Subject: [PATCH] connection.writer wasn't checking closed state --- connection.go | 7 +++++++ torrent.go | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/connection.go b/connection.go index 3165e46f..46083d56 100644 --- a/connection.go +++ b/connection.go @@ -348,6 +348,7 @@ func (cn *connection) SetInterested(interested bool, msg func(pp.Message) bool) // are okay. type messageWriter func(pp.Message) bool +// Proxies the messageWriter's response. func (cn *connection) request(r request, mw messageWriter) bool { if cn.requests == nil { cn.requests = make(map[request]struct{}, cn.nominalMaxRequests()) @@ -359,6 +360,9 @@ func (cn *connection) request(r request, mw messageWriter) bool { panic("requesting piece peer doesn't have") } cn.requests[r] = struct{}{} + if _, ok := cn.t.conns[cn]; !ok { + panic("requesting but not in active conns") + } cn.t.pendingRequests[r]++ return mw(pp.Message{ Type: pp.Request, @@ -421,6 +425,9 @@ func (cn *connection) writer(keepAliveTimeout time.Duration) { defer cn.Close() defer keepAliveTimer.Stop() for { + if cn.closed.IsSet() { + return + } buf.Write(cn.postedBuffer.Bytes()) cn.postedBuffer.Reset() if buf.Len() == 0 { diff --git a/torrent.go b/torrent.go index 6a332e0e..5bf85913 100644 --- a/torrent.go +++ b/torrent.go @@ -1171,6 +1171,11 @@ func (t *Torrent) SetInfoBytes(b []byte) (err error) { // Returns true if connection is removed from torrent.Conns. func (t *Torrent) deleteConnection(c *connection) (ret bool) { + if !c.closed.IsSet() { + panic("connection is not closed") + // There are behaviours prevented by the closed state that will fail + // if the connection has been deleted. + } _, ret = t.conns[c] delete(t.conns, c) c.deleteAllRequests()