gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.2k stars 1.64k forks source link

🐛 [Bug]: Cache middleware: index out of range panic (v2.48.0) #2600

Closed shell-skrimp closed 1 year ago

shell-skrimp commented 1 year ago

Bug Description

Requires the cache middleware to be enabled (see stacktrace below)

panic: runtime error: index out of range [559] with length 378
goroutine 2321 [running]:
github.com/gofiber/fiber/v2/middleware/cache.indexedHeap.Swap(...)
        github.com/gofiber/fiber/v2@v2.48.0/middleware/cache/heap.go:38
container/heap.Remove({0xc8ca50, 0xc000759980}, 0x22f)
        container/heap/heap.go:71 +0x4d
github.com/gofiber/fiber/v2/middleware/cache.(*indexedHeap).removeInternal(...)
        github.com/gofiber/fiber/v2@v2.48.0/middleware/cache/heap.go:80
github.com/gofiber/fiber/v2/middleware/cache.(*indexedHeap).remove(0xc000c5e6f0?, 0x2e?)
        github.com/gofiber/fiber/v2@v2.48.0/middleware/cache/heap.go:86 +0x36
github.com/gofiber/fiber/v2/middleware/cache.New.func4(0xc000004900)
        github.com/gofiber/fiber/v2@v2.48.0/middleware/cache/cache.go:128 +0x2ee
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc00015fb00?)
        github.com/gofiber/fiber/v2@v2.48.0/ctx.go:906 +0x3d
github.com/gofiber/fiber/v2/middleware/logger.New.func3(0xc000004900)
        github.com/gofiber/fiber/v2@v2.48.0/middleware/logger/logger.go:122 +0x3a5
github.com/gofiber/fiber/v2.(*App).next(0xc00012c480, 0xc000004900)
        github.com/gofiber/fiber/v2@v2.48.0/router.go:144 +0x1b2
github.com/gofiber/fiber/v2.(*App).handler(0xc00012c480, 0x4903af?)
        github.com/gofiber/fiber/v2@v2.48.0/router.go:171 +0x78
github.com/valyala/fasthttp.(*Server).serveConn(0xc000018400, {0xc8ff08?, 0xc000216960})
        github.com/valyala/fasthttp@v1.48.0/server.go:2363 +0x11d4
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0000a3a40, 0xc0003d61a0)
        github.com/valyala/fasthttp@v1.48.0/workerpool.go:224 +0xa4
github.com/valyala/fasthttp.(*workerPool).getCh.func1()
        github.com/valyala/fasthttp@v1.48.0/workerpool.go:196 +0x32
created by github.com/valyala/fasthttp.(*workerPool).getCh in goroutine 12
        github.com/valyala/fasthttp@v1.48.0/workerpool.go:195 +0x1ab

How to Reproduce

Unsure on how to exactly reproduce, the issue occurred after some time using the cache middle ware.

Expected Behavior

Expect no panic.

Fiber Version

v2.48.0

Code Snippet (optional)

No response

Checklist:

welcome[bot] commented 1 year ago

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

gaby commented 1 year ago

@shell-skrimp Do you have an example code?

shell-skrimp commented 1 year ago

@gaby All I do is enable the cache middleware and nothing more

    d, err := time.ParseDuration(site.CacheDuration)
    if err != nil {
        return err
    }

    app.Use(cache.New(cache.Config{
        ExpirationGenerator: func(c *fiber.Ctx, cfg *cache.Config) time.Duration {
            newCacheTime, _ := strconv.Atoi(c.GetRespHeader("Cache-Control", fmt.Sprintf("max-age=%.0f", d.Seconds())))
            return time.Second * time.Duration(newCacheTime)
        },
        KeyGenerator: func(c *fiber.Ctx) string {
            return c.Path()
        },
        Next: func(c *fiber.Ctx) bool {
            return c.Query("refresh") == "true"
        },
        MaxBytes:     site.CacheSizeBytes,
        Expiration:   d,
        CacheControl: true,
    }))
ReneWerner87 commented 1 year ago

@shell-skrimp error is that you use in your keyGenerator the path as key without creating a copy image

at the end of the request and response process for this request, the path is released again, but is referenced by your use in the cache map and then this value is reused and adjusted

solution is to make a copy, as also in our documentation for the default config https://docs.gofiber.io/api/middleware/cache#default-config

more about the reference topic -> https://docs.gofiber.io/#zero-allocation