fix: resource leaks in file handles and HTTP response bodies
Description
This PR fixes critical resource leaks in 3 files where file handles and HTTP response bodies were not properly closed before returning errors or on function exit paths. Without proper cleanup, these resources remain open in memory indefinitely, which can lead to file descriptor exhaustion and memory leaks under sustained usage.
Fixed Resource Leaks
- internal/commands/job/artifact/logic.go:101: Added
defer dstFile.Close()immediately after successfully opening a file for artifact extraction. Previously, if any subsequent operation failed, the file handle would leak. -
internal/cmdutils/cmdutils.go:89: Added
defer f.Close()after opening a GitLab template file. The file handle was not closed on the normal execution path, causing a leak on every template load. -
internal/commands/api/api.go:559: Added
r.Close()before returning an error whenos.Stat()fails. The file was opened but never closed in this error path.
- trigger pipeline
- feat: resource leak fix in logic.go, cmdutils.go, api.go
Related Issues
Resolves #8052 (closed)
How has this been tested?
- Ran
make test- all existing unit tests pass - Ran
make lint- no new linting errors introduced - Built the project successfully with
make
Screenshots (if appropriate):
Edited by Tanmay Sharma