Skip to content

Commit e22ebcd

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 c10a85d commit e22ebcd

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

delete.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ import (
1010
"github.com/urfave/cli"
1111
)
1212

13-
func killAndDestroy(container *libcontainer.Container) error {
14-
if err := container.EnsureKilled(); err != nil {
15-
return err
16-
}
17-
return container.Destroy()
18-
}
19-
2013
var deleteCommand = cli.Command{
2114
Name: "delete",
2215
Usage: "delete any resources held by the container often used with detached container",
@@ -58,25 +51,27 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
5851
}
5952
return err
6053
}
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-
}
6954
s, err := container.Status()
7055
if err != nil {
7156
return err
7257
}
7358
switch s {
7459
case libcontainer.Stopped:
75-
return container.Destroy()
60+
// If the container is stopped, we can just destroy it.
7661
case libcontainer.Created:
77-
return killAndDestroy(container)
62+
if err := container.EnsureKilled(); err != nil {
63+
return err
64+
}
7865
default:
79-
return fmt.Errorf("cannot delete container %s that is not stopped: %s", id, s)
66+
if !force {
67+
return fmt.Errorf("cannot delete container %s that is not stopped: %s", id, s)
68+
}
69+
// When --force is given, we kill all container processes and
70+
// then destroy the container.
71+
if err := container.EnsureKilled(); err != nil {
72+
return err
73+
}
8074
}
75+
return container.Destroy()
8176
},
8277
}

libcontainer/container_linux.go

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

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

0 commit comments

Comments
 (0)