connection.go | 7 +++++++ torrent.go | 5 +++++ diff --git a/connection.go b/connection.go index 3165e46ffa30daf874bc51c1e15f6a711b6942cb..46083d56d3794b6aa2d55a641a69a72951d2f238 100644 --- a/connection.go +++ b/connection.go @@ -348,6 +348,7 @@ // The function takes a message to be sent, and returns true if more messages // 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 @@ if !cn.PeerHasPiece(r.Index.Int()) { 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 @@ defer cn.mu().Unlock() 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 6a332e0e2920fc5ae04c2e651748a393ce2030b1..5bf85913dc3b6bb4c30f2b6793a6325dd58b8faf 100644 --- a/torrent.go +++ b/torrent.go @@ -1171,6 +1171,11 @@ } // 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()