Repository navigation
Description
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— addCtxfield.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 threadquery.Ctxso 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
Agentinterface signature itself (stays backward-compatible by extendingQueryinstead).
Related
- [Feature] NerdyData search engine #723 — first place the leak is user-visible because of long backoff.
Activity
Merge pull request #725 from projectdiscovery/724-thread-context-thro…
v1.2.1 (#734)
fix: thread ctx through sources.Agent (projectdiscovery#724)
fix: thread ctx through sources.Agent (projectdiscovery#724)
Merge pull request projectdiscovery#725 from projectdiscovery/724-thr…
v1.2.1 (projectdiscovery#734)