-
Notifications
You must be signed in to change notification settings - Fork 57
fix: handle non-positive timeout and nil cancel in RequestTimeoutMiddleware #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,25 +161,26 @@ func RequestTimeoutMiddleware(timeout time.Duration) gin.HandlerFunc { | |
| var cancel context.CancelFunc | ||
| var ctx context.Context | ||
| if timeout <= 0 { | ||
| // Preserve the existing behavior for zero/negative values. | ||
| ctx, cancel = context.WithTimeout(c.Request.Context(), timeout) | ||
| // Zero/negative timeout means no timeout — use the existing context | ||
| // without wrapping it, so requests don't cancel immediately. | ||
| ctx = c.Request.Context() | ||
| cancel = func() {} // no-op to avoid nil cancel panic in defer | ||
|
Comment on lines
163
to
+167
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add regression tests for the new timeout branchesIssue #196 explicitly requires coverage for the current timeout tests still cover only the positive timeout, fasthandler, and panic cases That leaves the exact branches introduced here unprotected, so the immediate-504 regression or deadline-preservation behavior can change again without any failing test |
||
| } else { | ||
| if d, ok := c.Request.Context().Deadline(); ok { | ||
| desired := time.Now().Add(timeout) | ||
| // If an earlier deadline already exists, keep it. Otherwise set | ||
| // a new deadline at the desired point. | ||
| if d.Before(desired) { | ||
| ctx = c.Request.Context() | ||
| cancel = func() {} // no-op to avoid nil cancel panic in defer | ||
| } else { | ||
| ctx, cancel = context.WithDeadline(c.Request.Context(), desired) | ||
| } | ||
| } else { | ||
| ctx, cancel = context.WithTimeout(c.Request.Context(), timeout) | ||
| } | ||
| } | ||
| if cancel != nil { | ||
| defer cancel() | ||
| } | ||
| defer cancel() | ||
| c.Request = c.Request.WithContext(ctx) | ||
|
|
||
| origWriter := c.Writer | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: AnkanMisra/MicroAI-Paygate
Length of output: 276
🏁 Script executed:
Repository: AnkanMisra/MicroAI-Paygate
Length of output: 165
🏁 Script executed:
Repository: AnkanMisra/MicroAI-Paygate
Length of output: 5729
Add missing tests for
RequestTimeoutMiddlewaretimeout<=0 and earlier parent-deadline behaviorgateway/timeout_test.goonly tests the positive-timeout path (504) and AI timeout (~1s) plus provider context deadline; it has no coverage for thetimeout <= 0branch (zero/negative) or for “earlier parent deadline” vs middleware timeout behavior.gateway/redis_receipt_store_test.go/gateway/config_test.gocover receipt TTL/config fallback, not the middleware timeout logic.🤖 Prompt for AI Agents