]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Ensure PeerConn._close is called for incoming connections
authorMatt Joiner <anacrolix@gmail.com>
Wed, 14 Oct 2020 04:11:45 +0000 (15:11 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 14 Oct 2020 04:11:45 +0000 (15:11 +1100)
This fixes missing calls to PeerConnClosed callback.

client.go
peerconn.go

index 98563227c7c86758fee2ced0a67af0e74704c09c..25f7a860dd8aaf957f6f658edcf153a61ce7ffe3 100644 (file)
--- a/client.go
+++ b/client.go
@@ -509,6 +509,7 @@ func (cl *Client) incomingConnection(nc net.Conn) {
        }
        c := cl.newConnection(nc, false, nc.RemoteAddr(), nc.RemoteAddr().Network(),
                regularNetConnPeerConnConnString(nc))
+       defer c.close()
        c.Discovery = PeerSourceIncoming
        cl.runReceivedConn(c)
 }
@@ -1348,6 +1349,7 @@ func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr,
                connString:  connString,
                conn:        nc,
                writeBuffer: new(bytes.Buffer),
+               callbacks:   &cl.config.Callbacks,
        }
        c.peerImpl = c
        c.logger = cl.logger.WithDefaultLevel(log.Warning).WithContextValue(c)
index 8ff841029ecdfa59f06456f076148fbaf3363962..c9a050a5cd7957b8c46624951ddf59bbf9912a11 100644 (file)
@@ -136,6 +136,8 @@ type PeerConn struct {
        writerCond  sync.Cond
 
        pex pexConnState
+
+       callbacks *Callbacks
 }
 
 func (cn *PeerConn) connStatusString() string {
@@ -356,7 +358,7 @@ func (cn *PeerConn) _close() {
        if cn.conn != nil {
                cn.conn.Close()
        }
-       if cb := cn.t.cl.config.Callbacks.PeerConnClosed; cb != nil {
+       if cb := cn.callbacks.PeerConnClosed; cb != nil {
                cb(cn)
        }
 }
@@ -1072,7 +1074,7 @@ func (c *PeerConn) mainReadLoop() (err error) {
                        defer cl.lock()
                        err = decoder.Decode(&msg)
                }()
-               if cb := cl.config.Callbacks.ReadMessage; cb != nil && err == nil {
+               if cb := c.callbacks.ReadMessage; cb != nil && err == nil {
                        cb(c, &msg)
                }
                if t.closed.IsSet() || c.closed.IsSet() {
@@ -1209,7 +1211,7 @@ func (c *PeerConn) onReadExtendedMsg(id pp.ExtensionNumber, payload []byte) (err
                        c.logger.Printf("error parsing extended handshake message %q: %s", payload, err)
                        return errors.Wrap(err, "unmarshalling extended handshake payload")
                }
-               if cb := cl.config.Callbacks.ReadExtendedHandshake; cb != nil {
+               if cb := c.callbacks.ReadExtendedHandshake; cb != nil {
                        cb(c, &d)
                }
                //c.logger.WithDefaultLevel(log.Debug).Printf("received extended handshake message:\n%s", spew.Sdump(d))