Drain HTTP response bodies on non-OK paths to enable TCP connection reuse#22101
Drain HTTP response bodies on non-OK paths to enable TCP connection reuse#22101
Conversation
…tion reuse Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> Agent-Logs-Url: https://github.com/github/gh-aw/sessions/649256a7-dc17-4d92-ad82-5e45595401eb
There was a problem hiding this comment.
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.Bodywithio.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.
| 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) |
There was a problem hiding this comment.
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.
_, _ = io.Copy(io.Discard, resp.Body)before non-OK return inpkg/parser/remote_fetch.go_, _ = io.Copy(io.Discard, resp.Body)before non-OK return inpkg/cli/agent_download.go_, _ = io.Copy(io.Discard, resp.Body)before non-OK return inpkg/cli/deps_outdated.go_, _ = io.Copy(io.Discard, resp.Body)before non-OK return inpkg/cli/deps_security.gomake fmtpasses, 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.