Description
The existing ctxbackground linter (pkg/linters/ctxbackground/) flags context.Background() calls inside functions that already receive a context.Context parameter, but it misses a common wrapper pattern: a function with no context.Context parameter whose body is just a call to an identically-named *Context sibling with a hardcoded context.Background() as the first argument, e.g.:
func Foo(args) T {
return FooContext(context.Background(), args)
}
This pattern caused the 92-call-site leakage found in pkg/workflow/github_cli.go (ExecGH, RunGH, RunGHCombined — see issue #48436) and likely recurs elsewhere. The linter currently has no rule for it.
Suggested Changes
- Add a new check (or extend the existing analyzer) in
pkg/linters/ctxbackground/ that detects:
- The function has no
context.Context parameter.
- Its body is a single return/expression statement calling a function named
<ThisFunctionName>Context.
- The first argument to that call is
context.Background().
- Flag matches with a message such as: "wrapper function synthesises context.Background(); consider adding a ctx parameter and delegating to the *Context variant".
- Add testdata covering positive (should flag) and negative (should not flag) cases.
- Register the analyzer/rule change in
pkg/linters/all.go if it introduces a new analyzer id.
Files Affected
pkg/linters/ctxbackground/ (analyzer + testdata)
pkg/linters/all.go (if new analyzer registered)
Success Criteria
- New/updated analyzer detects the wrapper-delegation pattern with both positive and negative testdata cases
make fmt and make test-unit (including pkg/linters/...) pass
Source
Extracted from Repository Quality Improvement Report — context.Background() Call-Chain Leakage (discussion #48389), Task 5.
Priority
Low - Prevents future recurrence of this leakage pattern; not blocking.
🔍 Task mining by Discussion Task Miner - Code Quality Improvement Agent · sonnet50 · 43.6 AIC · ⌖ 7.19 AIC · ⊞ 9.8K · ◷
Description
The existing
ctxbackgroundlinter (pkg/linters/ctxbackground/) flagscontext.Background()calls inside functions that already receive acontext.Contextparameter, but it misses a common wrapper pattern: a function with nocontext.Contextparameter whose body is just a call to an identically-named*Contextsibling with a hardcodedcontext.Background()as the first argument, e.g.:This pattern caused the 92-call-site leakage found in
pkg/workflow/github_cli.go(ExecGH,RunGH,RunGHCombined— see issue #48436) and likely recurs elsewhere. The linter currently has no rule for it.Suggested Changes
pkg/linters/ctxbackground/that detects:context.Contextparameter.<ThisFunctionName>Context.context.Background().pkg/linters/all.goif it introduces a new analyzer id.Files Affected
pkg/linters/ctxbackground/(analyzer + testdata)pkg/linters/all.go(if new analyzer registered)Success Criteria
make fmtandmake test-unit(includingpkg/linters/...) passSource
Extracted from Repository Quality Improvement Report — context.Background() Call-Chain Leakage (discussion #48389), Task 5.
Priority
Low - Prevents future recurrence of this leakage pattern; not blocking.