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

Return on EOF error while reading stream. #1733

Merged
merged 4 commits into from
Aug 18, 2021
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
48 changes: 48 additions & 0 deletions dot/network/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,51 @@ func Test_RemoveReservedPeers(t *testing.T) {
err = nodeA.host.removeReservedPeers("failing peer ID")
require.Error(t, err)
}

func TestStreamCloseEOF(t *testing.T) {
basePathA := utils.NewTestBasePath(t, "nodeA")
configA := &Config{
BasePath: basePathA,
Port: 7001,
NoBootstrap: true,
NoMDNS: true,
}

nodeA := createTestService(t, configA)
nodeA.noGossip = true

basePathB := utils.NewTestBasePath(t, "nodeB")

configB := &Config{
BasePath: basePathB,
Port: 7002,
NoBootstrap: true,
NoMDNS: true,
}

nodeB := createTestService(t, configB)
nodeB.noGossip = true
handler := newTestStreamHandler(testBlockRequestMessageDecoder)
nodeB.host.registerStreamHandler("", handler.handleStream)
require.False(t, handler.exit)

addrInfoB := nodeB.host.addrInfo()
err := nodeA.host.connect(addrInfoB)
// retry connect if "failed to dial" error
if failedToDial(err) {
time.Sleep(TestBackoffTimeout)
err = nodeA.host.connect(addrInfoB)
}
require.NoError(t, err)

stream, err := nodeA.host.send(addrInfoB.ID, nodeB.host.protocolID, testBlockRequestMessage)
require.NoError(t, err)
require.False(t, handler.exit)

err = stream.Close()
require.NoError(t, err)

time.Sleep(TestBackoffTimeout)

require.True(t, handler.exit)
}
4 changes: 2 additions & 2 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ func (s *Service) readStream(stream libp2pnetwork.Stream, decoder messageDecoder

for {
tot, err := readStream(stream, msgBytes[:])
if err == io.EOF {
continue
if errors.Is(err, io.EOF) {
return
} else if err != nil {
logger.Trace("failed to read from stream", "peer", stream.Conn().RemotePeer(), "protocol", stream.Protocol(), "error", err)
_ = stream.Close()
Expand Down
10 changes: 8 additions & 2 deletions dot/network/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package network

import (
"errors"
"io"
"math/big"

Expand Down Expand Up @@ -89,6 +90,7 @@ func testBlockResponseMessage() *BlockResponseMessage {
type testStreamHandler struct {
messages map[peer.ID][]Message
decoder messageDecoder
exit bool
}

func newTestStreamHandler(decoder messageDecoder) *testStreamHandler {
Expand Down Expand Up @@ -135,10 +137,14 @@ func (s *testStreamHandler) readStream(stream libp2pnetwork.Stream, peer peer.ID
msgBytes = make([]byte, maxMessageSize)
)

defer func() {
s.exit = true
}()

for {
tot, err := readStream(stream, msgBytes)
if err == io.EOF {
continue
if errors.Is(err, io.EOF) {
return
} else if err != nil {
logger.Debug("failed to read from stream", "protocol", stream.Protocol(), "error", err)
_ = stream.Close()
Expand Down