Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions libcontainer/cgroups/systemd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ func startUnit(cm *dbusConnManager, unitName string, properties []systemdDbus.Pr
retry := true

retry:
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := cm.retryOnDisconnect(func(c *systemdDbus.Conn) error {
_, err := c.StartTransientUnitContext(context.TODO(), unitName, "replace", properties, statusChan)
_, err := c.StartTransientUnitContext(ctx, unitName, "replace", properties, statusChan)
return err
})
if err != nil {
Expand All @@ -157,9 +159,6 @@ retry:
return err
}

timeout := time.NewTimer(30 * time.Second)
defer timeout.Stop()

select {
case s := <-statusChan:
close(statusChan)
Expand All @@ -168,7 +167,7 @@ retry:
_ = resetFailedUnit(cm, unitName)
return fmt.Errorf("error creating systemd unit `%s`: got `%s`", unitName, s)
}
case <-timeout.C:
case <-ctx.Done():
_ = resetFailedUnit(cm, unitName)
return errors.New("Timeout waiting for systemd to create " + unitName)
}
Expand All @@ -178,13 +177,13 @@ retry:

func stopUnit(cm *dbusConnManager, unitName string) error {
statusChan := make(chan string, 1)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := cm.retryOnDisconnect(func(c *systemdDbus.Conn) error {
_, err := c.StopUnitContext(context.TODO(), unitName, "replace", statusChan)
_, err := c.StopUnitContext(ctx, unitName, "replace", statusChan)
return err
})
if err == nil {
timeout := time.NewTimer(30 * time.Second)
defer timeout.Stop()

select {
case s := <-statusChan:
Expand All @@ -193,15 +192,15 @@ func stopUnit(cm *dbusConnManager, unitName string) error {
if s != "done" {
logrus.Warnf("error removing unit `%s`: got `%s`. Continuing...", unitName, s)
}
case <-timeout.C:
case <-ctx.Done():
return errors.New("Timed out while waiting for systemd to remove " + unitName)
}
}

// In case of a failed unit, let systemd remove it.
_ = resetFailedUnit(cm, unitName)

return nil
return err
}

func resetFailedUnit(cm *dbusConnManager, name string) error {
Expand Down