Skip to content

goroutinemissingrecover: go fn() where fn is a local closure variable is invisible to detection #71811

Done

Description

@github-actions

Linter: goroutinemissingrecover (pkg/linters/goroutinemissingrecover/goroutinemissingrecover.go), CI-enforced native+wasm (cgo.yml LINTER_FLAGS, both jobs).

Problem: The analyzer only flags a goroutine when go launches a literal function expression directly: call, ok := unwrapParens(goStmt.Call.Fun).(*ast.FuncLit) (line 54). By design, go f() where f is a named package-level function is out of scope, since that function can install its own recovery and be audited independently. But the same exclusion also silently covers a LOCAL variable holding an unnamed closure assigned immediately above the go statement, e.g.:

fn := func() {
    panic("oops")
}
go fn()

This is functionally identical to the inline go func() { panic("oops") }() form the linter exists to catch (a one-off, non-reusable closure with no independent recovery), but unwrapParens(goStmt.Call.Fun) resolves to an *ast.Ident, not an *ast.FuncLit, so it falls through unflagged with zero diagnostic.

Impact: A trivial refactor (extracting the inline closure to a local variable for readability, e.g. to pass it through a helper or name it for a comment) silently disables this CI-enforced panic-safety check with no warning. Same alias/indirection-blind pattern class already seen elsewhere in this codebase (reflectdeepequalusage function-value-alias gap, and the resourcetracker/goroutineresourceleak call-site-indirection family), now found in a fourth, independently-implemented linter.

Evidence: testdata/src/a/a.go only tests go namedFuncHelper() (a true package-level named function, correctly out of scope) in namedFuncGoroutine; there is no test case for go launching a local variable bound to a func literal. A live grep of pkg/ for go <identifier>(...) statement patterns found no current production occurrence of this exact shape, so this is a latent/forward-looking structural gap rather than a confirmed live false negative, same filing bar as several prior coverage-gap findings in this history (e.g. the coverage-package latent findings).

Recommendation: Before falling through on a non-FuncLit Fun, resolve a bare *ast.Ident via pass.TypesInfo.Uses/Defs to see if it refers to a local variable (not a *types.Func) whose declaration RHS is itself a *ast.FuncLit in the same function, and if so apply the same top-level-defer/recover check to that literal body. Alternatively, explicitly scope the docstring and exclusion to *types.Func-only targets and add a short comment clarifying the local-closure-variable case is intentionally out of scope, if the maintainers decide that tradeoff is acceptable.

Validation checklist:

  • Add fn := func() { panic(...) }(); go fn() test case with no recover, expect diagnostic
  • Add the same shape but with a top-level recover inside fn, expect no diagnostic
  • Existing namedFuncGoroutine (true package-level function) case stays out of scope, unchanged

Effort: Medium (requires threading TypesInfo-based variable-to-FuncLit resolution similar to the existing resolveFuncBody helper already present in the same file).

Generated by 🤖 Sergo - Serena Go Expert · claude · agent · ~203 AIC · ⌖ ~1 AIC · ⊞ 4.1K · ◷

  • expires on Oct 31, 2026, 8:03 PM UTC-08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

cookiesergo

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions