Panic on pool.SetSize(5) via http request
4n70w4 opened this issue · 3 comments
4n70w4 commented
var pool tunny.Pool
...
numCPUs := runtime.NumCPU()
pool := tunny.NewFunc(numCPUs, func(payload interface{}) interface{} {
time.Sleep(time.Second)
log.Println(fmt.Sprintf("superprocess %v", payload))
return nil
})
pool.SetSize(1)
defer pool.Close()
...
http.HandleFunc("/tune", endpointTune)
...
go func() {
for {
for Deque.Len() != 0 {
go pool.Process(Deque.PopFront())
}
time.Sleep(time.Second)
}
}()
package main
import (
"net/http"
)
type TuneRequest struct {
PoolSize int
}
func endpointTune(res http.ResponseWriter, req *http.Request) {
r := TuneRequest{}
err := decoder.Decode(&r, req.URL.Query())
if err != nil {
return
}
pool.SetSize(r.PoolSize)
}
2020/04/07 11:45:23 http: panic serving [::1]:57105: runtime error: invalid memory address or nil pointer dereference
goroutine 27 [running]:
net/http.(*conn).serve.func1(0xc0000a2b40)
/Go/src/net/http/server.go:1772 +0x150
panic(0x840e40, 0xb3e540)
/Go/src/runtime/panic.go:973 +0x429
github.com/Jeffail/tunny.(*Pool).SetSize(0xb4b460, 0x5)
/Go/bin/src/github.com/Jeffail/tunny/tunny.go:236 +0x108
main.endpointTune(0x90ef20, 0xc00010c000, 0xc000132100)
/src/endpointTune.go:24 +0xf4
net/http.HandlerFunc.ServeHTTP(0x8bc580, 0x90ef20, 0xc00010c000, 0xc000132100)
/Go/src/net/http/server.go:2012 +0x4b
net/http.(*ServeMux).ServeHTTP(0xb4b4e0, 0x90ef20, 0xc00010c000, 0xc000132100)
/Go/src/net/http/server.go:2387 +0x1ad
net/http.serverHandler.ServeHTTP(0xc000180000, 0x90ef20, 0xc00010c000, 0xc000132100)
/Go/src/net/http/server.go:2807 +0x216
net/http.(*conn).serve(0xc0000a2b40, 0x90f520, 0xc0000e1780)
/Go/src/net/http/server.go:1895 +0x171d
created by net/http.(*Server).Serve
/Go/src/net/http/server.go:2933 +0x938
fairyhunter13 commented
@4n70w4 You use pool := ....
. It should be pool = ....
.
4n70w4 commented
@fairyhunter13 but what does it have to do with it?
doron-cohen commented
When you use pool := tunny...
you redefine pool
inside the function scope. The global var pool tunny.Pool
stays uninitialized. So, when you refer to it in endpointTune
it's value is nil
and you basically get nil pointer dereference.