gofiber / fiber

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

🐛 Requests from Google Chrome get stuck on Read call #1905

Closed fabiobulgarella closed 1 year ago

fabiobulgarella commented 2 years ago

Fiber version 2.33.0

Issue description When requests come from Google Chrome, after being served, they get stuck on Read call. To reproduce the problem use the below "Graceful Shutdown Example". When ReadTimeout is set in FiberConfig, requests from Chrome get stuck on Read until timeout expires, producing a Request Timeout error. When ReadTimeout is set to 0, no error is thrown but Fiber App can't shutdown until the browser close the request connection. This not happen using other browser. I tried with Firefox and Safari, requests do not stay blocked on Read and App can be shut down without any problem. Maybe it could be a fasthttp related problem, but I'm not sure. Here the stack trace of the Request Timeout error:

panic: Request Timeout

goroutine 36 [running]:
main.main.func1(0x14000290000, {0x104ae3ee0, 0x14000120288})
        /Users/fabio/Source/fiber-gshut/main.go:22 +0x38
github.com/gofiber/fiber/v2.(*App).ErrorHandler(0x1400017e000, 0x14000290000, {0x104ae3ee0, 0x14000120288})
        /Users/fabio/go/pkg/mod/github.com/gofiber/fiber/v2@v2.33.0/app.go:1092 +0x1c8
github.com/gofiber/fiber/v2.(*App).serverErrorHandler(0x1400017e000, 0x140000d2000, {0x104ae4200, 0x140000260f0})
        /Users/fabio/go/pkg/mod/github.com/gofiber/fiber/v2@v2.33.0/app.go:1114 +0x1bc
github.com/valyala/fasthttp.(*Server).writeErrorResponse(0x1400014a6c0, 0x0, 0x140000d2000, {0x0, 0x0, 0x0}, {0x104ae4200, 0x140000260f0})
        /Users/fabio/go/pkg/mod/github.com/valyala/fasthttp@v1.36.0/server.go:2828 +0x54
github.com/valyala/fasthttp.(*Server).serveConn(0x1400014a6c0, {0x104aebc50, 0x1400009e058})
        /Users/fabio/go/pkg/mod/github.com/valyala/fasthttp@v1.36.0/server.go:2266 +0x1a9c
github.com/valyala/fasthttp.(*workerPool).workerFunc(0x140000c6000, 0x140000a0080)
        /Users/fabio/go/pkg/mod/github.com/valyala/fasthttp@v1.36.0/workerpool.go:224 +0x7c
github.com/valyala/fasthttp.(*workerPool).getCh.func1(0x140000c6000, 0x140000a0080, {0x104a89d80, 0x140000a0080})
        /Users/fabio/go/pkg/mod/github.com/valyala/fasthttp@v1.36.0/workerpool.go:196 +0x30
created by github.com/valyala/fasthttp.(*workerPool).getCh
        /Users/fabio/go/pkg/mod/github.com/valyala/fasthttp@v1.36.0/workerpool.go:195 +0x1e8

Code snippet

package main

import (
    "fmt"
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/favicon"
    "log"
    "os"
    "os/signal"
    "syscall"
    "time"
)

const idleTimeout = 10 * time.Second
const readTimeout = 30 * time.Second

func main() {
    app := fiber.New(fiber.Config{
        IdleTimeout: idleTimeout,
        ReadTimeout: readTimeout,
        ErrorHandler: func(ctx *fiber.Ctx, err error) error {
            fmt.Println("Error:", err)
            return nil
        },
    })
    app.Use(favicon.New())

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello world!")
    })

    // Listen from a different goroutine
    go func() {
        if err := app.Listen(":3000"); err != nil {
            log.Panic(err)
        }
    }()

    c := make(chan os.Signal, 1) // Create channel to signify a signal being sent
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)

    _ = <-c // This blocks the main thread until an interrupt is received
    fmt.Println("Gracefully shutting down...")
    _ = app.Shutdown()

    fmt.Println("Running cleanup tasks...")

    // Your cleanup tasks go here
    // db.Close()
    // redisConn.Close()
    fmt.Println("Fiber was successful shutdown.")
}
welcome[bot] commented 2 years 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

leonklingele commented 1 year ago

Hi @fabiobulgarella, I just tried to reproduce this using fiber v2.40.1 and Google Chrome 108.0.5359.124, however everything seems to work fine for me.

This is what I did:

  1. Run your code
  2. Issue some requests to the server
  3. SIGINT the server
  4. While the server is gracefully shutting down, issue some more requests

Is this what you did to cause the panic? Is it still producible on your side using the latest version of fiber?

leonklingele commented 1 year ago

Has this been fixed or closed due to no response?

ReneWerner87 commented 1 year ago

it was closed because it was not reproducible and there was no response in the last year, so we have to assume that this problem does not occur