bigcache icon indicating copy to clipboard operation
bigcache copied to clipboard

v2.2.3 panic

Open work-chausat opened this issue 3 years ago • 3 comments

panic: runtime error: index out of range [7] with length 7

goroutine 491 [running]: encoding/binary.littleEndian.Uint64(...) /export/App/go/src/encoding/binary/binary.go:77 github.com/baudtime/baudtime/vendor/github.com/allegro/bigcache/v2.readTimestampFromEntry(...) /export/App/gopath/src/github.com/baudtime/baudtime/vendor/github.com/allegro/bigcache/v2/encoding.go:43 github.com/baudtime/baudtime/vendor/github.com/allegro/bigcache/v2.(*cacheShard).onEvict(0xc12892c360, 0xc1b072c275, 0x7, 0x14fb, 0x5f1fc08d, 0xc136bf8ad0, 0x0) /export/App/gopath/src/github.com/baudtime/baudtime/vendor/github.com/allegro/bigcache/v2/shard.go:247 +0x7d github.com/baudtime/baudtime/vendor/github.com/allegro/bigcache/v2.(*cacheShard).set(0xc12892c360, 0xc0669891f0, 0xd, 0x97320e0161446698, 0xc0669891f0, 0xd, 0xc136bf8e30, 0xf273c0, 0xc000199ce0) /export/App/gopath/src/github.com/baudtime/baudtime/vendor/github.com/allegro/bigcache/v2/shard.go:131 +0x30f github.com/baudtime/baudtime/vendor/github.com/allegro/bigcache/v2.(*BigCache).Set(0xc11cab3860, 0xc0669891f0, 0xd, 0xc0669891f0, 0xd, 0xc136bf8e30, 0xf273c0, 0xc000199ce0) /export/App/gopath/src/github.com/baudtime/baudtime/vendor/github.com/allegro/bigcache/v2/bigcache.go:133 +0xac

work-chausat avatar Jul 28 '20 06:07 work-chausat

duplicates https://github.com/allegro/bigcache/issues/226

janisz avatar Jul 28 '20 10:07 janisz

Could be reproduced with following

package main

import (
	"fmt"
	"math/rand"
	"runtime"
	"strconv"
	"time"

	. "github.com/allegro/bigcache/v2"
	"github.com/pkg/profile"
)

const (
	entries = 3000
	repeats = 1000
)

var val_big = append(make([]byte, 100 * 1024), 1)
var val_med = append(make([]byte, 1024), 1)
var val_small = append(make([]byte, 2), 1)

func get_value() []byte {
	x := rand.Float64()
	if x < 0.7 {
		return val_small
	} else if x < 0.9 {
		return  val_med
	}
	return val_big
}

func main() {
	defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
	fmt.Println("Number of entries: ", entries)
	printAllocs()

	config := Config{
		Shards:             128,
		LifeWindow:         time.Hour,
		CleanWindow:        time.Second,
		MaxEntriesInWindow: entries,
		MaxEntrySize:       1024,
		Verbose:            true,
		HardMaxCacheSize:   128,
		OnRemoveWithReason: func(key string, entry []byte, reason RemoveReason) {
			fmt.Println("Evicted:",  len(entry), " reason: ", reason)
		},
	}

	bigcache, err := NewBigCache(config)
	if err != nil {
		panic(err)
	}
	for i := 0; i < repeats; i++ {
		printAllocs()
		for j := 0; j < entries; j++ {
			key := strconv.FormatInt(int64(j), 10)
			err := bigcache.Set(key, get_value())
			if err != nil {
				panic(err)
			}
		}
	}
}

func printAllocs() {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	fmt.Printf("Alloc: %6d MB \n", m.Alloc / 1e6)
}

Output

go run main.go
2020/08/14 15:33:44 profile: memory profiling enabled (rate 4096), mem.pprof
goroutine 1 [running]:
github.com/allegro/bigcache/v2.readHashFromEntry(...)
	/go/pkg/mod/github.com/allegro/bigcache/[email protected]/encoding.go:57
github.com/allegro/bigcache/v2.(*cacheShard).removeOldestEntry(0xc000340ea0, 0xc000000002, 0x19017, 0x19017)
	/go/pkg/mod/github.com/allegro/bigcache/[email protected]/shard.go:296 +0x17d
github.com/allegro/bigcache/v2.(*cacheShard).set(0xc000340ea0, 0xc000170ff8, 0x4, 0xf21040b304196e1, 0xc000100000, 0x19001, 0x20000, 0x7c6e0fa4f545dddc, 0xc000090180)
	/go/pkg/mod/github.com/allegro/bigcache/[email protected]/shard.go:142 +0x1df
github.com/allegro/bigcache/v2.(*BigCache).Set(0xc000092d00, 0xc000170ff8, 0x4, 0xc000100000, 0x19001, 0x20000, 0x0, 0x0)
	/go/pkg/mod/github.com/allegro/bigcache/[email protected]/bigcache.go:133 +0xac
main.main()
	/main.go:59 +0x2ea
exit status 2
```'


env

go version go1.14.4 darwin/amd64 github.com/allegro/bigcache/v2 v2.2.3


PaulLiang1 avatar Aug 14 '20 05:08 PaulLiang1

https://github.com/allegro/bigcache/issues/148#issuecomment-579364262

janisz avatar Aug 14 '20 12:08 janisz