Skip to content

Commit df920a9

Browse files
abel-vonlifubang
authored andcommitted
libct: reduce the delete delay
When using unix.Kill to kill the container, we need a for loop to detect the init process exited or not manually, we sleep 100ms each time in the current, but for stopped containers or containers running in a low load machine, we don't need to wait so long time. This change will reduce the delete delay in some situations, especially for those pods with many containers in. Co-authored-by: Abel Feng <[email protected]> Signed-off-by: lifubang <[email protected]>
1 parent 591c739 commit df920a9

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

delete.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ import (
77
"path/filepath"
88

99
"github.com/opencontainers/runc/libcontainer"
10+
"github.com/opencontainers/runc/libcontainer/configs"
1011
"github.com/urfave/cli"
1112
)
1213

13-
func killAndDestroy(container *libcontainer.Container) error {
14-
if err := container.EnsureKilled(); err != nil {
15-
return err
16-
}
17-
return container.Destroy()
18-
}
19-
2014
var deleteCommand = cli.Command{
2115
Name: "delete",
2216
Usage: "delete any resources held by the container often used with detached container",
@@ -58,25 +52,34 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
5852
}
5953
return err
6054
}
61-
// When --force is given, we kill all container processes and
62-
// then destroy the container. This is done even for a stopped
63-
// container, because (in case it does not have its own PID
64-
// namespace) there may be some leftover processes in the
65-
// container's cgroup.
66-
if force {
67-
return killAndDestroy(container)
68-
}
6955
s, err := container.Status()
7056
if err != nil {
7157
return err
7258
}
7359
switch s {
7460
case libcontainer.Stopped:
75-
return container.Destroy()
61+
// For a stopped container, because (in case it does not have
62+
// its own PID namespace) there may be some leftover processes
63+
// in the container's cgroup.
64+
if !container.Config().Namespaces.IsPrivate(configs.NEWPID) {
65+
if err := container.EnsureKilled(); err != nil {
66+
return err
67+
}
68+
}
7669
case libcontainer.Created:
77-
return killAndDestroy(container)
70+
if err := container.EnsureKilled(); err != nil {
71+
return err
72+
}
7873
default:
79-
return fmt.Errorf("cannot delete container %s that is not stopped: %s", id, s)
74+
if !force {
75+
return fmt.Errorf("cannot delete container %s that is not stopped: %s", id, s)
76+
}
77+
// When --force is given, we kill all container processes and
78+
// then destroy the container.
79+
if err := container.EnsureKilled(); err != nil {
80+
return err
81+
}
8082
}
83+
return container.Destroy()
8184
},
8285
}

libcontainer/container_linux.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,15 @@ func (c *Container) killViaPidfd() error {
486486

487487
func (c *Container) kill() error {
488488
_ = c.Signal(unix.SIGKILL)
489+
490+
// For containers running in a low load machine, we only need to wait about 1ms.
491+
time.Sleep(time.Millisecond)
492+
if err := c.Signal(unix.Signal(0)); err != nil {
493+
return nil
494+
}
495+
496+
// For some containers in a heavy load machine, we need to wait more time.
497+
logrus.Debugln("We need more time to wait the init process exit.")
489498
for i := 0; i < 100; i++ {
490499
time.Sleep(100 * time.Millisecond)
491500
if err := c.Signal(unix.Signal(0)); err != nil {

0 commit comments

Comments
 (0)