Skip to content

fix: avoid chi route context reuse#1039

Closed
aqr91 wants to merge 1 commit into
danielgtaylor:mainfrom
aqr91:fix/autopatch-chi-route-context
Closed

fix: avoid chi route context reuse#1039
aqr91 wants to merge 1 commit into
danielgtaylor:mainfrom
aqr91:fix/autopatch-chi-route-context

Conversation

@aqr91

@aqr91 aqr91 commented Jun 2, 2026

Copy link
Copy Markdown

This fixes a regression introduced by creating autopatch internal GET/PUT requests with the incoming request context.

Using http.NewRequestWithContext(ctx.Context(), ...) preserves user context values, but with humachi it also preserves chi.RouteContext. When autopatch serves the internal GET/PUT through the same adapter/router, chi sees an existing route context and reuses stale routing state from the outer PATCH request. As a result, the internal GET can be routed back to the generated PATCH handler, causing recursion and eventually a panic when the nested request has no body.

Example failure:

PATCH /resource/{id}
autopatch -> internal GET /resource/{id}
chi reuses outer RouteContext with PATCH route state
internal GET hits PATCH handler again
io.ReadAll(nil) panic

Fix

Wrap the incoming context for autopatch internal requests and filter out chi.RouteCtxKey, while preserving all other context behavior and values.

This keeps the intent of #1019:

request-scoped context values are still available;
cancellation and deadlines are preserved;
router-specific state from chi is not leaked into nested internal requests.

Notes:

This is my first PR to a community-driven library, so I may have missed some project-specific code style or contribution conventions. I also did not have much time to review the code style deeply, so I am open to adjusting the implementation if needed.

I tested this fork in my project and confirmed that it fixes the autopatch recursion issue with humachi/chi. However, my original issue was not about preserving context values for the internal GET/PUT requests, so I cannot be 100% sure that this does not affect the behavior intended by the previous PR. The goal of this change is to preserve the incoming context while filtering out the chi routing state that causes the nested request to be routed incorrectly.

@codecov

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.07%. Comparing base (5e980c7) to head (91ad8cd).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
autopatch/autopatch.go 50.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1039      +/-   ##
==========================================
- Coverage   93.14%   93.07%   -0.08%     
==========================================
  Files          23       23              
  Lines        4901     4907       +6     
==========================================
+ Hits         4565     4567       +2     
- Misses        272      276       +4     
  Partials       64       64              

☔ 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.

@wolveix

wolveix commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Hey @aqr91! Thanks so much for taking the time to put this fix together :) Unfortunately, I want to avoid pulling chi into the autopatch package itself, as it's meant to stay router-agnostic, so importing go-chi/chi/v5 there couples a generic feature to one specific router and links chi into every autopatch user's binary, even folks on echo/gin/stdlib.

But your assessment was spot on. I've created a PR inspired by this which should address the underlying issue: #1049.

Thank you!

@wolveix wolveix closed this Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants