Skip to content

Simplify or fix the http.DefaultTransport clone fallback in registry NewTransport #32682

Closed

Description

@TerryHowe

Split out of #31386, where @ogulcanaydogan noted this as "the Go 1.20-era cloner fallback in pkg/registry/transport.go". It is unrelated to the oras-go workarounds that issue tracked, so filing it separately.

NewTransport in pkg/registry/transport.go:

func NewTransport(debug bool) *retry.Transport {
	// clone http.DefaultTransport so mutations (e.g. TLS config) are not
	// reflected globally
	transport := http.DefaultTransport
	if t, ok := transport.(*http.Transport); ok {
		transport = t.Clone()
	}
	...
}

The type assertion dates from when (*http.Transport).Clone was newer and the guard was worth having. The standard library documents http.DefaultTransport as a *http.Transport, so in practice the assertion always succeeds and the fallback branch never runs — we now require Go 1.26 (#32078), so there is no supported toolchain where this differs.

Worth being precise about what the fallback actually guards, though: http.DefaultTransport is a package-level variable of interface type, so a caller (or an imported package) can reassign it to some other RoundTripper. If that happened, today's code silently falls through and we would wrap and hand out the shared global without cloning it — which is exactly the aliasing the comment says we are trying to avoid. So the branch is not strictly dead; it is a silent-and-wrong path rather than an impossible one.

Two reasonable directions:

  1. Drop the assertion and clone http.DefaultTransport.(*http.Transport) directly, accepting a panic if someone has replaced it. Simplest, and fails loudly instead of silently sharing state.
  2. Keep a guard but make the fallback meaningful — construct a fresh &http.Transport{} (or log) rather than aliasing the global.

Either is a small change; mostly this needs a decision on which behavior we want. Flagging it because the current shape reads as dead code but is not quite.

Activity

  1. added
    bug
    on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions