SKY-G260 · Go-specific rules
Go unclosed resource
A file from os.Open or a database handle from sql.Open holds an operating-system resource until Close is called. SKY-G260 reports a function that opens one and neither closes it, returns it nor stores it somewhere else.
- Languages
- Go
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
Go's garbage collector does not close files promptly. A handler that leaks one file per request eventually fails with "too many open files", and a leaked sql.DB keeps its own pool of connections to the database.
What Skylos flags
package reports import ( "bufio" "os") func CountLines(path string) (int, error) { f, err := os.Open(path) if err != nil { return 0, err } lines := 0 scanner := bufio.NewScanner(f) for scanner.Scan() { lines++ } return lines, scanner.Err()}What Skylos leaves alone
package reports import "os" // OpenReport opens a report for the caller, who must close it.func OpenReport(path string) (*os.File, error) { f, err := os.Open(path) if err != nil { return nil, err } return f, nil}Why Skylos leaves it alone
The file is returned, so closing it is the caller's job. Skylos treats a returned handle, or one stored in a struct field, map or slice, as handed off.
How Skylos decides
- Runs with
--danger. The bundled Go engine,skylos-go, parses each.gofile with Go's own parser._test.gofiles, symbolic links and files undervendor,testdata,node_modulesand hidden folders are skipped. - In each function and function literal, finds variables assigned the result of
os.Open,os.OpenFileordatabase/sql'sOpen. - Treats the handle as handled when the same function calls
.Close()on it anywhere (deferred, direct or inside a deferred closure), returns it, alone or inside another value, or assigns it to a struct field or an index. - Reports MEDIUM on the line of the open call for each handle that is not handled. Whether
Closeruns on every path, or its error is checked, is not examined.
How to fix it
Close the handle with defer f.Close() right after the error check.
package reports import ( "bufio" "os") func CountLines(path string) (int, error) { f, err := os.Open(path) if err != nil { return 0, err }Added: defer f.Close() lines := 0 scanner := bufio.NewScanner(f) for scanner.Scan() { lines++ } return lines, scanner.Err()}When it can be wrong
- A handle passed to a helper that closes it (
process(f)) is reported, because onlyClose,returnand field or index assignment count. - It misses other resources, such as
net.Dialconnections andhttp.Responsebodies, and aClosethat only runs on one branch.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
At the end of the reported line:
Go f, err := os.Open(path) // skylos: ignore[SKY-G260] closed by the registry[tool.skylos] ignore = ["SKY-G260"]in apyproject.tomlat the project root turns the rule off. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.