From d38af90c3eaf36bf2aaae9c202c779869fe1afa5 Mon Sep 17 00:00:00 2001 From: lowzj Date: Wed, 18 Mar 2020 14:24:00 +0800 Subject: [PATCH] bugfix: check err returned by peerMgr.Get to prevent nil pointer reference Signed-off-by: lowzj --- supernode/daemon/mgr/task/manager_util.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/supernode/daemon/mgr/task/manager_util.go b/supernode/daemon/mgr/task/manager_util.go index b6c65cc3b..45a12947b 100644 --- a/supernode/daemon/mgr/task/manager_util.go +++ b/supernode/daemon/mgr/task/manager_util.go @@ -333,8 +333,6 @@ func (tm *Manager) parseAvailablePeers(ctx context.Context, clientID string, tas return true, finishInfo, nil } - // Get peerName to represent peer in metrics. - peer, _ := tm.peerMgr.Get(context.Background(), dfgetTask.PeerID) // get scheduler pieceResult logrus.Debugf("start scheduler for taskID: %s clientID: %s", task.ID, clientID) startTime := time.Now() @@ -342,7 +340,14 @@ func (tm *Manager) parseAvailablePeers(ctx context.Context, clientID string, tas if err != nil { return false, nil, err } - tm.metrics.scheduleDurationMilliSeconds.WithLabelValues(peer.IP.String()).Observe(timeutils.SinceInMilliseconds(startTime)) + timeCost := timeutils.SinceInMilliseconds(startTime) + // Get peerName to represent peer in metrics. + if peer, err := tm.peerMgr.Get(context.Background(), dfgetTask.PeerID); err == nil { + tm.metrics.scheduleDurationMilliSeconds.WithLabelValues(peer.IP.String()).Observe(timeCost) + } else { + logrus.Warnf("failed to get peer with peerId(%s) taskId(%s): %v", + dfgetTask.PeerID, task.ID, err) + } logrus.Debugf("get scheduler result length(%d) with taskID(%s) and clientID(%s)", len(pieceResult), task.ID, clientID) if len(pieceResult) == 0 {