Skip to content

Keep Transport dials from writing request state (v1 branch) - #387

Merged
shubh-stripe merged 1 commit into
v1from
shubh/v1-dial-races
Nov 25, 2026
Merged

shubh-stripe merged 1 commit into
v1from
shubh/v1-dial-races

Conversation

@shubh-stripe

@shubh-stripe shubh-stripe commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

This fixes #383 on the v1 branch. The same fix for master is in #388.

For plain HTTP requests and MITM inner requests, net/http's Transport dials on its own goroutine. It can finish the dial after RoundTrip returns, for example when the client cancels or when another pooled connection serves the request first. dialContext then wrote sctx.Logger and the decision's ResolvedAddr and Reason while the request goroutine still used them.

A Transport dial now starts from a copy of the request logger, taken before RoundTrip, and hands its log fields back through a locked value. The request keeps those fields only if the dial finished during RoundTrip. A CONNECT dial runs on the request goroutine, so it still writes sctx.Logger directly.

Two log changes come with this:

  • When a Transport dial resolves an address again, for example for an upper-case host, decision_reason keeps the ACL reason, and Decision.ResolvedAddr keeps the address from the ACL check. They used to change to the dialer's result. A CONNECT dial still records the re-resolved address and reason in the decision as before, for example the address of an upstream proxy, because it runs on the request goroutine.
  • conn_establish_time_ms and the outbound_*_addr fields appear only when the dial finished during RoundTrip.

Test plan

I tested this with a race test that this PR leaves out. It is in the branch history at 1b66786. The test cancels a plain HTTP request while net/http holds a dial for it. It holds the dial once inside the configured dialer and once before dialContext starts, and it orders the steps with channels instead of sleeps.

  • On v1 without the fix, both cases report DATA RACE between the dialer's write to sctx.Logger and rejectResponse.
  • With the fix, both cases pass 30 runs in a row under -race.

🤖 Generated with Claude Code

net/http's Transport dials on its own goroutine, and it can finish a dial after RoundTrip returns. That happens when the client cancels, or when another pooled connection serves the request first. dialContext then wrote sctx.Logger and the decision's ResolvedAddr and Reason while the request goroutine still used them, and the race detector reported it.

A Transport dial now hands its log fields back through a locked value, and the request keeps them only if the dial finished during RoundTrip. Its re-resolved address stays local. A CONNECT dial runs on the request goroutine, so it still writes the logger and records the re-resolved address and reason in the decision, which hooks such as AcceptResponseHandler read after the dial. Only tracked CONNECT and MITM connections build an enriched logger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shubh-stripe
shubh-stripe merged commit b464c02 into v1 Nov 25, 2026
29 checks passed
@shubh-stripe
shubh-stripe deleted the shubh/v1-dial-races branch November 25, 2026 14:56
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