Skip to content

Commit

Permalink
chore(dot/network): log the errors from stream.Close() (#2779)
Browse files Browse the repository at this point in the history
* log the errors from stream.Close()

Fixed #2376

* replace %w with %s in logger.Warnf()
  • Loading branch information
axaysagathiya authored Aug 24, 2022
1 parent 3fd63db commit 4ed3def
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
10 changes: 8 additions & 2 deletions dot/network/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func (s *testStreamHandler) readStream(stream libp2pnetwork.Stream,
return
} else if err != nil {
logger.Debugf("failed to read from stream using protocol %s: %s", stream.Protocol(), err)
_ = stream.Close()
err := stream.Close()
if err != nil {
logger.Warnf("failed to close stream: %s", err)
}
return
}

Expand All @@ -105,7 +108,10 @@ func (s *testStreamHandler) readStream(stream libp2pnetwork.Stream,
err = handler(stream, msg)
if err != nil {
logger.Errorf("failed to handle message %s from stream: %s", msg, err)
_ = stream.Close()
err := stream.Close()
if err != nil {
logger.Warnf("failed to close stream: %s", err)
}
return
}
}
Expand Down
5 changes: 4 additions & 1 deletion dot/network/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func (s *Service) decodeLightMessage(in []byte, peer peer.ID, _ bool) (Message,

func (s *Service) handleLightMsg(stream libp2pnetwork.Stream, msg Message) (err error) {
defer func() {
_ = stream.Close()
err := stream.Close()
if err != nil {
logger.Warnf("failed to close stream: %s", err)
}
}()

lr, ok := msg.(*LightRequest)
Expand Down
5 changes: 4 additions & 1 deletion dot/network/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ func closeOutboundStream(info *notificationsProtocol, peerID peer.ID, stream net
)

info.peersData.deleteOutboundHandshakeData(peerID)
_ = stream.Close()
err := stream.Close()
if err != nil {
logger.Warnf("failed to close outbound stream: %s", err)
}
}

func (s *Service) sendData(peer peer.ID, hs Handshake, info *notificationsProtocol, msg NotificationsMessage) {
Expand Down
3 changes: 2 additions & 1 deletion dot/network/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ func Test_HandshakeTimeout(t *testing.T) {
info.peersData.deleteOutboundHandshakeData(nodeB.host.id())
connAToB := nodeA.host.p2pHost.Network().ConnsToPeer(nodeB.host.id())
for _, stream := range connAToB[0].GetStreams() {
_ = stream.Close()
err := stream.Close()
require.NoError(t, err)
}

testHandshakeMsg := &BlockAnnounceHandshake{
Expand Down
5 changes: 4 additions & 1 deletion dot/network/stream_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func (sm *streamManager) cleanupStreams() {
stream := data.stream

if time.Since(lastReceived) > cleanupStreamInterval {
_ = stream.Close()
err := stream.Close()
if err != nil {
logger.Warnf("failed to close inactive stream: %s", err)
}
delete(sm.streamData, id)
}
}
Expand Down
10 changes: 8 additions & 2 deletions dot/network/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func (s *Service) DoBlockRequest(to peer.ID, req *BlockRequestMessage) (*BlockRe
}

defer func() {
_ = stream.Close()
err := stream.Close()
if err != nil {
logger.Warnf("failed to close stream: %s", err)
}
}()

if err = s.host.writeToStream(stream, req); err != nil {
Expand Down Expand Up @@ -102,7 +105,10 @@ func (s *Service) handleSyncMessage(stream libp2pnetwork.Stream, msg Message) er
}

defer func() {
_ = stream.Close()
err := stream.Close()
if err != nil {
logger.Warnf("failed to close stream: %s", err)
}
}()

if req, ok := msg.(*BlockRequestMessage); ok {
Expand Down

0 comments on commit 4ed3def

Please sign in to comment.