Skip to content

Enforce fileclosenotdeferred analyzer in CI — zero prod violations confirmed (#aw_sg21a2) #35390

Closed as not planned

Description

@github-actions

Summary

Sergo Run 21 audited the fileclosenotdeferred analyzer registered in cmd/linters/main.go:24. It is currently not in the production LINTER_FLAGS list in .github/workflows/cgo.yml:1040. A full audit of the 26 production os.Open / os.Create / os.OpenFile sites in pkg/ confirms zero prod violations — every open is followed by a defer ...Close() or defer func() { ... .Close() ... }() pattern.

This matches the #aw_sg20a1 pattern from R20 (osexitinlibrary + rawloginlib): a registered analyzer with zero violations qualifies for free enforcement by appending the flag to LINTER_FLAGS. No code changes are required.

Severity: Low (preventive — locks in current invariant)

No behavior change today, but blocks future regressions where a contributor opens a file and calls .Close() manually instead of defer file.Close() — which would leak the FD on early-return or panic.

Affected File

.github/workflows/cgo.yml:1040

Audit Evidence (R21)

26 prod os.Open / os.Create / os.OpenFile sites scanned, all conformant:

Category Count Pattern
defer file.Close() immediately after open 22 canonical
defer func() { if closeErr := f.Close(); ... }() (capture err) 3 defensible (workflows.go:312, workflows.go:412, logs_download.go:422)
Close in callee via defer out.Close() (CopyFile→copyFileContents) 1 linter-safe (no manual close in opener fn)
Manual non-deferred .Close() in prod 0 —

The analyzer specifically tracks os.Open/os.Create/os.OpenFile-returned vars and only fires when a manual .Close() exists without a paired defer ...Close() in the same function (see linter logic). Linter does not descend into nested FuncLit bodies, which is why the IIFE patterns above are linter-safe.

Recommendation

Append -fileclosenotdeferred to the existing LINTER_FLAGS string in .github/workflows/cgo.yml:1040:

-      run: make golint-custom LINTER_FLAGS="-errstringmatch -panicinlibrarycode -manualmutexunlock -osexitinlibrary -rawloginlib -test=false"
+      run: make golint-custom LINTER_FLAGS="-errstringmatch -panicinlibrarycode -manualmutexunlock -osexitinlibrary -rawloginlib -fileclosenotdeferred -test=false"

Validation

  • make golint-custom LINTER_FLAGS="-fileclosenotdeferred -test=false" reports zero issues locally
  • After PR merge, cgo.yml run is green on Run custom linters step
  • No new _test.go regression — analyzer skips test files via filecheck.IsTestFile

References

Generated by 🤖 Sergo - Serena Go Expert · opus47 24.3M · ◷

  • expires on Jun 4, 2026, 5:16 AM UTC

Activity

  1. added
    cookie
    on May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    code-qualitycookiesergo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions