-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Try to use pidfd and epoll to wait init process exit #4517
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -423,6 +423,78 @@ func (c *Container) signal(s os.Signal) error { | |
return nil | ||
} | ||
|
||
func (c *Container) killViaPidfd() error { | ||
pidfd, err := unix.PidfdOpen(c.initProcess.pid(), 0) | ||
if err != nil { | ||
return err | ||
} | ||
defer unix.Close(pidfd) | ||
|
||
epollfd, err := unix.EpollCreate1(unix.EPOLL_CLOEXEC) | ||
if err != nil { | ||
return err | ||
} | ||
defer unix.Close(epollfd) | ||
|
||
event := unix.EpollEvent{ | ||
Events: unix.EPOLLIN, | ||
Fd: int32(pidfd), | ||
} | ||
if err := unix.EpollCtl(epollfd, unix.EPOLL_CTL_ADD, pidfd, &event); err != nil { | ||
return err | ||
} | ||
|
||
// We don't need unix.PidfdSendSignal because go runtime will use it if possible. | ||
_ = c.Signal(unix.SIGKILL) | ||
|
||
events := make([]unix.EpollEvent, 1) | ||
for { | ||
// Set the timeout to 10s, the same as the traditional unix.Signal solution. | ||
n, err := unix.EpollWait(epollfd, events, 10000) | ||
if err != nil { | ||
if err == unix.EINTR { | ||
continue | ||
} | ||
return err | ||
} | ||
|
||
if n == 0 { | ||
return errors.New("container init still running") | ||
} | ||
|
||
if n > 0 { | ||
event := events[0] | ||
if event.Fd == int32(pidfd) { | ||
return nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *Container) kill() error { | ||
_ = c.Signal(unix.SIGKILL) | ||
for i := 0; i < 100; i++ { | ||
time.Sleep(100 * time.Millisecond) | ||
if err := c.Signal(unix.Signal(0)); err != nil { | ||
return nil | ||
} | ||
} | ||
return errors.New("container init still running") | ||
} | ||
|
||
// Kill kills the container and wait the init process exit. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and waits for the init process to exit. |
||
func (c *Container) Kill() error { | ||
if c.config.Namespaces.IsPrivate(configs.NEWPID) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might make sense to explain the reason for this "if". Something like "when the container doesn't have a private pidns, we have to kill every process in the cgroup, which killViaPidfd can't do". |
||
err := c.killViaPidfd() | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
logrus.Debugf("pidfd & epoll failed, falling back to unix.Signal: %v", err) | ||
} | ||
return c.kill() | ||
} | ||
|
||
func (c *Container) createExecFifo() (retErr error) { | ||
rootuid, err := c.Config().HostRootUID() | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... the same as in kill below