github-vet / github-vet/rangeloop-pointer-findings

jindezgm/gmap: transport.go; 47 LoC

Open
#7,604 0 comments 0 reactions 0 assignees View on GitHub
fresh small
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [jindezgm/gmap](https://www.github.com/jindezgm/gmap) at [transport.go](https://github.com/jindezgm/gmap/blob/3b12e5e99415f100d1ff619377fd951dbbf60ddc/transport.go#L156-L202)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable msg used in defer or goroutine at line 183

[Click here to see the code in its original context.](https://github.com/jindezgm/gmap/blob/3b12e5e99415f100d1ff619377fd951dbbf60ddc/transport.go#L156-L202)

Click here to show the 47 line(s) of Go which triggered the analyzer.

```go
for msg := range p.msgc {
// Connect node.
if nil == serviceClient {
conn, err := grpc.Dial(p.endpoint, grpc.WithInsecure(), grpc.WithBackoffMaxDelay(2*time.Second))
if nil != err {
logger.Errorf("connect node(%s) failed:%v", p.endpoint, err)
reportUnreachable(p.gmap.raft, &msg)
continue
}
serviceClient = NewTransportServiceClient(conn)
}

var try int
retry:
// Create send stream client.
if nil == sendClient {
if sendClient, err = serviceClient.SendMessage(context.Background()); nil != err {
logger.Errorf("create send client(%s) failed:%v", p.endpoint, err)
reportUnreachable(p.gmap.raft, &msg)
continue
}
}
// Snapshots should be sent by creating a coroutine, because the snapshot may be very large,
// and the sending time will be very long, even affect heartbeat sending.
if msg.Type == raftpb.MsgSnap {
go func() {
// Compress the sent snapshot with gzip.
ctx, snap, opt := context.Background(), &Bytes{Data: pbutil.MustMarshal(&msg)}, grpc.UseCompressor(gzip.Name)
if _, err := serviceClient.SendSnapshot(ctx, snap, opt); nil != err {
logger.Errorf("send(%s) snapshot failed:%v", p.endpoint, err)
p.gmap.raft.ReportSnapshot(msg.To, raft.SnapshotFailure)
}
// Report send snapshot finished to raft and reduce inflight snapshots count.
p.gmap.raft.ReportSnapshot(p.id, raft.SnapshotFinish)
atomic.AddInt64(&p.gmap.inflightSnapshots, -1)
}()
} else if err := sendClient.Send(&Bytes{Data: pbutil.MustMarshal(&msg)}); nil != err {
logger.Errorf("send(%s) message failed:%v", p.endpoint, err)
// Maybe the network connection exception, reconnect and try again.
if try++; try >= 3 {
reportUnreachable(p.gmap.raft, &msg)
} else {
sendClient = nil
goto retry
}
}
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: 3b12e5e99415f100d1ff619377fd951dbbf60ddc

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.