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

zwdong1994/cockroach-old: pkg/cmd/roachtest/cancel.go; 40 LoC

Open
#15,636 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 [zwdong1994/cockroach-old](https://www.github.com/zwdong1994/cockroach-old) at [pkg/cmd/roachtest/cancel.go](https://github.com/zwdong1994/cockroach-old/blob/b0652329251811da42582bd405a0ce8f329c962c/pkg/cmd/roachtest/cancel.go#L62-L101)

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 q used in defer or goroutine at line 65

[Click here to see the code in its original context.](https://github.com/zwdong1994/cockroach-old/blob/b0652329251811da42582bd405a0ce8f329c962c/pkg/cmd/roachtest/cancel.go#L62-L101)

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

```go
for _, q := range queries {
sem := make(chan struct{}, 1)
go func() {
fmt.Printf("executing \"%s\"\n", q)
sem <- struct{}{}
_, err := conn.Exec(queryPrefix + q)
if err == nil {
close(sem)
t.Fatal("query completed before it could be canceled")
} else {
fmt.Printf("query failed with error: %s\n", err)
}
sem <- struct{}{}
}()

<-sem

// The cancel query races with the execution of the query it's trying to
// cancel, which may result in attempting to cancel the query before it
// has started. To be more confident that the query is executing, wait
// a bit before attempting to cancel it.
time.Sleep(100 * time.Millisecond)

const cancelQuery = `CANCEL QUERY (
SELECT query_id FROM [SHOW CLUSTER QUERIES] WHERE query not like '%SHOW CLUSTER QUERIES%')`
c.Run(ctx, c.Node(1), `./cockroach sql --insecure -e "`+cancelQuery+`"`)
cancelStartTime := timeutil.Now()

select {
case _, ok := <-sem:
if !ok {
t.Fatal("query could not be canceled")
}
timeToCancel := timeutil.Now().Sub(cancelStartTime)
fmt.Printf("canceling \"%s\" took %s\n", q, timeToCancel)

case <-time.After(5 * time.Second):
t.Fatal("query took too long to respond to cancellation")
}
}

```

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

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.