Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:handshake stuck by long connection #890

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions p2p/peers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,24 @@ func (p *ConnMsgRW) Send(msgcode uint64, data interface{}, respondID uint64) (in
msg.Reply = make(chan interface{})
}
log.Debug("Send message", "msg", msg.String())
ctx, can := context.WithTimeout(context.Background(), HandleTimeout)
defer can()

select {
case p.w <- msg:
case <-ctx.Done():
go p.Close()
return nil, fmt.Errorf("ConnMsgRW send message timeout:%s", msg.String())
case <-p.closing:
return nil, ErrConnClosed
}
if msg.Reply != nil {
select {
case ret := <-msg.Reply:
return ret, nil
case <-ctx.Done():
go p.Close()
return nil, fmt.Errorf("ConnMsgRW wait respond message timeout:%s", msg.String())
case <-p.closing:
return nil, ErrConnClosed
}
Expand Down
15 changes: 9 additions & 6 deletions p2p/synch/longconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ func (s *Sync) establishLongConnection(pe *peers.Peer) error {
if err != nil {
return common.NewErrorStr(common.ErrStreamBase, fmt.Sprintf("open stream on topic %v failed", topic)).ToError()
}
defer func() {
err := stream.Close()
common.EgressConnectMeter.Mark(1)

go func() {
err = pe.Run(stream, s.p2p.Encoding())
if err != nil {
log.Warn(err.Error())
}
err = stream.Close()
if err != nil {
log.Warn(err.Error())
}
}()

common.EgressConnectMeter.Mark(1)

return pe.Run(stream, s.p2p.Encoding())
return nil
}

func (s *Sync) registerLongConnection() {
Expand Down
Loading