Skip to content

fix(web): keep a share's rewritten page headers off the handler's goroutine - #102

Merged
lukas-frey merged 2 commits into
flock-php:mainfrom
omarabulkhair-66:fix/share-test-race
Nov 6, 2026
Merged

lukas-frey merged 2 commits into
flock-php:mainfrom
omarabulkhair-66:fix/share-test-race

Conversation

@omarabulkhair-66

Copy link
Copy Markdown
Contributor

Fixes #100

What and why

TestServesAShareThroughCaddy failed under -race now and then, on main too. 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 encode sends the headers with its first write. Once the body is copied, reverse_proxy still calls Header() 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

  • While a page is rewritten, rewriteWriter gives the handler a copy of the headers. The response's own headers belong to the rewriting goroutine from then on.
  • Trailers the handler sets on that copy (keys with http.TrailerPrefix) are passed on once the rewrite is done, so they still go out.

How I tested it

  • TestShareHandlerKeepsTheHandlersHeadersApartWhileRewriting uses a writer that sends the headers with its first write, as encode does, 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.
  • TestServesAShareThroughCaddy under -race:
    • Before the fix, it failed 1 in 400 runs (8 processes × 50).
    • After the fix, it passed 800 runs (8 × 100).
  • go test -race ./... and make lint pass.

🤖 Generated with Claude Code

…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]>
@omarabulkhair-66

Copy link
Copy Markdown
Contributor Author

@onairmarc this fixes #100 (the flaky TestServesAShareThroughCaddy race). Could you approve its workflow run when you get a chance? Thanks!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread internal/web/share.go Resolved
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
onairmarc enabled auto-merge November 4, 2026 14:16
@omarabulkhair-66

Copy link
Copy Markdown
Contributor Author

@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?

@onairmarc

Copy link
Copy Markdown
Member

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

@onairmarc
onairmarc requested a review from lukas-frey November 4, 2026 14:50
@omarabulkhair-66

Copy link
Copy Markdown
Contributor Author

Got it, thanks for explaining!

@omarabulkhair-66

Copy link
Copy Markdown
Contributor Author

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

@lukas-frey

Copy link
Copy Markdown
Member

Thanks for digging into it :) Merging

@lukas-frey
lukas-frey disabled auto-merge November 6, 2026 20:12
@lukas-frey
lukas-frey merged commit 14f30ce into flock-php:main Nov 6, 2026
16 checks passed
lukas-frey added a commit that referenced this pull request Nov 6, 2026
This was referenced Nov 6, 2026
@onairmarc onairmarc added this to the v1.1.0 milestone Nov 14, 2026 — with Copilot SWE Agent
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.

TestServesAShareThroughCaddy races under -race (also on main)

4 participants