Skip to content

🐛 [Bug]: DefaultCtx.Done() returns nil — cancellation impossible through context.Context interface #4335

Description

@pageton

Bug Description

DefaultCtx.Done() returns nil, which blocks forever in any select statement. This means:

  • Users cannot detect client disconnection via ctx.Done()
  • Libraries accepting context.Context (database drivers, gRPC clients, HTTP clients) will never respect cancellation when passed Fiber's DefaultCtx
  • The context.Context interface contract is broken: Err() always returns nil, Deadline() always returns no deadline

How to Reproduce

Steps to reproduce the behavior:

  1. Create a handler that passes c (the DefaultCtx) to a library expecting context.Context
  2. The client disconnects or a timeout fires
  3. The library never detects cancellation because Done() returns nil
app.Get("/", func(c fiber.Ctx) error {
    // This will never be cancelled even if the client disconnects
    rows, err := db.QueryContext(c, "SELECT pg_sleep(60)")
    // ...
})

Affected Code

Done()ctx.go:168-170

func (*DefaultCtx) Done() <-chan struct{} {
    return nil  // blocks forever in select
}

Err()ctx.go:176-178

func (*DefaultCtx) Err() error {
    return nil  // always returns nil, never reflects cancellation
}

Deadline()ctx.go:156-158

func (*DefaultCtx) Deadline() (time.Time, bool) {
    return time.Time{}, false  // always returns no deadline
}

Contrast with fasthttp

Fasthttp's RequestCtx.Done() returns a server-level channel that at least signals server shutdown. Fiber's implementation is strictly less functional.

Expected Behavior

Done(), Err(), and Deadline() should delegate to the user context stored via SetContext():

func (c *DefaultCtx) Done() <-chan struct{} {
    return c.Context().Done()
}

func (c *DefaultCtx) Err() error {
    return c.Context().Err()
}

func (c *DefaultCtx) Deadline() (time.Time, bool) {
    return c.Context().Deadline()
}

This would propagate cancellation from context.WithTimeout (set by the timeout middleware) or any user-set cancelable context. When no context is set, c.Context() returns context.Background() whose Done() is nil — matching current behavior.

Fiber Version

v3 (latest main branch)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions