-
Notifications
You must be signed in to change notification settings - Fork 668
Goroutine Leak ConnectVerfication() -> Limitter #1250
Description
When invoke ConnectVerfication() func in runner it creates new limiter on line 717
Lines 714 to 717 in 10f5605
| func (r *Runner) ConnectVerification() { | |
| r.scanner.ListenHandler.Phase.Set(scan.Scan) | |
| var swg sync.WaitGroup | |
| limiter := ratelimit.New(context.Background(), uint(r.options.Rate), time.Second) |
Creating new instances of Limiter starts new goroutine:
https://github.com/projectdiscovery/ratelimit/blob/77dad731f2e9a2e98564787086ade7be2ac33b4e/ratelimit.go#L112-L129
And it closed only if any of two contexts done https://github.com/projectdiscovery/ratelimit/blob/main/ratelimit.go#L39-L45
select {
case <-ctx.Done():
// Internal Context
imiter.ticker.Stop()
eturn
case <-limiter.ctx.Done():
limiter.ticker.Stop()
return
case ...
So this happens if original context done or if we call stop method https://github.com/projectdiscovery/ratelimit/blob/77dad731f2e9a2e98564787086ade7be2ac33b4e/ratelimit.go#L101-L106 that cancel second, created context https://github.com/projectdiscovery/ratelimit/blob/77dad731f2e9a2e98564787086ade7be2ac33b4e/ratelimit.go#L113
As we can see, when we invoke ConnectVerification() it creates new instance of limiter with context.Background() and not invoke Stop() later:
Lines 717 to 733 in 10f5605
| limiter := ratelimit.New(context.Background(), uint(r.options.Rate), time.Second) | |
| verifiedResult := result.NewResult() | |
| for hostResult := range r.scanner.ScanResults.GetIPsPorts() { | |
| limiter.Take() | |
| swg.Add(1) | |
| go func(hostResult *result.HostResult) { | |
| defer swg.Done() | |
| results := r.scanner.ConnectVerify(hostResult.IP, hostResult.Ports) | |
| verifiedResult.SetPorts(hostResult.IP, results) | |
| }(hostResult) | |
| } | |
| r.scanner.ScanResults = verifiedResult | |
| swg.Wait() |
So, we leak goroutine every ConnectVerification() call, what sometimes creates problems if we work with naabu in SDK mode
Thanks