Skip to content

Thread context.Context through sources.Agent for proper cancellation #724

Closed

Description

@ShubhamRasal

Problem

All agents under sources/agent/* produce results on an unbuffered chan sources.Result, but the sources.Agent interface doesn't accept a context.Context:

// sources/agent.go
type Agent interface {
    Query(*Session, *Query) (chan Result, error)
    Name() string
}

The caller (Service.Execute) takes a ctx and uses it in the relay goroutine at uncover.go:138-144:

for {
    select {
    case <-ctx.Done():
        return
    case res, ok := <-source:
        // ...
        relay <- res
    }
}

When the ctx is cancelled, the relay exits — but the producing agent goroutine keeps running and will block on its next results <- ... send forever, since source is unbuffered and nobody is reading. Every agent leaks its goroutine on cancellation.

Why it's (mostly) invisible today

Most agents call a synchronous upstream API and finish in ~1 s, so users rarely Ctrl+C during a query. The leak is real but short-lived and often gets GC'd once the process exits.

Why it matters now

PR #723 (NerdyData) adds a 202-retry path with exponential backoff up to ~15.5 min (30+60+120+240+480s). During backoff the agent holds its goroutine open, and Ctrl+C does nothing — the process keeps sleeping and keeps hitting the NerdyData API even after the user has given up. That PR will ship with the select-on-context.Background() dead-code removed (matching the rest of the codebase), but the underlying limitation is what this issue is for.

Proposed fix

Add Ctx context.Context to sources.Query:

// sources/agent.go
type Query struct {
    Ctx   context.Context
    Query string
    Limit int
}

Callers construct Query with the Execute ctx:

// uncover.go:119
ch, err := agent.Query(s.Session, &sources.Query{
    Ctx:   ctx,
    Query: q,
    Limit: s.Options.Limit,
})

Agents wrap every results <- r with a select:

select {
case <-query.Ctx.Done():
    return
case results <- r:
}

Backward-compatible if Ctx == nil is treated as context.Background() inside each agent.

Scope

  • sources/agent.go — add Ctx field.
  • uncover.go — pass ctx at call site.
  • Each of the 17 agents under sources/agent/*/*.go — wrap channel sends. Mechanical change, ~3-5 lines per agent.
  • Ideally, upstream API calls that accept a ctx (SDK calls in censys, HTTP requests via retryablehttp.Request.WithContext) should also thread query.Ctx so network work aborts promptly, not just the channel send.

Out of scope

  • Buffering chan sources.Result. Orthogonal; unbuffered is fine if sends respect ctx.
  • Changing the Agent interface signature itself (stays backward-compatible by extending Query instead).

Related

Activity

  1. added
    Status: Available
    Type: Enhancement
    on Apr 24, 2026
  2. added a commit that references this issue on May 14, 2026
  3. added a commit that references this issue on May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Status: AvailableType: Enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions