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

zhaoxuyang13/Dist-KV: go/slave/slave_server.go; 83 LoC

Open
#17,913 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [zhaoxuyang13/Dist-KV](https://www.github.com/zhaoxuyang13/Dist-KV) at [go/slave/slave_server.go](https://github.com/zhaoxuyang13/Dist-KV/blob/61ed6d9f7ea850e21439e83d6ca98d5320758af6/go/slave/slave_server.go#L340-L422)

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 conf used in defer or goroutine at line 391

[Click here to see the code in its original context.](https://github.com/zhaoxuyang13/Dist-KV/blob/61ed6d9f7ea850e21439e83d6ca98d5320758af6/go/slave/slave_server.go#L340-L422)

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

```go
for conf := range confChan{
log.Printf("processing conf Version %d\n",conf.Version)
if s.localVersion == conf.Version {
continue // if up2date, not check conf
}
log.Printf("new configuration found %+v\n", conf)

/* server should init the shard if it is the first configuration. */
if s.localVersion == 0 && conf.Version == 1 {
for _, shard := range conf.Id2Groups[s.conf.GroupID].Shards {
s.slave.shards = append(s.slave.shards, shard)
s.slave.assureStorage(shard, READY)
}
s.localVersion = 1
continue
}

/* server should compare the two configurations, and react to changes */

/* deep copy last configuration */
var shards []int
if group, exist := conf.Id2Groups[s.conf.GroupID]; exist {
shards = make([]int, len(group.Shards))
copy(shards, group.Shards)
} else {
// this group not belongs to confs, not copy configuration,
shards = make([]int, 0)
}
version := conf.Version
log.Printf("old shards, %v, new conf %v, new shards %v\n", s.slave.shards, conf.Id2Groups[s.conf.GroupID], shards)

added, removed := s.slave.compareShards(shards)
if len(added) == 0 && len(removed) == 0 {
/* no difference, already up2date*/
s.localVersion = version
continue
}

/* for shards that has been removed, send it to its new manager */
if s.primary {
/* send all shardID to it's current manager, do it concurrently */
counter := sync.WaitGroup{}
counter.Add(len(removed))
log.Printf("about to send %v out\n", removed)
for _, shardID := range removed {
shardID := shardID
go func() {
s.slave.deleteShard(shardID)
counter.Done()

gid := conf.Assignment[shardID]
if err := s.sendShard(shardID, gid); err != nil {
log.Println(err.Error())
}
s.slave.deleteStorage(shardID)
/* inform backups that this shard has been successfully send. */
s.backupConfLock.RLock()
defer s.backupConfLock.RUnlock()
for _,backup := range s.backupServers {
backup.Sync(request{
ShardID: shardID,
ReqCode: InvalidateReq,
})
}
}()
}
counter.Wait()
}else {
for _, shardID := range removed {
s.slave.deleteShard(shardID)
s.slave.expireStorage(shardID)
}
}
/* for shards that's new, allocate entry if not allocated. */
log.Printf("about to add %v \n", added)
for _, shardID := range added {
s.slave.assureStorage(shardID, UNREADY)
s.slave.addShard(shardID)
}
log.Printf("shard after send and added: %+v\n", s.slave.shards)
/* TODO: remove those shards not used */
}

```

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: 61ed6d9f7ea850e21439e83d6ca98d5320758af6

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in go/slave/slave_server.go around lines 340-422, especially the goroutine at line 391 and its use of conf. Read the surrounding configuration-processing flow and check whether the goroutines can observe the intended range value. Done means determining whether the analyzer finding is a real bug and confirming the behavior after any focused correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend, distributed-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.