-
Notifications
You must be signed in to change notification settings - Fork 587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LocationResolver can leak goroutines #2487
Comments
We should also pass context down via the cataloger interface: https://github.com/anchore/syft/blob/main/syft/pkg/cataloger.go#L15. |
I have a question about the suggested implementation: Why are we making a separate iterator? Why no let func (r *Directory) AllLocations(ctx context.Context) <-chan file.Location {
results := make(chan file.Location)
go func() {
defer close(results)
for _, ref := range r.tree.AllFiles(stereoscopeFile.AllTypes()...) {
select {
case <-ctx.Done():
return
case results <- file.NewLocationFromDirectory(r.responsePath(string(ref.RealPath)), ref):
continue
}
}
}()
return results
} Testing this with func TestAllLocationsDoesNotLeakGoRoutine(t *testing.T) {
defer goleak.VerifyNone(t)
resolver, err := NewFromDirectory("./test-fixtures/symlinks-from-image-symlinks-fixture", "")
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
for _ = range resolver.AllLocations(ctx) {
break
}
cancel()
} This seems simpler than having a struct carry around the state of the iteration and implement a new interface. What do you think @wagoodman? |
I'm down with not needing to bring another interface along for the ride -- overall I think this looks good (and is a simpler change) 👍 |
Today the
LocationResolver
returns a channel of locations:However, while iterating though the channel if you return early you will leak a go routine. This should probably be updated to allow for cancellation via context and follow an iterator pattern (soft suggestion):
Where the caller would now be able to coordinate cancellation for correct usage:
Alternatively AllLocations can be a slice, which is simpler, but may cause memory pressures for images with many files:
The text was updated successfully, but these errors were encountered: