🚨 test: Fix race condition in TestTimeout_ContextPropagation#4119
🚨 test: Fix race condition in TestTimeout_ContextPropagation#4119adrian-lin-1-0-0 wants to merge 3 commits intogofiber:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on improving the stability and accuracy of a critical test case for the timeout middleware. It resolves a race condition that could lead to flaky test results and enhances the test's ability to verify correct context propagation under timeout conditions, ensuring the middleware behaves as expected. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Repository UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughUpdates TestTimeout_ContextPropagation to observe handler completion via a buffered error channel ( Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Code Review
This pull request addresses a race condition in the TestTimeout_ContextPropagation test by replacing an atomic.Bool with a buffered channel for proper synchronization between the test goroutine and the handler goroutine. The changes also improve the test by using time.NewTimer to prevent resource leaks and by strengthening the assertions to check for the specific context.DeadlineExceeded error. The fix is well-implemented and effectively resolves the race condition, making the test more robust and reliable. I have reviewed the changes and found no issues.
There was a problem hiding this comment.
Pull request overview
Improves reliability of the timeout middleware test suite by removing a race in TestTimeout_ContextPropagation and making the assertion depend on synchronized handler observation of context cancellation (relates to #3671).
Changes:
- Replace
atomic.Boolwith a bufferederrChto synchronize between the abandoned handler goroutine and the test. - Strengthen the test to assert the handler reports
context.DeadlineExceededand to fail fast if the handler never reports.
| timer := time.NewTimer(500 * time.Millisecond) | ||
| defer timer.Stop() | ||
|
|
||
| select { | ||
| case <-timer.C: | ||
| errCh <- nil | ||
| return c.SendString("completed") | ||
|
|
||
| case <-c.Context().Done(): | ||
| contextCanceled.Store(true) | ||
| errCh <- c.Context().Err() | ||
| return c.Context().Err() | ||
| case <-time.After(500 * time.Millisecond): | ||
| return c.SendString("completed") | ||
| } |
There was a problem hiding this comment.
The handler’s select can become nondeterministic if it isn’t scheduled until after both the 500ms timer has fired and c.Context().Done() is already closed; in that case Go may choose the timer branch and send nil, making this test flaky even though the context was canceled. To make the assertion deterministic, prefer a pattern that prioritizes the canceled context (e.g., check c.Context().Err()/Done() first in a non-blocking way before waiting on the timer), or make the timer duration so large that it can’t realistically become ready before the handler observes cancellation.
There was a problem hiding this comment.
The timer is 500ms while the timeout is 50ms — a 10x gap. For both cases to be ready simultaneously, the goroutine would need to go unscheduled for 500ms, which is not realistic in practice.
This is also the same select pattern used in other tests in this file (e.g. TestTimeout_PanicAfterTimeout, TestTimeout_ContextCleanup) and in the existing sleepWithContext helper. Changing only this test to a different pattern would be inconsistent.
Dismissing this as not actionable.
middleware/timeout/timeout_test.go
Outdated
| select { | ||
| case handlerErr := <-errCh: | ||
| require.ErrorIs(t, handlerErr, context.DeadlineExceeded, "handler should report DeadlineExceeded") | ||
|
|
||
| case <-time.After(1 * time.Second): |
There was a problem hiding this comment.
The select timeout uses time.After(1s). In repeated runs (e.g., -count=500) this leaves many pending timers until they fire, which can add avoidable allocations and noise. Prefer a time.NewTimer with defer Stop() (using timer.C in the select) so the timer can be stopped/drained when the handlerErr case wins.
| select { | |
| case handlerErr := <-errCh: | |
| require.ErrorIs(t, handlerErr, context.DeadlineExceeded, "handler should report DeadlineExceeded") | |
| case <-time.After(1 * time.Second): | |
| timer := time.NewTimer(1 * time.Second) | |
| defer func() { | |
| if !timer.Stop() { | |
| <-timer.C | |
| } | |
| }() | |
| select { | |
| case handlerErr := <-errCh: | |
| require.ErrorIs(t, handlerErr, context.DeadlineExceeded, "handler should report DeadlineExceeded") | |
| case <-timer.C: |
Description
Fix a potential race condition in
TestTimeout_ContextPropagationand strengthen its assertions. The original test usedatomic.Boolwhich could be read before the handler goroutine had written to it, since the timeout middleware returns immediately via the Abandon mechanism. This change replaces it with a buffered channel for proper synchronization.Relates to #3671
How to reproduce the race (before this fix)
go test -run TestTimeout_ContextPropagation -count=500 ./middleware/timeout/output
Changes introduced
List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.
Type of change
Checklist
/docs/directory for Fiber's documentation.Commit formatting
Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md