Skip to content

Keep Transport dials from writing request state - #388

Merged
shubh-stripe merged 2 commits into
masterfrom
shubh/dial-races
Nov 25, 2026
Merged

shubh-stripe merged 2 commits into
masterfrom
shubh/dial-races

Conversation

@shubh-stripe

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

Copy link
Copy Markdown
Contributor

Fixes #383. The same fix for the v1 branch is in #387.

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.dialAttrs and the decision's ResolvedAddr and Reason, and it read the request logger, 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 attributes back through a locked value. The request keeps them only if the dial finished during RoundTrip. A CONNECT dial runs on the request goroutine, so it still writes sctx.dialAttrs 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 attributes appear only when the dial finished during RoundTrip.

A custom resolver still gets the live SmokescreenContext when a Transport dial resolves an address again. The resolver docs already say to treat it as read-only and to not keep it, so this PR leaves that as it is.

Test plan

I tested this with a race test that this PR leaves out. It 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 master without the fix, both cases report DATA RACE between the dialer's write to sctx.dialAttrs and the reads in diagnosticLogger and logProxy.
  • 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.dialAttrs and the decision's ResolvedAddr and Reason, and read the request logger, while the request goroutine still used them.

A Transport dial now starts from a copy of the request logger and hands its attributes back through a locked value. The request keeps them only if the dial finished during RoundTrip. A re-resolved address stays local to dialContext, so the logged decision reason stays the ACL reason, as it already was for CONNECT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Committed-By-Agent: claude
A CONNECT dial runs on the request goroutine, so writing the re-resolved address and reason to the decision never raced. Hooks that run after the dial, such as AcceptResponseHandler, read Decision.ResolvedAddr and expect the address that was dialed, for example an upstream proxy. Only a Transport dial keeps its result local now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Committed-By-Agent: claude
@shubh-stripe
shubh-stripe merged commit 2040499 into master Nov 25, 2026
21 checks passed
@shubh-stripe
shubh-stripe deleted the shubh/dial-races branch November 25, 2026 14:39
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.

Data race on SmokescreenContext.dialAttrs when a plain HTTP dial outlives its round trip

2 participants