Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit 1e457b6

Browse files
nonsensefjl
authored andcommitted
p2p: don't send DiscReason when using net.Pipe (#16004)
1 parent bb5349b commit 1e457b6

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

p2p/rlpx.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,14 @@ func (t *rlpx) close(err error) {
108108
// Tell the remote end why we're disconnecting if possible.
109109
if t.rw != nil {
110110
if r, ok := err.(DiscReason); ok && r != DiscNetworkError {
111-
t.fd.SetWriteDeadline(time.Now().Add(discWriteTimeout))
112-
SendItems(t.rw, discMsg, r)
111+
// rlpx tries to send DiscReason to disconnected peer
112+
// if the connection is net.Pipe (in-memory simulation)
113+
// it hangs forever, since net.Pipe does not implement
114+
// a write deadline. Because of this only try to send
115+
// the disconnect reason message if there is no error.
116+
if err := t.fd.SetWriteDeadline(time.Now().Add(discWriteTimeout)); err == nil {
117+
SendItems(t.rw, discMsg, r)
118+
}
113119
}
114120
}
115121
t.fd.Close()

p2p/rlpx_test.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,18 @@ func TestProtocolHandshake(t *testing.T) {
156156
node1 = &discover.Node{ID: discover.PubkeyID(&prv1.PublicKey), IP: net.IP{5, 6, 7, 8}, TCP: 44}
157157
hs1 = &protoHandshake{Version: 3, ID: node1.ID, Caps: []Cap{{"c", 1}, {"d", 3}}}
158158

159-
fd0, fd1 = net.Pipe()
160-
wg sync.WaitGroup
159+
wg sync.WaitGroup
161160
)
162161

162+
fd0, fd1, err := tcpPipe()
163+
if err != nil {
164+
t.Fatal(err)
165+
}
166+
163167
wg.Add(2)
164168
go func() {
165169
defer wg.Done()
166-
defer fd1.Close()
170+
defer fd0.Close()
167171
rlpx := newRLPX(fd0)
168172
remid, err := rlpx.doEncHandshake(prv0, node1)
169173
if err != nil {
@@ -597,3 +601,31 @@ func TestHandshakeForwardCompatibility(t *testing.T) {
597601
t.Errorf("ingress-mac('foo') mismatch:\ngot %x\nwant %x", fooIngressHash, wantFooIngressHash)
598602
}
599603
}
604+
605+
// tcpPipe creates an in process full duplex pipe based on a localhost TCP socket
606+
func tcpPipe() (net.Conn, net.Conn, error) {
607+
l, err := net.Listen("tcp", "127.0.0.1:0")
608+
if err != nil {
609+
return nil, nil, err
610+
}
611+
defer l.Close()
612+
613+
var aconn net.Conn
614+
aerr := make(chan error, 1)
615+
go func() {
616+
var err error
617+
aconn, err = l.Accept()
618+
aerr <- err
619+
}()
620+
621+
dconn, err := net.Dial("tcp", l.Addr().String())
622+
if err != nil {
623+
<-aerr
624+
return nil, nil, err
625+
}
626+
if err := <-aerr; err != nil {
627+
dconn.Close()
628+
return nil, nil, err
629+
}
630+
return aconn, dconn, nil
631+
}

0 commit comments

Comments
 (0)