peer+server: ensure the remote TCP connection is always closed
This commit is contained in:
2 files changed
+14
-12
No files matched your search
@@ -77,7 +77,6 @@ type peer struct {
|
|||||||
|
|
||||||
// MUST be used atomically.
|
// MUST be used atomically.
|
||||||
started int32
|
started int32
|
||||||
connected int32
|
|
||||||
disconnect int32
|
disconnect int32
|
||||||
|
|
||||||
connReq *connmgr.ConnReq
|
connReq *connmgr.ConnReq
|
||||||
@@ -270,8 +269,6 @@ func (p *peer) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
atomic.AddInt32(&p.connected, 1)
|
|
||||||
|
|
||||||
peerLog.Tracef("peer %v starting", p)
|
peerLog.Tracef("peer %v starting", p)
|
||||||
|
|
||||||
p.wg.Add(5)
|
p.wg.Add(5)
|
||||||
@@ -289,14 +286,12 @@ func (p *peer) Start() error {
|
|||||||
// all goroutines have exited.
|
// all goroutines have exited.
|
||||||
func (p *peer) Stop() error {
|
func (p *peer) Stop() error {
|
||||||
// If we're already disconnecting, just exit.
|
// If we're already disconnecting, just exit.
|
||||||
if atomic.AddInt32(&p.disconnect, 1) != 1 {
|
if !atomic.CompareAndSwapInt32(&p.disconnect, 0, 1) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, close the connection if we're currently connected.
|
// Ensure that the TCP connection is properly closed before continuing.
|
||||||
if atomic.LoadInt32(&p.connected) != 0 {
|
p.conn.Close()
|
||||||
p.conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Signal all worker goroutines to gracefully exit.
|
// Signal all worker goroutines to gracefully exit.
|
||||||
close(p.quit)
|
close(p.quit)
|
||||||
@@ -314,12 +309,14 @@ func (p *peer) Disconnect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
peerLog.Tracef("Disconnecting %s", p)
|
peerLog.Tracef("Disconnecting %s", p)
|
||||||
if atomic.LoadInt32(&p.connected) != 0 {
|
|
||||||
p.conn.Close()
|
// Ensure that the TCP connection is properly closed before continuing.
|
||||||
}
|
p.conn.Close()
|
||||||
|
|
||||||
close(p.quit)
|
close(p.quit)
|
||||||
|
|
||||||
|
// If this connection was established persistently, then notify the
|
||||||
|
// connection manager that the peer has been disconnected.
|
||||||
if p.connReq != nil {
|
if p.connReq != nil {
|
||||||
p.server.connMgr.Disconnect(p.connReq.ID())
|
p.server.connMgr.Disconnect(p.connReq.ID())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -518,9 +518,14 @@ func (s *server) removePeer(p *peer) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// As the peer is now finished, ensure that the TCP connection is
|
||||||
|
// closed and all of its related goroutines have exited.
|
||||||
|
if err := p.Stop(); err != nil {
|
||||||
|
peerLog.Errorf("unable to stop peer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Ignore deleting peers if we're shutting down.
|
// Ignore deleting peers if we're shutting down.
|
||||||
if atomic.LoadInt32(&s.shutdown) != 0 {
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
||||||
p.Stop()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user