Skip to content

Commit

Permalink
context with timeout
Browse files Browse the repository at this point in the history
Signed-off-by: dsxing <xingdashuaostr@126.com>
  • Loading branch information
“dsxing” authored and dsxing committed Sep 9, 2023
1 parent a0466dd commit 1486a45
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 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 Down Expand Up @@ -178,8 +180,10 @@ 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 {
Expand Down

0 comments on commit 1486a45

Please sign in to comment.