Skip to content

Commit 36d61b4

Browse files
fix: connection leak from defer in polling loop
Extract pollOnce() helper to ensure resp.Body.Close() runs each iteration instead of deferring until function exit. Prevents ~150 leaked connections during 5-minute auth polling. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
1 parent 3ba1274 commit 36d61b4

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

internal/auth/login.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,28 +143,38 @@ func pollForApproval(apiBase, codeID string) (*cliPollResponse, error) {
143143
case <-timeout:
144144
return nil, fmt.Errorf("authentication timed out after 5 minutes")
145145
case <-ticker.C:
146-
resp, err := httpClient.Get(pollURL)
146+
result, done, err := pollOnce(pollURL)
147147
if err != nil {
148-
continue
148+
return nil, err
149149
}
150-
defer resp.Body.Close()
151-
152-
var result cliPollResponse
153-
decodeErr := json.NewDecoder(resp.Body).Decode(&result)
154-
if decodeErr != nil {
155-
continue
156-
}
157-
158-
if result.Status == "approved" {
159-
return &result, nil
160-
}
161-
if result.Status == "expired" {
162-
return nil, fmt.Errorf("authorization code expired or already used")
150+
if done {
151+
return result, nil
163152
}
164153
}
165154
}
166155
}
167156

157+
func pollOnce(pollURL string) (*cliPollResponse, bool, error) {
158+
resp, err := httpClient.Get(pollURL)
159+
if err != nil {
160+
return nil, false, nil
161+
}
162+
defer resp.Body.Close()
163+
164+
var result cliPollResponse
165+
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
166+
return nil, false, nil
167+
}
168+
169+
if result.Status == "approved" {
170+
return &result, true, nil
171+
}
172+
if result.Status == "expired" {
173+
return nil, false, fmt.Errorf("authorization code expired or already used")
174+
}
175+
return nil, false, nil
176+
}
177+
168178
func openBrowser(url string) error {
169179
return exec.Command("open", url).Start()
170180
}

0 commit comments

Comments
 (0)