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 s used in defer or goroutine at line 183
Click here to show the 83 line(s) of Go which triggered the analyzer.
```go
for _, s := range w.server {
w.noopFlag[s] = make(FlagChan)
wg.Add(1)
go func(server string) {
defer wg.Done()
for {
w.descJobs()
// retrieve next job
var req = newRequestToServerWithType(server, PtGrabJobAll)
resp, err := w.sender.sendAndWaitResp(req)
if err != nil {
Log.Printf("send grab req to %s fail, %s", server, err.Error())
return
}
Log.Printf("send grab request to %s suc", s)
switch resp.Type {
case PtNoJob:
// send pre sleep then Wait for wake up signal
var req = newRequestToServerWithType(server, PtPreSleep)
peer, err := w.sender.send(req)
if err != nil {
Log.Printf("send pre sleep packet to %s fail, %s", server, err)
return
}
Log.Printf("job retriever for %s Wait for weekup", server)
select {
case <-w.noopFlag[server]:
Log.Printf("job retriever for %s weekup", server)
case <-peer.Closed():
Log.Printf("job retriever for %s err. ", err.Error())
}
case PtJobAssign, PtJobAssignUnique, PtJobAssignAll:
go func(resp *Response) {
defer w.descJobs()
var job = &Job{w: w, resp: resp}
funcName, _ := job.resp.GetFuncName()
handle, ok := w.jobHandles[funcName]
if !ok {
Log.Printf("no worker handle found for job %s", funcName)
return
}
handleId, err := job.resp.GetHandle()
if err != nil {
log.Printf("get job handle err, %s", err)
return
}
// handle the job
data, err := handle(job)
var req = newRequestTo(server)
// set req type
if err != nil {
req.SetType(PtWorkFail)
} else {
req.SetType(PtWorkComplete)
req.SetData(data)
}
if err = req.SetHandle(handleId); err != nil {
log.Printf("req set handle err, %s", err.Error())
return
}
if _, err := w.sender.send(req); err != nil {
Log.Printf("send job result request %d err, %s", req.Type, err.Error())
}
}(resp)
}
}
}(s)
}
```
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 for more information.
Found a possible issue in zhanghjster/go-gearman at worker.go
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.
Click here to see the code in its original context.
Click here to show the 83 line(s) of Go which triggered the analyzer.
```go for _, s := range w.server { w.noopFlag[s] = make(FlagChan) wg.Add(1) go func(server string) { defer wg.Done() for { w.descJobs() // retrieve next job var req = newRequestToServerWithType(server, PtGrabJobAll) resp, err := w.sender.sendAndWaitResp(req) if err != nil { Log.Printf("send grab req to %s fail, %s", server, err.Error()) return } Log.Printf("send grab request to %s suc", s) switch resp.Type { case PtNoJob: // send pre sleep then Wait for wake up signal var req = newRequestToServerWithType(server, PtPreSleep) peer, err := w.sender.send(req) if err != nil { Log.Printf("send pre sleep packet to %s fail, %s", server, err) return } Log.Printf("job retriever for %s Wait for weekup", server) select { case <-w.noopFlag[server]: Log.Printf("job retriever for %s weekup", server) case <-peer.Closed(): Log.Printf("job retriever for %s err. ", err.Error()) } case PtJobAssign, PtJobAssignUnique, PtJobAssignAll: go func(resp *Response) { defer w.descJobs() var job = &Job{w: w, resp: resp} funcName, _ := job.resp.GetFuncName() handle, ok := w.jobHandles[funcName] if !ok { Log.Printf("no worker handle found for job %s", funcName) return } handleId, err := job.resp.GetHandle() if err != nil { log.Printf("get job handle err, %s", err) return } // handle the job data, err := handle(job) var req = newRequestTo(server) // set req type if err != nil { req.SetType(PtWorkFail) } else { req.SetType(PtWorkComplete) req.SetData(data) } if err = req.SetHandle(handleId); err != nil { log.Printf("req set handle err, %s", err.Error()) return } if _, err := w.sender.send(req); err != nil { Log.Printf("send job result request %d err, %s", req.Type, err.Error()) } }(resp) } } }(s) } ```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 for more information.
commit ID: 6461511c2f8fec3914300e59561ea4e6f6894906