Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the go-systemd dependency to version v22.7.0 and replaces the deprecated sdconn.SubscribeUnitsCustom function with the context-aware sdconn.SubscribeUnitsCustomContext in mantle/cmd/kolet/kolet.go. The change correctly passes a context to the new function.
However, the introduction of a context-aware subscription function means the returned channels can now be closed upon context cancellation. The existing code does not handle this, which could lead to a busy-loop. I've added a comment with a suggestion to make the code more robust by handling channel closures.
The rest of the changes are in vendored files and are part of the dependency update. They include several improvements like adding context support across the API, refactoring with generics, and making data structures thread-safe, which are all positive changes.
| } | ||
| compareFunc := func(u1, u2 *systemddbus.UnitStatus) bool { return *u1 != *u2 } | ||
| unitevents, uniterrs := sdconn.SubscribeUnitsCustom(time.Second, 0, compareFunc, filterFunc) | ||
| unitevents, uniterrs := sdconn.SubscribeUnitsCustomContext(ctx, time.Second, 0, compareFunc, filterFunc) |
There was a problem hiding this comment.
While context.Background() is used here, which never gets canceled, switching to SubscribeUnitsCustomContext introduces the possibility for the returned unitevents and uniterrs channels to be closed if a cancellable context is used in the future.
The select loop starting on line 410 does not check if the channels are closed. Reading from a closed channel returns a zero value immediately without blocking. This would cause a busy-loop, consuming 100% CPU.
To make the code more robust and prevent this potential issue, you should check for channel closure in the select statement.
For example:
// in the for loop
select {
// ... other cases
case m, ok := <-unitevents:
if !ok {
systemdjournal.Print(systemdjournal.PriInfo, "unitevents channel closed")
return nil
}
// ... existing logic for unitevents
case err, ok := <-uniterrs:
if !ok {
systemdjournal.Print(systemdjournal.PriInfo, "uniterrs channel closed")
return nil
}
return err
// ... other cases
}|
/retest |
|
code LGTM, but the commit titles and messages need a little more (as requested similarly over in #4438 (comment)). The context in the description of the PR here would be good to add to the second commit commit message. |
c1068ee to
53fc7c4
Compare
|
Same comment as here #4438 (comment) |
53fc7c4 to
eb0cafe
Compare
|
Ah lets remove the '`' as it does some odd formatting from the perspective viewing it. otherwise lgtm i.e systemddbus: |
Update systemd-go library ``` got get github.com/coreos/go-systemd/v22/dbus@latest go mod vendor go mod tidy ``` And replace sdconn.SubscribeUnitsCustom with sdconn.SubscribeUnitsCustomContext in kolet.go This fixes the following error: SA1019: sdconn.SubscribeUnitsCustom is deprecated: use SubscribeUnitsCustomContext instead. (staticcheck)
eb0cafe to
8c83f8c
Compare
prestist
left a comment
There was a problem hiding this comment.
LGTM, thank you for working on this @angelcerveraroldan!
Update
kola.goto usesdconn.SubscribeUnitsCustomContextinstead ofsdconn.SubscribeUnitsCustomsince its deprecated.This should fix the following error in
golangci-lint:Closes #4436