Skip to content

Drain HTTP response bodies on non-OK paths to enable TCP connection reuse#22101

Merged
pelikhan merged 2 commits intomainfrom
copilot/add-drain-http-response-bodies
Mar 21, 2026
Merged

Drain HTTP response bodies on non-OK paths to enable TCP connection reuse#22101
pelikhan merged 2 commits intomainfrom
copilot/add-drain-http-response-bodies

Conversation

Copy link
Contributor

Copilot AI commented Mar 21, 2026

  • Add _, _ = io.Copy(io.Discard, resp.Body) before non-OK return in pkg/parser/remote_fetch.go
  • Add _, _ = io.Copy(io.Discard, resp.Body) before non-OK return in pkg/cli/agent_download.go
  • Add _, _ = io.Copy(io.Discard, resp.Body) before non-OK return in pkg/cli/deps_outdated.go
  • Add _, _ = io.Copy(io.Discard, resp.Body) before non-OK return in pkg/cli/deps_security.go
  • make fmt passes, code compiles cleanly

💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI changed the title [WIP] Add drain for HTTP response bodies on non-OK paths Drain HTTP response bodies on non-OK paths to enable TCP connection reuse Mar 21, 2026
Copilot AI requested a review from pelikhan March 21, 2026 05:32
@pelikhan pelikhan marked this pull request as ready for review March 21, 2026 06:01
Copilot AI review requested due to automatic review settings March 21, 2026 06:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR ensures HTTP response bodies are drained on non-200/OK paths so Go’s net/http transport can reuse TCP connections instead of discarding them.

Changes:

  • Drain resp.Body with io.Copy(io.Discard, resp.Body) before returning errors on non-OK HTTP responses in several call sites.
  • Applies this to raw GitHub content fetches and CLI dependency/security queries.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
pkg/parser/remote_fetch.go Drain raw URL response body before returning an error on non-OK status.
pkg/cli/agent_download.go Drain agent download response body on non-OK status (but currently misses an early-return path).
pkg/cli/deps_outdated.go Drain Go proxy response body before returning an error on non-OK status.
pkg/cli/deps_security.go Drain GitHub Advisory API response body before returning an error on non-OK status.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 55 to 71
if resp.StatusCode != http.StatusOK {
// Fall back to gh CLI for authenticated access (e.g., private repos in codespaces)
if resp.StatusCode == http.StatusNotFound && isGHCLIAvailable() {
agentDownloadLog.Print("Unauthenticated download returned 404, trying gh CLI for authenticated access")
if verbose {
fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Retrying download with gh CLI authentication..."))
}
if content, ghErr := downloadAgentFileViaGHCLI(ref); ghErr == nil {
patchedContent := patchAgentFileURLs(content, ref)
agentDownloadLog.Printf("Successfully downloaded agent file via gh CLI (%d bytes)", len(patchedContent))
return patchedContent, nil
} else {
agentDownloadLog.Printf("gh CLI fallback failed: %v", ghErr)
}
}
_, _ = io.Copy(io.Discard, resp.Body)
return "", fmt.Errorf("failed to download agent file: HTTP %d", resp.StatusCode)
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

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

In this non-OK branch, the response body is only drained right before the final error return. If the 404 + gh CLI fallback succeeds, the function returns early (line 65) and never executes the drain, so the original HTTP connection still won't be eligible for reuse. Consider draining (and optionally closing) the raw HTTP response body immediately after detecting resp.StatusCode != http.StatusOK, before any early returns in the fallback path.

Copilot uses AI. Check for mistakes.
@pelikhan pelikhan merged commit a0c0a81 into main Mar 21, 2026
139 checks passed
@pelikhan pelikhan deleted the copilot/add-drain-http-response-bodies branch March 21, 2026 06:06
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.

[plan] Drain HTTP response bodies on non-OK error paths to enable TCP connection reuse

3 participants