Skip to content

🐛 bug: delegate Ctx context methods to SetContext#4399

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

🐛 bug: delegate Ctx context methods to SetContext#4399
puneetdixit200 wants to merge 4 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: 130ff83f-2df6-487b-850d-513b5d35fc04

📥 Commits

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

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

Walkthrough

DefaultCtx now forwards Deadline, Done, and Err to a stored context set via SetContext, with no-op fallback when none is present. Interface comments, documentation, and tests reflect the updated behavior.

Changes

Context Delegation

Layer / File(s) Summary
Method delegation and test coverage
ctx.go, ctx_test.go
DefaultCtx.Deadline, Done, and Err delegate to a stored user context when present, and tests verify values from a canceled custom context.
Interface and guide docs
ctx_interface_gen.go, docs/api/ctx.md, docs/guide/context.md
Documentation describes delegation through SetContext and the no-op fallback when no context is set.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

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 | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The revive suppression cleanup is unrelated to issue #4335 and the context-method fix. Move the revive directive cleanup to a separate PR or justify it as required for this fix.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change to delegate Ctx context methods to SetContext.
Description check ✅ Passed The description includes a summary, issue link, and test plan, but omits the template's Changes introduced/type sections.
Linked Issues check ✅ Passed The changes implement the requested delegation of Deadline, Done, and Err to the SetContext context and add regression coverage.
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 93.22%. Comparing base (edf3969) to head (d3cb846).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4399      +/-   ##
==========================================
+ Coverage   93.17%   93.22%   +0.04%     
==========================================
  Files         140      140              
  Lines       14541    14550       +9     
==========================================
+ Hits        13549    13564      +15     
+ Misses        619      614       -5     
+ Partials      373      372       -1     
Flag Coverage Δ
unittests 93.22% <100.00%> (+0.04%) ⬆️

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