]> Sergey Matveev's repositories - btrtrc.git/commitdiff
connection.writer wasn't checking closed state
authorMatt Joiner <anacrolix@gmail.com>
Fri, 2 Feb 2018 02:36:18 +0000 (13:36 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 2 Feb 2018 02:36:18 +0000 (13:36 +1100)
connection.go
torrent.go

index 3165e46ffa30daf874bc51c1e15f6a711b6942cb..46083d56d3794b6aa2d55a641a69a72951d2f238 100644 (file)
@@ -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 {
index 6a332e0e2920fc5ae04c2e651748a393ce2030b1..5bf85913dc3b6bb4c30f2b6793a6325dd58b8faf 100644 (file)
@@ -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()