Skip to content

Commit cf0270d

Browse files
committed
Tie to both contexts
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 5000997 commit cf0270d

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

core/http/middleware/request.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,16 @@ func (re *RequestExtractor) SetOpenAIRequest(ctx *fiber.Ctx) error {
162162
ctx.Set("X-Correlation-ID", correlationID)
163163

164164
//c1, cancel := context.WithCancel(re.applicationConfig.Context)
165+
// Use the application context as parent to ensure cancellation on app shutdown
166+
// We'll monitor the Fiber context separately and cancel our context when the request is canceled
165167
c1, cancel := context.WithCancel(ctx.Context())
168+
// Monitor the Fiber context and cancel our context when it's canceled
169+
// This ensures we respect request cancellation without causing panics
170+
go func() {
171+
<-ctx.Context().Done()
172+
// Fiber context was canceled (request completed or client disconnected)
173+
cancel()
174+
}()
166175
// Add the correlation ID to the new context
167176
ctxWithCorrelationID := context.WithValue(c1, CorrelationIDKey, correlationID)
168177

0 commit comments

Comments
 (0)