Skip to content

Commit ddda2b2

Browse files
authored
Merge pull request #6462 from twz123/properly-stop-containerd
Properly stop containerd component when startup fails
2 parents a67e33d + 4f7e8cb commit ddda2b2

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

pkg/component/worker/containerd/component.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io"
1515
"os"
1616
"path/filepath"
17+
"sync"
1718
"syscall"
1819
"time"
1920

@@ -51,6 +52,8 @@ type Component struct {
5152
executablePath string
5253
confPath string
5354
importsPath string
55+
56+
stop func()
5457
}
5558

5659
func NewComponent(logLevel string, vars *config.CfgVars, profile *workerconfig.Profile) *Component {
@@ -88,7 +91,7 @@ func (c *Component) Init(ctx context.Context) error {
8891
}
8992

9093
// Run runs containerd.
91-
func (c *Component) Start(ctx context.Context) error {
94+
func (c *Component) Start(ctx context.Context) (err error) {
9295
log := logrus.WithField("component", "containerd")
9396
log.Info("Starting containerd")
9497

@@ -114,11 +117,32 @@ func (c *Component) Start(ctx context.Context) error {
114117
return err
115118
}
116119

117-
go c.watchDropinConfigs(ctx)
120+
cctx, cancel := context.WithCancelCause(context.Background())
121+
var wg sync.WaitGroup
122+
stop := func() {
123+
cancel(errors.New("containerd component is stopping"))
124+
c.supervisor.Stop()
125+
wg.Wait()
126+
}
127+
128+
defer func() {
129+
if err == nil {
130+
c.stop = stop
131+
} else {
132+
stop()
133+
}
134+
}()
135+
136+
wg.Add(1)
137+
go func() {
138+
defer wg.Done()
139+
wait.UntilWithContext(cctx, c.watchDropinConfigs, 30*time.Second)
140+
log.Info("Stopped to watch for drop-ins")
141+
}()
118142

119143
log.Debug("Waiting for containerd")
120144
var lastErr error
121-
err := wait.ExponentialBackoffWithContext(ctx, wait.Backoff{
145+
err = wait.ExponentialBackoffWithContext(ctx, wait.Backoff{
122146
Duration: 100 * time.Millisecond, Factor: 1.2, Jitter: 0.05, Steps: 30,
123147
}, func(ctx context.Context) (bool, error) {
124148
rt := containerruntime.NewContainerRuntime(Endpoint(c.K0sVars.RunDir))
@@ -276,8 +300,8 @@ func (c *Component) restart() {
276300

277301
// Stop stops containerd.
278302
func (c *Component) Stop() error {
279-
if c.supervisor != nil {
280-
c.supervisor.Stop()
303+
if stop := c.stop; stop != nil {
304+
stop()
281305
}
282306
return nil
283307
}

pkg/debounce/debounce.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (d *Debouncer[T]) Run(ctx context.Context) error {
4545
for pendingItem := (*T)(nil); ; {
4646
select {
4747
case <-ctx.Done():
48-
return ctx.Err() // ctx is done, good bye ...
48+
return context.Cause(ctx) // ctx is done, good bye ...
4949

5050
case item, ok := <-d.Input:
5151
if !ok {

0 commit comments

Comments
 (0)