checkers

package
v0.7.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package checkers provides individual checker implementations for goroutinectx.

Checker Overview

This package contains implementations for detecting context propagation issues:

┌─────────────────────────────────────────────────────────────────────┐
│                         Checker Types                               │
├──────────────────────┬──────────────────────────────────────────────┤
│ GoStmtChecker        │ Checks go statements                         │
│  - Goroutine         │ go func() { ... }() without ctx              │
│  - GoroutineDerive   │ go func() { ... }() without deriver call     │
├──────────────────────┼──────────────────────────────────────────────┤
│ CallChecker          │ Checks function call expressions             │
│  - CallArgChecker    │ Generic callback argument checker            │
│    - Errgroup        │ errgroup.Group.Go() callbacks                │
│    - Waitgroup       │ sync.WaitGroup.Go() callbacks (Go 1.25+)     │
│    - Conc            │ github.com/sourcegraph/conc callbacks        │
│  - SpawnerChecker    │ //goroutinectx:spawner marked functions      │
│  - GotaskChecker     │ gotask library functions                     │
└──────────────────────┴──────────────────────────────────────────────┘

GoStmtChecker

Checks for go statements that don't properly propagate context:

// Bad - context not captured
func worker(ctx context.Context) {
    go func() {           // <- Warning: goroutine does not propagate context
        doWork()
    }()
}

// Good - context captured
func worker(ctx context.Context) {
    go func() {
        doWork(ctx)       // <- OK: context used
    }()
}

CallArgChecker

Factory functions create checkers for specific APIs:

checker := NewErrgroupChecker(deriveMatcher)
checker := NewWaitgroupChecker(deriveMatcher)
checker := NewConcChecker(deriveMatcher)

Example detection:

func worker(ctx context.Context) {
    g, _ := errgroup.WithContext(ctx)
    g.Go(func() error {   // <- Warning: closure should use context
        return doWork()
    })
}

GoroutineDerive

When configured with -goroutine-deriver flag, checks that goroutines call a specific context derivation function:

// With -goroutine-deriver=apm.NewGoroutineContext
func worker(ctx context.Context) {
    go func() {
        ctx := apm.NewGoroutineContext(ctx)  // <- Required
        doWork(ctx)
    }()
}

Gotask Checker

Checks gotask library usage for proper context derivation:

// gotask.Do* functions - task arguments must call deriver
gotask.DoAll(ctx,
    gotask.NewTask(func(ctx context.Context) error {
        ctx = apm.NewGoroutineContext(ctx)  // <- Required in task body
        return doWork(ctx)
    }),
)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Goroutine added in v0.7.2

type Goroutine struct{}

Goroutine checks that go statements propagate context.

func (*Goroutine) CheckGoStmt added in v0.7.2

func (c *Goroutine) CheckGoStmt(cctx *probe.Context, stmt *ast.GoStmt) *internal.Result

CheckGoStmt checks a go statement for context propagation.

func (*Goroutine) Name added in v0.7.2

func (*Goroutine) Name() ignore.CheckerName

Name returns the checker name for ignore directive matching.

type GoroutineDerive added in v0.7.2

type GoroutineDerive struct {
	// contains filtered or unexported fields
}

GoroutineDerive checks that go statements call a deriver function.

func NewGoroutineDerive added in v0.7.4

func NewGoroutineDerive(derivers *deriver.Matcher) *GoroutineDerive

NewGoroutineDerive creates a new GoroutineDerive checker.

func (*GoroutineDerive) CheckGoStmt added in v0.7.2

func (c *GoroutineDerive) CheckGoStmt(cctx *probe.Context, stmt *ast.GoStmt) *internal.Result

CheckGoStmt checks a go statement for deriver function calls.

func (*GoroutineDerive) Name added in v0.7.2

Name returns the checker name for ignore directive matching.

type GotaskChecker added in v0.7.2

type GotaskChecker struct {
	// contains filtered or unexported fields
}

GotaskChecker checks gotask library API calls.

func NewGotaskChecker added in v0.7.2

func NewGotaskChecker(derivers *deriver.Matcher) *GotaskChecker

NewGotaskChecker creates a gotask checker.

func (*GotaskChecker) CheckCall added in v0.7.2

func (c *GotaskChecker) CheckCall(cctx *probe.Context, call *ast.CallExpr) *internal.Result

CheckCall checks the call expression. Note: This checker may report multiple diagnostics directly to pass.

func (*GotaskChecker) MatchCall added in v0.7.2

func (c *GotaskChecker) MatchCall(pass *analysis.Pass, call *ast.CallExpr) bool

MatchCall returns true if this checker should handle the call.

func (*GotaskChecker) Name added in v0.7.2

Name returns the checker name.

type SpawnCallbackChecker added in v0.7.4

type SpawnCallbackChecker struct {
	// contains filtered or unexported fields
}

SpawnCallbackChecker checks function calls that take callbacks spawned as goroutines.

func NewConcChecker added in v0.7.2

func NewConcChecker(derivers *deriver.Matcher) *SpawnCallbackChecker

NewConcChecker creates the conc checker.

func NewErrgroupChecker added in v0.7.2

func NewErrgroupChecker(derivers *deriver.Matcher) *SpawnCallbackChecker

NewErrgroupChecker creates the errgroup checker.

func NewSpawnCallbackChecker added in v0.7.4

func NewSpawnCallbackChecker(name ignore.CheckerName, entries []SpawnCallbackEntry, derivers *deriver.Matcher) *SpawnCallbackChecker

NewSpawnCallbackChecker creates a new SpawnCallbackChecker.

func NewWaitgroupChecker added in v0.7.2

func NewWaitgroupChecker(derivers *deriver.Matcher) *SpawnCallbackChecker

NewWaitgroupChecker creates the waitgroup checker (Go 1.25+).

func (*SpawnCallbackChecker) CheckCall added in v0.7.4

func (c *SpawnCallbackChecker) CheckCall(cctx *probe.Context, call *ast.CallExpr) *internal.Result

CheckCall checks the call expression.

func (*SpawnCallbackChecker) MatchCall added in v0.7.4

func (c *SpawnCallbackChecker) MatchCall(pass *analysis.Pass, call *ast.CallExpr) bool

MatchCall returns true if this checker should handle the call.

func (*SpawnCallbackChecker) Name added in v0.7.4

Name returns the checker name for ignore directive matching.

type SpawnCallbackEntry added in v0.7.4

type SpawnCallbackEntry struct {
	Spec           funcspec.Spec
	CallbackArgIdx int
}

SpawnCallbackEntry defines a function that spawns its callback argument as a goroutine.

type SpawnerChecker added in v0.7.2

type SpawnerChecker struct {
	// contains filtered or unexported fields
}

SpawnerChecker checks calls to spawner-marked functions.

func NewSpawnerChecker added in v0.7.2

func NewSpawnerChecker(spawners SpawnerMap, derivers *deriver.Matcher) *SpawnerChecker

NewSpawnerChecker creates a spawner checker.

func (*SpawnerChecker) CheckCall added in v0.7.2

func (c *SpawnerChecker) CheckCall(cctx *probe.Context, call *ast.CallExpr) *internal.Result

CheckCall checks the call expression. Note: This checker reports directly to pass because it may have multiple failing arguments.

func (*SpawnerChecker) MatchCall added in v0.7.2

func (c *SpawnerChecker) MatchCall(pass *analysis.Pass, call *ast.CallExpr) bool

MatchCall returns true if this checker should handle the call.

func (*SpawnerChecker) Name added in v0.7.2

Name returns the checker name.

type SpawnerMap added in v0.7.2

type SpawnerMap interface {
	IsSpawner(fn *types.Func) bool
}

SpawnerMap interface for checking if a function is a spawner.

Directories

Path Synopsis
Package spawnerlabel provides spawner label directive validation.
Package spawnerlabel provides spawner label directive validation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL