Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions internal/oci/runtime_oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,13 @@ func (r *runtimeOCI) ExecSyncContainer(ctx context.Context, c *Container, comman
}
defer os.RemoveAll(processFile)

pidFile, err := createPidFile()
pidDir, err := ioutil.TempDir("", "pidfile")
if err != nil {
return nil, err
}
defer os.RemoveAll(pidFile)
defer os.RemoveAll(pidDir)

pidFile := filepath.Join(pidDir, c.id)

cmd := r.constructExecCommand(ctx, c, processFile, pidFile)
cmd.SysProcAttr = sysProcAttrPlatform()
Expand All @@ -356,7 +358,7 @@ func (r *runtimeOCI) ExecSyncContainer(ctx context.Context, c *Container, comman
select {
case <-time.After(time.Second * time.Duration(timeout)):
// Ensure the process is not left behind
killContainerExecProcess(ctx, pidFile)
killContainerExecProcess(ctx, pidFile, cmd)

// Make sure the runtime process has been cleaned up
<-done
Expand Down Expand Up @@ -414,13 +416,15 @@ func createPidFile() (string, error) {
return pidFileName, nil
}

func killContainerExecProcess(ctx context.Context, pidFile string) {
func killContainerExecProcess(ctx context.Context, pidFile string, cmd *exec.Cmd) {
// Attempt to get the container PID and PGID from the file the runtime should have written.
// TODO(haircommander): There does exist a race that we could time out before the runtime actually creates the file.
// Should we do inotify on this file to ensure it exists? Is that overkill?
ctrPid, ctrPgid, err := pidAndpgidFromFile(pidFile)
if err != nil {
log.Errorf(ctx, "Failed to get pid (%d) or pgid (%d) from file %s: %v", ctrPid, ctrPgid, pidFile, err)
if err != nil && ctrPid <= 0 {
// only kill the runtime process if we failed to find a ctrPid
// as this means the runtime exec hasn't successfully written the pid file
if killErr := cmd.Process.Kill(); killErr != nil {
log.Errorf(ctx, "Error killing runtime exec process(%v) after error finding runtime pid: (%v)", killErr, err)
}
}

if ctrPgid > 1 {
Expand Down