From d0c778ed9dd4b606134f24cefaabcac9c03e3766 Mon Sep 17 00:00:00 2001 From: kuba-4chain Date: Tue, 2 Jul 2024 16:18:54 +0200 Subject: [PATCH] fix: infinite loop when checking for write connection in peer --- peer.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/peer.go b/peer.go index d3a2758..7dbcf85 100644 --- a/peer.go +++ b/peer.go @@ -683,17 +683,15 @@ func (p *Peer) startWriteChannelHandler(ctx context.Context, instance int) { p.logger.Debug("Write handler canceled", slog.Int("instance", instance)) return case msg := <-p.writeChan: + p.mu.RLock() + writeConn := p.writeConn + p.mu.RUnlock() - for { - p.mu.RLock() - writeConn := p.writeConn - p.mu.RUnlock() - - if writeConn != nil { - break - } + if writeConn == nil { time.Sleep(100 * time.Millisecond) + continue } + err := p.writeRetry(ctx, msg) if err != nil { if errors.Is(err, context.Canceled) { @@ -732,6 +730,7 @@ func (p *Peer) startWriteChannelHandler(ctx context.Context, instance int) { case *wire.MsgGetData: p.logger.Debug(sentMsg, slog.String(commandKey, strings.ToUpper(message.Command())), slog.String(hashKey, m.InvList[0].Hash.String()), slog.String(typeKey, "getdata")) case *wire.MsgInv: + p.logger.Debug(sentMsg, slog.String(commandKey, strings.ToUpper(message.Command())), slog.String(hashKey, m.InvList[0].Hash.String()), slog.String(typeKey, "inv")) default: p.logger.Debug(sentMsg, slog.String(commandKey, strings.ToUpper(message.Command())), slog.String(typeKey, "unknown")) }