diff --git a/peer.go b/peer.go index 549e94a3..70b40922 100644 --- a/peer.go +++ b/peer.go @@ -1005,7 +1005,12 @@ func (p *peer) readHandler() { out: for atomic.LoadInt32(&p.disconnect) == 0 { nextMsg, err := p.readNextMessage() - idleTimer.Stop() + if !idleTimer.Stop() { + select { + case <-idleTimer.C: + default: + } + } if err != nil { peerLog.Infof("unable to read message from %v: %v", p, err) @@ -1427,6 +1432,14 @@ func (p *peer) writeMessage(msg lnwire.Message) error { // // NOTE: This method MUST be run as a goroutine. func (p *peer) writeHandler() { + // We'll stop the timer after a new messages is sent, and also reset it + // after we process the next message. + idleTimer := time.AfterFunc(idleTimeout, func() { + err := fmt.Errorf("Peer %s no write for %s -- disconnecting", + p, idleTimeout) + p.Disconnect(err) + }) + var exitErr error const ( @@ -1501,6 +1514,16 @@ out: goto retryWithDelay } + // The write succeeded, reset the idle timer to prevent + // us from disconnecting the peer. + if !idleTimer.Stop() { + select { + case <-idleTimer.C: + default: + } + } + idleTimer.Reset(idleTimeout) + // If the peer requested a synchronous write, respond // with the error. if outMsg.errChan != nil {