peer: avoid exit deadlock by ensuring WaitGroups are decremented before disconnect

This commit is contained in:
Olaoluwa Osuntokun 2017-09-27 20:22:40 -07:00
parent ab25b636f5
commit c5876e5d21
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21

24
peer.go

@ -584,7 +584,6 @@ func (c *chanMsgStream) AddMsg(msg lnwire.Message) {
// //
// NOTE: This method MUST be run as a goroutine. // NOTE: This method MUST be run as a goroutine.
func (p *peer) readHandler() { func (p *peer) readHandler() {
defer p.wg.Done()
chanMsgStreams := make(map[lnwire.ChannelID]*chanMsgStream) chanMsgStreams := make(map[lnwire.ChannelID]*chanMsgStream)
out: out:
@ -708,6 +707,8 @@ out:
} }
} }
p.wg.Done()
p.Disconnect(errors.New("read handler closed")) p.Disconnect(errors.New("read handler closed"))
for cid, chanStream := range chanMsgStreams { for cid, chanStream := range chanMsgStreams {
@ -793,11 +794,8 @@ func (p *peer) writeMessage(msg lnwire.Message) error {
// //
// NOTE: This method MUST be run as a goroutine. // NOTE: This method MUST be run as a goroutine.
func (p *peer) writeHandler() { func (p *peer) writeHandler() {
defer func() { var exitErr error
p.wg.Done() out:
peerLog.Tracef("writeHandler for peer %v done", p)
}()
for { for {
select { select {
case outMsg := <-p.sendQueue: case outMsg := <-p.sendQueue:
@ -823,15 +821,21 @@ func (p *peer) writeHandler() {
} }
if err != nil { if err != nil {
p.Disconnect(errors.Errorf("unable to write message: %v", exitErr = errors.Errorf("unable to write message: %v", err)
err)) break out
return
} }
case <-p.quit: case <-p.quit:
return exitErr = errors.Errorf("peer exiting")
break out
} }
} }
p.wg.Done()
p.Disconnect(exitErr)
peerLog.Tracef("writeHandler for peer %v done", p)
} }
// queueHandler is responsible for accepting messages from outside subsystems // queueHandler is responsible for accepting messages from outside subsystems