github-vet / github-vet/rangeloop-pointer-findings
aykevl/dtsync: dtsync/msgpack.go; 67 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [aykevl/dtsync](https://www.github.com/aykevl/dtsync) at [dtsync/msgpack.go](https://github.com/aykevl/dtsync/blob/be7858c7dc735c67f5511c59b773d682cab1c194/dtsync/msgpack.go#L200-L266)
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 item used in defer or goroutine at line 235
[Click here to see the code in its original context.](https://github.com/aykevl/dtsync/blob/be7858c7dc735c67f5511c59b773d682cab1c194/dtsync/msgpack.go#L200-L266)
Click here to show the 67 line(s) of Go which triggered the analyzer.
```go
for item := range jobs {
state.lock.Lock()
progress := float64(state.costDone) / float64(state.costTotal)
state.lock.Unlock()
mp.sendValue("apply-progress", -1, applyProgressValue{
TotalProgress: progress,
Job: item.index,
State: "starting",
JobProgress: 0.0,
})
jobCostTotal := item.job.Cost()
progressChan := make(chan int64, 2)
progressExit := make(chan struct{})
go func() {
// Progress indication is inexact. It may not give the exact
// number of bytes per file, or might even count a bit more
// in rare circumstances.
var jobCostDone int64
for cost := range progressChan {
prevJobCostDone := jobCostDone
jobCostDone += cost
if jobCostDone >= jobCostTotal {
// Be forgiving in the progress indication.
jobCostDone = jobCostTotal - 1
}
state.lock.Lock()
state.costDone += (jobCostDone - prevJobCostDone) // add current job progress to global progress
progress := float64(state.costDone) / float64(state.costTotal)
state.lock.Unlock()
mp.sendValue("apply-progress", -1, applyProgressValue{
TotalProgress: progress,
Job: item.index,
State: "progress",
JobProgress: float64(jobCostDone) / float64(jobCostTotal),
})
}
state.lock.Lock()
state.costDone -= jobCostDone // subtract estimated/inexact progress
state.costDone += jobCostTotal // add real progress
state.lock.Unlock()
close(progressExit)
}()
err := item.job.Apply(progressChan)
<-progressExit // wait until the goroutine exited
state.lock.Lock()
progress = float64(state.costDone) / float64(state.costTotal)
state.lock.Unlock()
progressValue := applyProgressValue{
TotalProgress: progress,
Job: item.index,
State: "finished",
JobProgress: 1.0,
}
if err != nil {
progressValue.State = "error"
progressValue.Error = err.Error()
}
mp.sendValue("apply-progress", -1, progressValue)
}
```
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: be7858c7dc735c67f5511c59b773d682cab1c194
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.