Repository navigation
fix(web): keep a share's rewritten page headers off the handler's goroutine - #102
Conversation
…outine While a share rewrites a page's Vite tags, the page goes out on the rewriting goroutine, and encode sends the headers with its first write. reverse_proxy still sets trailers on Header() from the handler's goroutine once the body is copied, so the two raced (TestServesAShare- ThroughCaddy failed under -race now and then). The handler now gets a copy of the headers while rewriting, and its trailers are passed on once the rewrite is done. Fixes flock-php#100 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@onairmarc this fixes #100 (the flaky |
There was a problem hiding this comment.
🟡 Changes recommended
Response rewriting drops predeclared trailer values because it only copies back prefixed trailers, not fields named in the Trailer declaration.
1 open issue
What changed in this PR
This PR addresses the response-header race reported in #100 by separating handler-side headers from those used by the share’s page-rewriting goroutine.
Changes:
- Clone headers when HTML rewriting begins to isolate subsequent handler access.
- Forward prefixed trailers after rewriting completes.
- Add a regression test for header isolation and trailer delivery using a writer that defers sending headers.
| File | Description |
|---|---|
internal/web/share.go |
Isolates headers during rewriting and forwards prefixed trailers afterward. |
internal/web/share_test.go |
Adds a deferred-header writer and a race-focused trailer regression test. |
Review effort: Balanced
reverse_proxy declares the upstream's trailers in Trailer before the headers go out and sets them by name after the body, without http.TrailerPrefix. Those stayed in the handler's copy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@onairmarc thanks for approving! Auto-merge is on and all checks pass, but it doesn't seem to have been added to the merge queue yet. Could you take a look when you have a moment? |
That's expected. I handle issue triage and reviews, but @lukas-frey handles all merging. Once he approves, it should merge. |
|
Got it, thanks for explaining! |
|
@lukas-frey friendly ping: this one is approved by @onairmarc, all checks pass and it's mergeable, so it's just waiting for your approval and merge. |
|
Thanks for digging into it :) Merging |

Fixes #100
What and why
TestServesAShareThroughCaddyfailed under-racenow and then, onmaintoo. The race is real, not just in the test.While a share rewrites a page's Vite tags, the page goes out on the rewriting goroutine, and
encodesends the headers with its first write. Once the body is copied,reverse_proxystill callsHeader()from the handler's goroutine to set the upstream's trailers (finalizeResponse). Those two calls touched the same response from two goroutines with nothing ordering them.What it does
rewriteWritergives the handler a copy of the headers. The response's own headers belong to the rewriting goroutine from then on.http.TrailerPrefix) are passed on once the rewrite is done, so they still go out.How I tested it
TestShareHandlerKeepsTheHandlersHeadersApartWhileRewritinguses a writer that sends the headers with its first write, asencodedoes, and a handler that sets a trailer after the page. Without the fix it reports the race every run. With the fix it passes, and the trailer arrives.TestServesAShareThroughCaddyunder-race:go test -race ./...andmake lintpass.🤖 Generated with Claude Code