Skip to content

🐛 bug: delegate Ctx context methods to SetContext#4399

Open
puneetdixit200 wants to merge 3 commits into
gofiber:mainfrom
puneetdixit200:bug/defaultctx-context-methods
Open

🐛 bug: delegate Ctx context methods to SetContext#4399
puneetdixit200 wants to merge 3 commits into
gofiber:mainfrom
puneetdixit200:bug/defaultctx-context-methods

Conversation

@puneetdixit200

Copy link
Copy Markdown

Summary

  • Delegate DefaultCtx.Deadline, Done, and Err to the context set by SetContext, while preserving the current no-op fallback when no context is set.
  • Add regression coverage for custom context deadline and cancellation propagation.
  • Update context docs/generated interface comments and switch stale fatal-log revive suppressions to direct revive:disable-line directives so the repo lint target stays clean.

Closes #4335

Test plan

  • go test ./ -run 'Test_Ctx_(Deadline|Done|Err|Context|ContextMethods_UseCustomContext)' -count=1
  • make generate
  • make format
  • make betteralign
  • git diff --check
  • make lint
  • make test
  • GOVERSION=go1.26.4 make audit
  • make markdown

Copilot AI review requested due to automatic review settings June 4, 2026 02:54
@puneetdixit200 puneetdixit200 requested a review from a team as a code owner June 4, 2026 02:54
@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2d06b74a-f394-4146-b1e6-32d0a5ba6779

📥 Commits

Reviewing files that changed from the base of the PR and between 551fc3b and 4255af0.

📒 Files selected for processing (4)
  • ctx.go
  • ctx_interface_gen.go
  • ctx_test.go
  • docs/api/ctx.md
✅ Files skipped from review due to trivial changes (2)
  • docs/api/ctx.md
  • ctx_interface_gen.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • ctx_test.go
  • ctx.go

Walkthrough

DefaultCtx now forwards Deadline, Done, and Err to a stored context set via SetContext, with no-op fallback when none is present. Related interface comments, user docs, and tests were updated to match.

Changes

Context Delegation

Layer / File(s) Summary
Method delegation and test coverage
ctx.go, ctx_test.go
DefaultCtx.Deadline, Done, and Err now delegate to a stored user context when present, and a new test asserts the methods mirror a canceled custom context.
Interface and guide docs
ctx_interface_gen.go, docs/api/ctx.md, docs/guide/context.md
The Ctx interface comments and context docs now describe delegation through SetContext and the no-op fallback when no context is set.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • gofiber/fiber#3720: Introduces Context() / SetContext, which this PR now uses for Deadline(), Done(), and Err() delegation.
  • gofiber/fiber#3382: Touches the same DefaultCtx.Deadline/Done/Err behavior and related documentation/tests.
  • gofiber/fiber#4393: Uses DefaultCtx context propagation in session storage, which is affected by the new delegation behavior.

Suggested labels

🧹 Updates

Suggested reviewers

  • gaby
  • sixcolors
  • efectn
  • ReneWerner87

Poem

A rabbit hopped through context green,
With SetContext tucked in between.
Done() twitched, Err() rang clear,
Deadline() bloomed bright and near,
🐇 Hop-hop — cancellation now seen!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main fix: delegating Ctx context methods to SetContext.
Description check ✅ Passed The description includes the change summary, linked issue, and test plan, which is mostly complete for the template.
Linked Issues check ✅ Passed The code matches #4335 by delegating Deadline, Done, and Err to the stored context and keeping the no-context fallback.
Out of Scope Changes check ✅ Passed No clear out-of-scope code changes are visible beyond the context fix, docs, and tests.
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 4, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone Jun 4, 2026

Copilot AI 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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates Fiber’s Ctx context.Context behavior so Deadline(), Done(), and Err() delegate to a custom context set via SetContext, and aligns documentation and tests accordingly.

Changes:

  • DefaultCtx now forwards Deadline(), Done(), and Err() to the context stored by SetContext (otherwise remains a no-op).
  • Documentation updated to describe the custom-context delegation behavior.
  • Added a regression test verifying delegation to a custom context; updated linter suppression comment for os.Exit usage.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
log/default.go Adjusts revive suppression for intentional os.Exit(1) on fatal logs.
ctx.go Implements delegation of Deadline/Done/Err to a custom context stored in fasthttp user values.
ctx_test.go Adds a test ensuring context methods delegate when a custom context is set.
docs/guide/context.md Updates guide text and summary to reflect delegation behavior.
docs/api/ctx.md Updates API docs to describe SetContext delegation semantics.
Files not reviewed (1)
  • ctx_interface_gen.go: Language not supported

Comment thread log/default.go Outdated
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
os.Exit(1) //revive:disable-line:deep-exit // Fatal logs intentionally terminate the process.
Comment thread log/default.go Outdated
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
os.Exit(1) //revive:disable-line:deep-exit // Fatal logs intentionally terminate the process.
Comment thread log/default.go Outdated
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
os.Exit(1) //revive:disable-line:deep-exit // Fatal logs intentionally terminate the process.
Comment thread ctx.go
Comment on lines +157 to 164
func (c *DefaultCtx) Deadline() (time.Time, bool) {
if c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Deadline()
}
}
return time.Time{}, false
}
Comment thread ctx.go
Comment on lines +175 to 182
func (c *DefaultCtx) Done() <-chan struct{} {
if c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Done()
}
}
return nil
}
Comment thread ctx.go
Comment on lines +189 to 196
func (c *DefaultCtx) Err() error {
if c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Err() //nolint:wrapcheck // Err mirrors the stored context.
}
}
return nil
}

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates the Deadline, Done, and Err methods of DefaultCtx to delegate to a custom context if one has been set via SetContext, rather than operating as no-ops. It also updates the corresponding interface documentation, adds unit tests, and adjusts linter directives in the logger. The review feedback suggests optimizing these context methods by checking c.isUserContextSet first, which avoids the overhead of map lookups and type assertions for the majority of requests that do not use a custom context.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread ctx.go
Comment on lines +157 to 164
func (c *DefaultCtx) Deadline() (time.Time, bool) {
if c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Deadline()
}
}
return time.Time{}, false
}

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.

medium

Checking c.isUserContextSet before performing the UserValue lookup and type assertion is a great optimization. Since isUserContextSet is only set to true when a custom context is actually configured, we can completely bypass the overhead of the map/slice lookup and type assertion for the vast majority of requests that do not use a custom context.

Suggested change
func (c *DefaultCtx) Deadline() (time.Time, bool) {
if c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Deadline()
}
}
return time.Time{}, false
}
func (c *DefaultCtx) Deadline() (time.Time, bool) {
if c.isUserContextSet && c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Deadline()
}
}
return time.Time{}, false
}

Comment thread ctx.go
Comment on lines +175 to 182
func (c *DefaultCtx) Done() <-chan struct{} {
if c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Done()
}
}
return nil
}

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.

medium

Checking c.isUserContextSet before performing the UserValue lookup and type assertion is a great optimization. Since isUserContextSet is only set to true when a custom context is actually configured, we can completely bypass the overhead of the map/slice lookup and type assertion for the vast majority of requests that do not use a custom context.

Suggested change
func (c *DefaultCtx) Done() <-chan struct{} {
if c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Done()
}
}
return nil
}
func (c *DefaultCtx) Done() <-chan struct{} {
if c.isUserContextSet && c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Done()
}
}
return nil
}

Comment thread ctx.go
Comment on lines +189 to 196
func (c *DefaultCtx) Err() error {
if c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Err() //nolint:wrapcheck // Err mirrors the stored context.
}
}
return nil
}

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.

medium

Checking c.isUserContextSet before performing the UserValue lookup and type assertion is a great optimization. Since isUserContextSet is only set to true when a custom context is actually configured, we can completely bypass the overhead of the map/slice lookup and type assertion for the vast majority of requests that do not use a custom context.

Suggested change
func (c *DefaultCtx) Err() error {
if c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Err() //nolint:wrapcheck // Err mirrors the stored context.
}
}
return nil
}
func (c *DefaultCtx) Err() error {
if c.isUserContextSet && c.fasthttp != nil {
if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
return ctx.Err() //nolint:wrapcheck // Err mirrors the stored context.
}
}
return nil
}

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.38%. Comparing base (57cf8d2) to head (d442c96).
⚠️ Report is 51 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4399      +/-   ##
==========================================
- Coverage   91.40%   91.38%   -0.03%     
==========================================
  Files         132      132              
  Lines       13120    13170      +50     
==========================================
+ Hits        11992    12035      +43     
- Misses        711      719       +8     
+ Partials      417      416       -1     
Flag Coverage Δ
unittests 91.38% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gaby

gaby commented Jun 6, 2026

Copy link
Copy Markdown
Member

@ReneWerner87 If I remember correctly we left these as no-op same as fasthttp to avoid the performance hit?

@ReneWerner87

Copy link
Copy Markdown
Member

Will check in some hours,need to make a little bit research

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

4 participants