fix connection close race

Need to prevent Close from being called at the same time. The lock
already existed, but was only used in one place where it should be
applied generally.
This commit is contained in:
John Eikenberry 2020-03-07 15:05:46 -08:00
parent 8f77623878
commit c31b3c3b22
1 changed files with 6 additions and 2 deletions

View File

@ -31,6 +31,12 @@ func (c *conn) sendPacket(m encoding.BinaryMarshaler) error {
return sendPacket(c, m) return sendPacket(c, m)
} }
func (c *conn) Close() error {
c.Lock()
defer c.Unlock()
return c.WriteCloser.Close()
}
type clientConn struct { type clientConn struct {
conn conn
wg sync.WaitGroup wg sync.WaitGroup
@ -67,9 +73,7 @@ func (c *clientConn) loop() {
// appropriate channel. // appropriate channel.
func (c *clientConn) recv() error { func (c *clientConn) recv() error {
defer func() { defer func() {
c.conn.Lock()
c.conn.Close() c.conn.Close()
c.conn.Unlock()
}() }()
for { for {
typ, data, err := c.recvPacket() typ, data, err := c.recvPacket()