fix(update): close response body and file on error paths#3829
fix(update): close response body and file on error paths#3829nghiphaam wants to merge 2 commits intospicetify:mainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe update download flow now ensures explicit closure of the output file and HTTP response body on all error paths: after Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/cmd/update.go (1)
82-95:⚠️ Potential issue | 🟡 MinorSubsequent
utils.Fatalcalls still leakoutandresp2.Body.The same root cause addressed by this PR (deferred closes don't run because
utils.Fatalinvokesos.Exit(1)) applies to the error branches at lines 84, 87, and insidepermissionError(line 94). On those paths, both the deferredout.Close()(line 56) anddefer resp2.Body.Close()(line 72) are skipped, leaving the file and response body open until process termination.Consider closing both resources before invoking
utils.Fatal/permissionErroron these remaining branches to fully eliminate the leak class, e.g.:🛠️ Suggested adjustment
exe, err := os.Executable() if err != nil { + resp2.Body.Close() + out.Close() utils.Fatal(err) } if exe, err = filepath.EvalSymlinks(exe); err != nil { + resp2.Body.Close() + out.Close() utils.Fatal(err) }A cleaner alternative is to close both resources explicitly right after
io.Copysucceeds (and drop thedeferat line 72), so later error paths don't need to repeat the cleanup.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/cmd/update.go` around lines 82 - 95, The error paths in update.go call utils.Fatal or permissionError before deferred cleanup runs, leaking out and resp2.Body; modify the code in the update flow (references: variables out and resp2.Body, functions utils.Fatal and permissionError, and the io.Copy success point) to explicitly close both out.Close() and resp2.Body.Close() before any call to utils.Fatal or permissionError, or move the closes to occur immediately after the successful io.Copy and remove the deferred resp2.Body.Close()/out.Close() so later error branches no longer rely on defer for cleanup.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/cmd/update.go`:
- Around line 74-79: The io.Copy error branch leaks the output file handle
because out.Close() isn't called before utils.Fatal exits; update the error path
after io.Copy (where resp2.Body is closed and spinner.Fail is called) to also
explicitly call out.Close() prior to invoking utils.Fatal so the file descriptor
is released (referencing out, resp2.Body, io.Copy, spinner.Fail, and
utils.Fatal).
---
Outside diff comments:
In `@src/cmd/update.go`:
- Around line 82-95: The error paths in update.go call utils.Fatal or
permissionError before deferred cleanup runs, leaking out and resp2.Body; modify
the code in the update flow (references: variables out and resp2.Body, functions
utils.Fatal and permissionError, and the io.Copy success point) to explicitly
close both out.Close() and resp2.Body.Close() before any call to utils.Fatal or
permissionError, or move the closes to occur immediately after the successful
io.Copy and remove the deferred resp2.Body.Close()/out.Close() so later error
branches no longer rely on defer for cleanup.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 5ba24610-83b8-456a-ab72-f3b050d09c83
📒 Files selected for processing (1)
src/cmd/update.go
utils.Fatalcallsos.Exit(1), so deferred closes never run on error paths.Manually close
outandresp2.Bodybefore eachFatalcall.Move
defer resp2.Body.Close()to after all error checks pass.Fixes resource leak in
src/cmd/update.go.Summary by CodeRabbit