Skip to main content

SKY-G260 · Go-specific rules

Go unclosed resource

Medium

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

reports/lines.go · GoReported on line 9
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

reports/open.go · Go
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

  1. Runs with --danger. The bundled Go engine, skylos-go, parses each .go file with Go's own parser. _test.go files, symbolic links and files under vendor, testdata, node_modules and hidden folders are skipped.
  2. In each function and function literal, finds variables assigned the result of os.Open, os.OpenFile or database/sql's Open.
  3. 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.
  4. Reports MEDIUM on the line of the open call for each handle that is not handled. Whether Close runs 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.

reports/lines.go · Go · the fix, against the flagged file
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 only Close, return and field or index assignment count.
  • It misses other resources, such as net.Dial connections and http.Response bodies, and a Close that 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 a pyproject.toml at 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.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.