Skip to content
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

context with timeout #4007

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@
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 @@
return err
}

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

select {
case s := <-statusChan:
close(statusChan)
Expand All @@ -168,7 +167,7 @@
_ = 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,14 +177,14 @@

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()

Check failure on line 187 in libcontainer/cgroups/systemd/common.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
select {
case s := <-statusChan:
close(statusChan)
Expand All @@ -193,15 +192,15 @@
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