Crash under stress test
1a1a11a opened this issue · 10 comments
Hi there,
I tried to use it with fasthttp, but it seems it crashes immediately after I use multiple clients, the code involved are
cachedContent, _ = cache.Get(req)
if len(cachedContent) != 0{
ctx.Response.Header.Set("CacheHit", "frontendRAM")
ctx.SetBody(cachedContent)
}else{
cachedContent = []byte("objContents")
// save to memory
_ = cache.Set(req, cachedContent, 0)
ctx.Response.Header.Set("CacheHit", "chunkServerDecode")
}
Hers is the stack trace
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x458ee6]
goroutine 40 [running]:
github.com/coocood/freecache.(*RingBuf).Write(0xc0bed84240, 0x0, 0xb, 0x0, 0xc, 0x0, 0x0)
/home/ubuntu/src/github.com/coocood/freecache/ringbuf.go:89 +0x101
github.com/coocood/freecache.(*segment).set(0xc0bed84240, 0xc10bd3cdc8, 0xc, 0x18, 0x0, 0xb, 0x0, 0xe8a4600e8662d28, 0x0, 0xc10e2d5160, ...)
/home/ubuntu/src/github.com/coocood/freecache/segment.go:144 +0x56b
github.com/coocood/freecache.(*Cache).Set(0xc0bed78000, 0xc10bd3cdc8, 0xc, 0x18, 0x0, 0xb, 0x0, 0x0, 0xc000010260, 0x0)
/home/ubuntu/src/github.com/coocood/freecache/cache.go:53 +0x112
main.akamaiHandler(0xc10bd1f400)
/home/ubuntu/src/frontend/main.go:189 +0x595
main.requestHandler(0xc10bd1f400)
/home/ubuntu/src/frontend/main.go:87 +0x124
github.com/valyala/fasthttp.(*Server).serveConn(0xc10bcb6000, 0x725bc0, 0xc10bd0c020, 0x0, 0x0)
/home/ubuntu/src/github.com/valyala/fasthttp/server.go:1932 +0x6f7
github.com/valyala/fasthttp.(*Server).serveConn-fm(0x725bc0, 0xc10bd0c020, 0x1, 0x0)
/home/ubuntu/src/github.com/valyala/fasthttp/server.go:1538 +0x3e
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc10bcc2000, 0xc10bce0220)
/home/ubuntu/src/github.com/valyala/fasthttp/workerpool.go:212 +0xc3
github.com/valyala/fasthttp.(*workerPool).getCh.func1(0xc10bcc2000, 0xc10bce0220, 0x665a00, 0xc10bce0220)
/home/ubuntu/src/github.com/valyala/fasthttp/workerpool.go:184 +0x35
created by github.com/valyala/fasthttp.(*workerPool).getCh
/home/ubuntu/src/github.com/valyala/fasthttp/workerpool.go:183 +0x119
exit status 2
How is the cache created?
Hi @coocood, here is what I have done for cache initialization.
Cache = freecache.NewCache(ramCacheSize)
debug.SetGCPercent(20)
Do you have any cache instance created by new(freecache.Cache)
?
From the stack, it seems like the RingBuf
is nil, it only happens when cache is created by new(freecache.Cache
.
No I don't. But in order to be accessed by handler, I have the cache as global variable, var Cache *freecache.Cache
, then in the main function, I have Cache = freecache.NewCache(ramCacheSize)
.
I double checked that it only happens if there are multiple clients writing to it, if I wrap the set with lock, then it won't crash.
I can send a full test if that would be helpful.
OK, I am wrong, it does happen if I let it run longer, so it is not write data race problem.
There is another possibility that the value passed to Set
is nil.
You can try to add this code to verify if this is the case.
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&cachedContent))
fmt.Println(hdr.Data, hdr.Len, hdr.Cap)
If the hdr.Data
is 0, then this is the cause of the crash.
This is useful, the data passed to set was nil, now it works like a charm. Thank you!