Skip to content

🐛 [Bug]: Middleware timeout returns nil by default and results in a panic. #3674

@xlients-dev

Description

@xlients-dev

Bug Description

Hi, when I was running my application, all endpoints were panicking and crashing, and after I traced the panic error it came from the timeout middleware that I declared by default on the global middleware.
I hope this will resolve the error that occurred.

How to Reproduce

when i do curl on my endpoint it will panic and cause crash.

root@root:~# curl localhost:8080/api/auth/login

and displays output like this, then exit status 2.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xac9823]

goroutine 18 [running]:
github.com/gofiber/fiber/v3/middleware/timeout.runHandler({0xdeffa0, 0x3d6008}, 0x0?, {0x0, 0x0, 0x0, 0x0})
	/path/to/go/pkg/mod/github.com/gofiber/fiber/v3@v3.0.0-beta.5/middleware/timeout/timeout.go:43 +0x43
main.InitServer.New.func2({0xdeffa0, 0x3d6008})
	/path/to/go/pkg/mod/github.com/gofiber/fiber/v3@v3.0.0-beta.5/middleware/timeout/timeout.go:22 +0xc5
github.com/gofiber/fiber/v3.(*DefaultCtx).Next(0x68808?)
	/path/to/go/pkg/mod/github.com/gofiber/fiber/v3@v3.0.0-beta.5/ctx.go:1171 +0x47
github.com/gofiber/fiber/v3/middleware/etag.New.func1({0xdeffa0, 0x3d6008})
	/path/to/go/pkg/mod/github.com/gofiber/fiber/v3@v3.0.0-beta.5/middleware/etag/etag.go:33 +0xd1
github.com/gofiber/fiber/v3.(*DefaultCtx).Next(0x98808?)
	/path/to/go/pkg/mod/github.com/gofiber/fiber/v3@v3.0.0-beta.5/ctx.go:1171 +0x47
main.InitServer.New.func1({0xdeffa0, 0x3d6008})
	/path/to/go/pkg/mod/github.com/gofiber/fiber/v3@v3.0.0-beta.5/middleware/requestid/requestid.go:40 +0x144
github.com/gofiber/fiber/v3.(*DefaultCtx).Next(0x988d8?)
	/path/to/go/pkg/mod/github.com/gofiber/fiber/v3@v3.0.0-beta.5/ctx.go:1171 +0x47
github.com/gofiber/fiber/v3/middleware/compress.New.func3({0xdeffa0, 0x3d6008})
	/path/to/go/pkg/mod/github.com/gofiber/fiber/v3@v3.0.0-beta.5/middleware/compress/compress.go:54 +0x50
github.com/gofiber/fiber/v3.(*DefaultCtx).Next(0x89182?)
	/path/to/go/pkg/mod/github.com/gofiber/fiber/v3@v3.0.0-beta.5/ctx.go:1171 +0x47
github.com/gofiber/fiber/v3/middleware/helmet.New.func1({0xdeffa0, 0x3d6008})
	... (stack trace continues)

And other accesses also experience the same thing, when I access the endpoint on the CLI and website, the result is still panic and crash.

Expected Behavior

I don't know whether this is an error from the fiber middleware library which doesn't indicate a fixed value so that it doesn't produce nil, or my own error in forgetting to configure it, and the panic happens on all endpoints because I declared this middleware globally and by default.

but for sure, I declared the middleware from the fiber library and used it by default like other middleware.
Is it mandatory to modify the timeout middleware first?

Fiber Version

Fiber V3 latest

Code Snippet (optional)

package server

import (
some import...
)

type Server struct {
        app *fiber.App
}

func InitServer() *Server {
        s := &Server{
                app: fiber.New(fiber.Config{
                        AppName:       "Myapp",
                        StrictRouting: true,
                }),
        }

        // DB MIGRATE
        if r := db.DB.AutoMigrate(
                &models.User{},
                &models.Tickets{},
        ); r != nil {
                logger.Error.Fatalf("Database migrattion failed\nError: %v", r)
        }

        // global middleware
        s.app.Use(cors.New())
        s.app.Use(helmet.New())
        s.app.Use(requestid.New())

        s.app.Use(timeout.New(timeout.ConfigDefault.OnTimeout)) //I declared this as default on global, and all endpoints panicked.

        s.app.Use(recover.New(recover.Config{
                EnableStackTrace: true, // The panic is captured by the recover middleware on the output endpoint.
        }))

      // register routes
        routes.RegisterRoutes(s.app)

        return &Server{app: s.app}
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions