Skip to content

cmd/vet: doesn't allow discarded cancel function on subcontexts #51160

Description

@bryndin

What version of Go are you using (go version)?

go version go1.16.5 darwin/amd64

What did you do?

go vet fails on discarded cancel function in child context, even though the context package states parent cancellation cancels all children.

Getting the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak

A very simplified example

func ParentCall(ctx context.Context) {
    c, cancel := context.WithCancel(ctx)
    Call(c)
    // wait for asyncs
    cancel()
}

func Call(ctx context.Context) {
    c, _ = context.WithDeadline(ctx, getDeadline())
    asyncCall(c)
    // can't call `cancel()` here since the `asyncCall` hasn't finished yet.
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions