Skip to content

🐛 fix(ctx): delegate Deadline/Done/Err to the user context#4465

Open
yathboss wants to merge 1 commit into
gofiber:mainfrom
yathboss:fix/ctx-cancellation-delegation
Open

🐛 fix(ctx): delegate Deadline/Done/Err to the user context#4465
yathboss wants to merge 1 commit into
gofiber:mainfrom
yathboss:fix/ctx-cancellation-delegation

Conversation

@yathboss

Copy link
Copy Markdown

Description

DefaultCtx.Deadline(), Done() and Err() were hardcoded no-ops, so the context.Context contract was broken:

  • Done() returned nil → blocks forever in any select
  • Err() always returned nil → never reflected cancellation
  • Deadline() always reported no deadline

Because of this, a cancelable context set via SetContextincluding the context.WithTimeout installed by the timeout middleware — was invisible to any library that consumed fiber.Ctx as a context.Context (database drivers, HTTP/gRPC clients, etc.). They would never respect cancellation.

Fix

Delegate the three methods to c.Context():

func (c *DefaultCtx) Deadline() (time.Time, bool) { return c.Context().Deadline() }
func (c *DefaultCtx) Done() <-chan struct{}       { return c.Context().Done() }
func (c *DefaultCtx) Err() error                  { return c.Context().Err() }

When no context has been set, c.Context() returns context.Background(), whose Done() is nil, Err() is nil and Deadline() reports no deadline — so existing behavior is preserved and the change is backward compatible.

Tests

  • Added Test_Ctx_Deadline_Delegates, Test_Ctx_Done_Delegates, Test_Ctx_Err_Delegates covering deadline/cancellation propagation.
  • Existing Test_Ctx_Deadline, Test_Ctx_Done, Test_Ctx_Err (no-context case) still pass, confirming backward compatibility.
  • go vet clean; timeout-middleware and Context tests pass.

Updated the context.Context section in docs/api/ctx.md.

Fixes #4335

DefaultCtx.Deadline(), Done() and Err() were hardcoded no-ops, so the
context.Context contract was broken: Done() returned nil (blocking forever
in select), Err() always returned nil, and Deadline() never reported a
deadline. Cancellation set via SetContext — including the context.WithTimeout
installed by the timeout middleware — was therefore invisible to any library
that consumed fiber.Ctx as a context.Context.

Delegate the three methods to c.Context(). When no context has been set,
c.Context() returns context.Background(), preserving the previous behavior
(nil Done, nil Err, no deadline) so the change is backward compatible.

Adds tests covering cancellation/deadline propagation and updates the
context.Context docs.

Fixes gofiber#4335

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@yathboss yathboss requested a review from a team as a code owner June 26, 2026 20:23
@welcome

welcome Bot commented Jun 26, 2026

Copy link
Copy Markdown

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

DefaultCtx now delegates Deadline, Done, and Err to the context set via SetContext, falling back to context.Background() when unset. Tests cover deadline and cancellation behavior, and the API docs describe the updated compatibility contract.

Changes

Context cancellation delegation

Layer / File(s) Summary
Context methods delegate
ctx.go
DefaultCtx.Deadline(), Done(), and Err() now delegate to Context() instead of returning fixed empty, nil, or nil values.
Tests and docs
ctx_test.go, docs/api/ctx.md
New tests assert delegated deadline and cancellation behavior, and the API docs describe the SetContext / context.Background() behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

  • gofiber/fiber issue 4338: Matches the same DefaultCtx cancellation path by delegating Deadline(), Done(), and Err() to the stored context.

Possibly related PRs

  • gofiber/fiber#3382: Directly related to the same DefaultCtx context delegation path, but in the opposite direction.
  • gofiber/fiber#3720: Introduced the Context() / SetContext() storage used by the new delegation behavior.

Suggested labels

📒 Documentation

Suggested reviewers

  • sixcolors
  • efectn
  • ReneWerner87
  • gaby

Poem

A bunny hopped through context bright,
And found Done() blinking back to light.
Err() and deadlines sprang alive,
So cancelable tunnels now can thrive.
🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the core change: delegating Deadline/Done/Err to the user context.
Description check ✅ Passed The description covers the bug, fix, tests, docs update, and issue reference, which is mostly complete for this template.
Linked Issues check ✅ Passed The code, tests, and docs match #4335 by delegating context methods and preserving background fallback behavior.
Out of Scope Changes check ✅ Passed The changes stay focused on context delegation, related tests, and documentation with no obvious unrelated additions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ReneWerner87 ReneWerner87 added this to v3 Jun 26, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone Jun 26, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
ctx.go (1)

158-184: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Update the generated Ctx interface comments to match this contract.

ctx_interface_gen.go still says Deadline(), Done(), and Err() are fasthttp no-ops, so the GoDoc now contradicts both this implementation and the updated Markdown docs. Please regenerate or update that interface documentation in the same change.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ctx.go` around lines 158 - 184, The generated Ctx interface comments are
outdated and still describe Deadline(), Done(), and Err() as fasthttp no-ops,
which contradicts the DefaultCtx methods in ctx.go and the current docs. Update
the interface documentation in ctx_interface_gen.go so it matches the contract
implemented by DefaultCtx.Context(), Deadline(), Done(), and Err(), or
regenerate that file if it is derived from templates, ensuring the GoDoc
reflects delegation to the configured context rather than no-op behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/api/ctx.md`:
- Line 83: The timeout middleware reference in the Ctx documentation is pointing
to the wrong relative path. Update the markdown link in docs/api/ctx.md so the
mention of the timeout middleware resolves from the API docs folder to the
middleware docs using the correct relative target, and keep the rest of the
Ctx/SetContext/Context wording unchanged.

---

Outside diff comments:
In `@ctx.go`:
- Around line 158-184: The generated Ctx interface comments are outdated and
still describe Deadline(), Done(), and Err() as fasthttp no-ops, which
contradicts the DefaultCtx methods in ctx.go and the current docs. Update the
interface documentation in ctx_interface_gen.go so it matches the contract
implemented by DefaultCtx.Context(), Deadline(), Done(), and Err(), or
regenerate that file if it is derived from templates, ensuring the GoDoc
reflects delegation to the configured context rather than no-op behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6e3b63d4-bf8a-49f9-820d-fbb6316bd57c

📥 Commits

Reviewing files that changed from the base of the PR and between 464387b and 2912584.

📒 Files selected for processing (3)
  • ctx.go
  • ctx_test.go
  • docs/api/ctx.md

Comment thread docs/api/ctx.md
### context.Context

`Ctx` implements `context.Context`. However due to [current limitations in how fasthttp](https://github.com/valyala/fasthttp/issues/965#issuecomment-777268945) works, `Deadline()`, `Done()` and `Err()` are no-ops. The `fiber.Ctx` instance is reused after the handler returns and must not be used for asynchronous operations once the handler has completed. Call [`Context`](#context) within the handler to obtain a `context.Context` that can be used outside the handler.
`Ctx` implements `context.Context`. `Deadline()`, `Done()` and `Err()` delegate to the context set via [`SetContext`](#setcontext) (the same context returned by [`Context`](#context)). When no context has been set they fall back to `context.Background()`, so `Done()` returns `nil`, `Err()` returns `nil` and `Deadline()` reports no deadline. This means a cancelable context — such as the one installed by the [timeout middleware](./middleware/timeout.md) — propagates cancellation through `c.Done()`/`c.Err()`. The `fiber.Ctx` instance is reused after the handler returns and must not be used for asynchronous operations once the handler has completed. Call [`Context`](#context) within the handler to obtain a `context.Context` that can be used outside the handler.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
python - <<'PY'
from pathlib import Path

src = Path("docs/api/ctx.md")
for rel in ("./middleware/timeout.md", "../middleware/timeout.md"):
    target = (src.parent / rel).resolve()
    print(f"{rel} -> {target} exists={target.exists()}")
PY

Repository: gofiber/fiber

Length of output: 327


Fix the timeout middleware link docs/api/ctx.md should use ../middleware/timeout.md; ./middleware/timeout.md points under docs/api/ and does not exist.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/api/ctx.md` at line 83, The timeout middleware reference in the Ctx
documentation is pointing to the wrong relative path. Update the markdown link
in docs/api/ctx.md so the mention of the timeout middleware resolves from the
API docs folder to the middleware docs using the correct relative target, and
keep the rest of the Ctx/SetContext/Context wording unchanged.

@gaby

gaby commented Jun 26, 2026

Copy link
Copy Markdown
Member

This is a duplicate of #4399

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

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

3 participants