Skip to content

Commit

Permalink
fix: cancel all operations if c.V1Alpha2().Run fails
Browse files Browse the repository at this point in the history
The current code contains a deadlock if `c.V1Alpha2().Run` fails with an error because `EnforceKSPPRequirements`
will indefinitely wait for a condition to happen, but the controller runtime is not running.
This commit addresses this by replacing the error channel with `context.WithCancelCause` and
canceling the context if the controller runner fails.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Feb 4, 2024
1 parent d7d4154 commit 2036645
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions internal/app/machined/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/siderolabs/talos/internal/app/poweroff"
"github.com/siderolabs/talos/internal/app/trustd"
"github.com/siderolabs/talos/internal/app/wrapperd"
"github.com/siderolabs/talos/internal/pkg/ctxutil"
"github.com/siderolabs/talos/internal/pkg/mount"
"github.com/siderolabs/talos/pkg/httpdefaults"
"github.com/siderolabs/talos/pkg/machinery/api/common"
Expand Down Expand Up @@ -163,8 +164,6 @@ func runDebugServer(ctx context.Context) {

//nolint:gocyclo
func run() error {
errCh := make(chan error)

// Limit GOMAXPROCS.
startup.LimitMaxProcs(constants.MachinedMaxProcs)

Expand All @@ -179,8 +178,8 @@ func run() error {
return err
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx, cancel := context.WithCancelCause(context.Background())
defer cancel(nil)

drainer := runtime.NewDrainer()
defer func() {
Expand All @@ -207,7 +206,7 @@ func run() error {
// Start v2 controller runtime.
go func() {
if e := c.V1Alpha2().Run(ctx, drainer); e != nil {
errCh <- fmt.Errorf("fatal controller runtime error: %s", e)
cancel(fmt.Errorf("fatal controller runtime error: %s", e))
}

log.Printf("controller runtime finished")
Expand Down Expand Up @@ -265,21 +264,21 @@ func run() error {
continue
}

errCh <- fmt.Errorf(
cancel(fmt.Errorf(
"fatal sequencer error in %q sequence: %v",
msg.GetSequence(),
msg.GetError().String(),
)
))
}
case *machine.RestartEvent:
errCh <- runtime.RebootError{Cmd: int(msg.Cmd)}
cancel(runtime.RebootError{Cmd: int(msg.Cmd)})
}
}
}
},
)

return <-errCh
return ctxutil.Cause(ctx)
}

func main() {
Expand Down

0 comments on commit 2036645

Please sign in to comment.