fix(mux): use context.AfterFunc to prevent premature pool reclamation#1135
Open
jpalafox-12 wants to merge 1 commit into
Open
fix(mux): use context.AfterFunc to prevent premature pool reclamation#1135jpalafox-12 wants to merge 1 commit into
jpalafox-12 wants to merge 1 commit into
Conversation
jpalafox-12
marked this pull request as draft
July 17, 2026 01:11
When middleware clones the request context with r.WithContext(), the downstream handler can lose URL parameters because the sync.Pool reclaims chi.Context immediately after ServeHTTP returns. Use context.AfterFunc to defer pool reclamation until the parent request context is cancelled by the HTTP server. This ensures all goroutines holding cloned contexts have finished before the chi.Context is returned to the pool.
jpalafox-12
marked this pull request as ready for review
July 17, 2026 01:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes RoseMark45/chi#1
Fix
Uses
context.AfterFuncto defer pool reclamation until the parent request context is cancelled, preventing URL parameter loss when middleware clones the request context.Changes
mux.go: Replace immediatemx.pool.Put(rctx)withcontext.AfterFunc(r.Context(), ...)mux_test.go: 3 test cases (basic, concurrent, nested sub-routers)Verification
All existing tests pass. 3 new tests confirm the fix works.