Potential Resource Leak

ID

go.potential_resource_leak

Severity

low

Remediation Complexity

trivial

Remediation Risk

medium

Remediation Effort

low

Resource

Reliability

Language

Go

Tags

reliability, resource-leak

Description

Reports a file opened with os.Open or os.Create and stored in a variable that is never closed within the same function.

Rationale

An open file held until garbage collection leaks a file descriptor; under load this exhausts the process’s descriptor limit. The canonical Go idiom is to defer f.Close() immediately after a successful open so the file is released however the function returns.

f, _ := os.Open("a") // FLAW — f is never closed
_ = f

f, _ := os.Open("a") // OK
defer f.Close()

Remediation

Add defer f.Close() right after the open (after checking the error), or call f.Close() explicitly once the file is no longer needed.