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

freenowtech/mqtt-loadtest: mqtt/subscriber.go; 25 LoC

Open
#16,488 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 [freenowtech/mqtt-loadtest](https://www.github.com/freenowtech/mqtt-loadtest) at [mqtt/subscriber.go](https://github.com/freenowtech/mqtt-loadtest/blob/2a69169753d083efebf7de8a69ad6baf033691db/mqtt/subscriber.go#L97-L121)

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 sub used in defer or goroutine at line 100

[Click here to see the code in its original context.](https://github.com/freenowtech/mqtt-loadtest/blob/2a69169753d083efebf7de8a69ad6baf033691db/mqtt/subscriber.go#L97-L121)

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

```go
for idx, sub := range subs.clients {
log.Debugf("Churning subscriber %v", idx)
go func() {
sub.Unsubscribe(subs.TopicPrefix + strconv.Itoa(idx))
sub.Disconnect(0)
oldOpts := sub.OptionsReader()
opts := mqtt.NewClientOptions().SetUsername(oldOpts.Username()).SetPassword(oldOpts.Password()).SetClientID(oldOpts.ClientID()).SetProtocolVersion(oldOpts.ProtocolVersion()).AddBroker(oldOpts.Servers()[0].String()).SetCleanSession(oldOpts.CleanSession())
opts.SetDefaultPublishHandler(createPublishHandler(subs.TopicPrefix + strconv.Itoa(idx)))
newSub := mqtt.NewClient(opts)
subs.clients[idx] = newSub

time.Sleep(connectDelay)
if token := newSub.Connect(); token.Wait() && token.Error() != nil {
log.Error(token.Error())
churningErrors.Inc()
return
}
if token := newSub.Subscribe(subs.TopicPrefix+strconv.Itoa(idx), 0, nil); token.Wait() && token.Error() != nil {
log.Error(token.Error())
churningErrors.Inc()
}
}()
churnedSubscribers.Inc()
time.Sleep(churnRate)
}

```

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: 2a69169753d083efebf7de8a69ad6baf033691db

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.