fix(chain): Copy returns wrong pointer, connection leaks in Connect/Handshake, deferred cancel accumulation in retry loops

- transport.go: Copy() returned original pointer instead of the allocated copy,
  causing data races and incorrect routing for multiplexed chains.
- route.go: Connect and Handshake failures in intermediate nodes leaked the
  returned connection. Handshake fix preserves the original Connect result
  (guaranteed non-nil) and closes both the Handshake return value and input.
- router.go: defer cancel() inside retry loops accumulated context timers
  across iterations. Wrapped each loop body in an IIFE so defer scopes
  per-iteration and timers are released immediately.
This commit is contained in:
ginuerzh
2026-05-23 16:30:53 +08:00
parent d015378654
commit 569572d6f7
3 changed files with 97 additions and 78 deletions
+10 -1
View File
@@ -271,20 +271,29 @@ func (r *chainRoute) connect(ctx context.Context, logger logger.Logger) (conn ne
}
cc, err = preNode.Options().Transport.Connect(ctx, cn, "tcp", addr)
if err != nil {
if cc != nil {
cc.Close()
}
cn.Close()
if marker != nil {
marker.Mark()
}
return
}
cc, err = node.Options().Transport.Handshake(ctx, cc)
var cc2 net.Conn
cc2, err = node.Options().Transport.Handshake(ctx, cc)
if err != nil {
if cc2 != nil {
cc2.Close()
}
cc.Close()
cn.Close()
if marker != nil {
marker.Mark()
}
return
}
cc = cc2
if marker != nil {
marker.Reset()
}