Skip to content

πŸ› [Bug]: Session methods lack *WithContext variants β€” no way to propagate cancellationΒ #4336

@pageton

Description

@pageton

Bug Description

Session methods that perform storage I/O (Destroy, Regenerate, Reset, Save) do not accept a context.Context parameter. They use an internal fiber.Ctx field which has no-op cancellation (Done() returns nil). Users have no way to pass a properly-scoped context for cancellation or timeout control.

How to Reproduce

Steps to reproduce the behavior:

  1. Get a session in a handler: sess, _ := session.Get(c)
  2. Call sess.Destroy() β€” this performs Storage.DeleteWithContext() internally
  3. The storage backend is slow (e.g., Redis over a slow network)
  4. There is no way to set a timeout or cancellation on this operation
  5. Even if the client disconnects, the storage I/O runs to completion

Affected Methods

Method Line I/O Operation
Destroy() session/session.go:187 Storage.DeleteWithContext(ctx, s.id)
Regenerate() session/session.go:220 Storage.DeleteWithContext(ctx, s.id)
Reset() session/session.go:247 Storage.DeleteWithContext(ctx, s.id)
Save() / saveSession() session/session.go:294,311 Storage.SetWithContext(ctx, s.id, ...)

All four use this fallback pattern:

var ctx context.Context = s.ctx
if ctx == nil {
    ctx = context.Background()
}

When s.ctx is nil (after Release() or when created via Store.GetByID()), the fallback is context.Background() β€” uncancellable.

Expected Behavior

Add *WithContext variants, mirroring the SharedState pattern:

func (s *Session) DestroyWithContext(ctx context.Context) error { ... }
func (s *Session) RegenerateWithContext(ctx context.Context) error { ... }
func (s *Session) ResetWithContext(ctx context.Context) error { ... }
func (s *Session) SaveWithContext(ctx context.Context) error { ... }

The existing methods can delegate to the *WithContext variants with the stored s.ctx as fallback.

Related

Fiber Version

v3 (latest main branch)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions