github-vet / github-vet/rangeloop-pointer-findings
madcowfred/GoPostStuff: spawner.go; 154 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [madcowfred/GoPostStuff](https://www.github.com/madcowfred/GoPostStuff) at [spawner.go](https://github.com/madcowfred/GoPostStuff/blob/1fc63640f5f34a641a926f4d760190517756069d/spawner.go#L53-L206)
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 name used in defer or goroutine at line 67
[Click here to see the code in its original context.](https://github.com/madcowfred/GoPostStuff/blob/1fc63640f5f34a641a926f4d760190517756069d/spawner.go#L53-L206)
Click here to show the 154 line(s) of Go which triggered the analyzer.
```go
for name, server := range Config.Server {
log.Info("[%s] Starting %d connections", name, server.Connections)
// Make a channel to stuff Articles into
achan := make(chan *Article, server.Connections)
// Make a channel to stuff Totals into
tchan := make(chan *Totals, server.Connections)
// Start a goroutine to generate articles
wg.Add(1)
go func(c chan *Article, files []FileData) {
defer wg.Done()
log.Debug("[%s] Article generator started", name)
mc := NewMmapCache()
for filenum, fd := range files {
// Open and mmap the file
md, err := mc.MapFile(fd.path, len(Config.Server))
if err != nil {
log.Fatalf("MapFile error: %s", err)
}
// Work out how many parts we need
parts := fd.size / Config.Global.ArticleSize
rem := fd.size % Config.Global.ArticleSize
if rem > 0 {
parts++
}
// Build some articles
for partnum := int64(0); partnum < parts; partnum++ {
start := partnum * Config.Global.ArticleSize
end := min((partnum+1)*Config.Global.ArticleSize, fd.size)
ad := &ArticleData{
PartNum: partnum + 1,
PartTotal: parts,
PartSize: end - start,
PartBegin: start,
PartEnd: end,
FileNum: filenum + 1,
FileTotal: len(files),
FileSize: fd.size,
FileName: filepath.Base(fd.path),
}
var subject string
if *dirSubjectFlag {
subject = filepath.Base(filepath.Dir(fd.path))
} else {
subject = *subjectFlag
}
a := NewArticle(md.data[start:end], ad, subject)
c <- a
//log.Debug("%s %d = %d -> %d", fd.path, i, start, end)
}
if md.Decrement() {
err = mc.CloseFile(fd.path)
if err != nil {
log.Fatalf("CloseFile error: %s", err)
}
log.Debug("[%s] Closed file %s", name, fd.path)
}
}
close(c)
}(achan, files)
// Start a goroutine for each individual connection
for i := 0; i < server.Connections; i++ {
connID := i + 1
// Increment the WaitGroup counters
wg.Add(1)
go func(achan chan *Article, tchan chan *Totals) {
// Decrement the counter when the goroutine completes
defer wg.Done()
// Connect
log.Debug("[%s:%02d] Connecting...", name, connID)
conn, err := simplenntp.Dial(server.Address, server.Port, server.TLS, server.InsecureSSL, tdchan)
if err != nil {
log.Fatalf("[%s] Error while connecting: %s", name, err)
}
log.Debug("[%s:%02d] Connected", name, connID)
// Authenticate if required
if len(server.Username) > 0 {
log.Debug("[%s:%02d] Authenticating...", name, connID)
err := conn.Authenticate(server.Username, server.Password)
if err != nil {
log.Fatalf("[%s:%02d] Error while authenticating: %s", name, connID, err)
}
log.Debug("[%s:%02d] Authenticated", name, connID)
}
t := &Totals{start: time.Now()}
// Begin consuming
for article := range achan {
err := conn.Post(article.Body, Config.Global.ChunkSize)
if err != nil {
log.Warning("[%s:%02d] Post error: %s", name, connID, err)
}
t.bytes += int64(len(article.Body))
}
// Stick our totals struct into the channel
t.end = time.Now()
tchan <- t
// Close the connection
log.Debug("[%s:%02d] Closing connection", name, connID)
err = conn.Quit()
if err != nil {
log.Warning("[%s:%02d] Error while closing connection: %s", name, connID, err)
}
}(achan, tchan)
}
// Start a goroutine to print some stats when done, sigh
wg.Add(1)
go func(tchan chan *Totals) {
defer wg.Done()
minStart := time.Now()
var maxEnd time.Time
var totalBytes int64
for i := 0; i < server.Connections; i++ {
t := <-tchan
if t.start.Before(minStart) {
minStart = t.start
}
if t.end.After(maxEnd) {
maxEnd = t.end
}
totalBytes += t.bytes
}
// Calculate and log the result
dur := maxEnd.Sub(minStart)
speedKB := float64(totalBytes) / dur.Seconds() / 1024
totalMB := float64(totalBytes) / 1024 / 1024
log.Info("[%s] Posted %.1fMiB in %s at %.1fKB/s", name, totalMB, dur.String(), speedKB)
}(tchan)
}
```
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: 1fc63640f5f34a641a926f4d760190517756069d
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with spawner.go lines 53-206, especially the goroutines using name, server, and connID from the surrounding loops. Read the analyzer finding and inspect whether those goroutines can observe unintended loop values. Classify the instance with the requested reaction as Bug, Mitigated, or Desirable Behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100