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

theflyingcodr/rabbitmq: consumer/host.go; 106 LoC

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

Description

Found a possible issue in [theflyingcodr/rabbitmq](https://www.github.com/theflyingcodr/rabbitmq) at [consumer/host.go](https://github.com/theflyingcodr/rabbitmq/blob/bdfe0f4424b6248514ab5b05ea8f39489d7b7b27/consumer/host.go#L118-L223)

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 k used in defer or goroutine at line 176

[Click here to see the code in its original context.](https://github.com/theflyingcodr/rabbitmq/blob/bdfe0f4424b6248514ab5b05ea8f39489d7b7b27/consumer/host.go#L118-L223)

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

```go
for k, r := range c.Queues(ctx){
go func(key string, routes *Routes) {
h.wg.Add(1)
defer h.wg.Done()

for {
// wait until we have a connection
if !h.connected {
time.Sleep(200 * time.Millisecond)
continue
}

// attempt to get a channel
queueChannel, err := h.connection.Channel()
if err != nil{
log.Error("error setting up consumer queue for %s", key)
time.Sleep(500 *time.Millisecond)
continue
}

h.mu.Lock()
h.channels[key] = queueChannel
h.mu.Unlock()

closeChannel := make(chan *amqp.Error)
cancelChannel := make(chan string)
queueChannel.NotifyClose(closeChannel)
queueChannel.NotifyCancel(cancelChannel)

// build the queue, if it's deleted it will be recreated
cfg.BuildQueue(key, routes, queueChannel, n)

// start consuming
go func() {
h.wg.Add(1)
defer h.wg.Done()
// start consuming messages
msgs, err := queueChannel.Consume(key, fmt.Sprintf("%s-%s", cfg.GetName(), uuid.NewUUID()), false, cfg.GetExclusive(), false, cfg.GetNoWait(), cfg.Args)
if err != nil {
log.Fatal(err)
}

// setup global, consumer & default middleware
middleware := h.buildChain(c.Middleware(errorHandler(routes.DeliveryFunc)), h.middleware)
for d := range msgs {
panicHandler(middleware).HandleMessage(context.Background(), d)
}
}()

select {
case queueErr := <-closeChannel:
if h.shutdown{
// indicates a graceful shutdown
// exit the routine
return
} else if queueErr != nil{
// there was an error, usually due to connection being closed
// log it and then we attempt to recreate the channel & queue
log.Errorf("queue channel closed for queue %s: %s", k, queueErr.Error())
}
case <-cancelChannel:
if h.shutdown {
return
}
log.Infof("channel for queue %s deleted, recreating", k)
}
h.mu.Lock()
delete(h.channels, key)
h.mu.Unlock()
}
}(k, r)

// setup the dead letter queue
if cfg.GetHasDeadletter() {
// check queue every second to check it hasn't been deleted,
// recreate it if we can
go func(key string, routes *Routes){
h.wg.Add(1)
defer h.wg.Done()
for {
// we're in the middle of shutdown, exit
if h.shutdown{
return
}
// wait for connection
for !h.connected{
time.Sleep(200 *time.Millisecond)
}

t := time.NewTimer(time.Second)
<-t.C

dlCh, err := h.connection.Channel()
if err != nil{
log.Error(err)
time.Sleep(200 *time.Millisecond)
break
}
if err := cfg.BuildDeadletterQueue(routes, dlCh, h.connection, n); err != nil{
log.Error(err)
}
t.Reset(time.Second)
}
}(k, r)
}
}

```

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: bdfe0f4424b6248514ab5b05ea8f39489d7b7b27

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.