Skip to content

[Code Quality] Extend ctxbackground linter to flag wrapper functions that synthesise context.Background() for a *Context sibling #48556

Description

@github-actions

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

  1. 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().
  2. Flag matches with a message such as: "wrapper function synthesises context.Background(); consider adding a ctx parameter and delegating to the *Context variant".
  3. Add testdata covering positive (should flag) and negative (should not flag) cases.
  4. 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 · ◷

  • expires on Jul 28, 2026, 11:41 PM UTC-08:00

Activity

  1. github-actions commented on Jul 29, 2026

    @github-actions
    ContributorAuthor

    This issue was automatically closed because it expired on 2026-07-29T07:41:37.787Z.

    Closed by Workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions