diff --git a/components/engine/libcontainerd/client_linux.go b/components/engine/libcontainerd/client_linux.go index 6c3460a8c9..95d597a2a5 100644 --- a/components/engine/libcontainerd/client_linux.go +++ b/components/engine/libcontainerd/client_linux.go @@ -117,6 +117,13 @@ func (clnt *client) AddProcess(ctx context.Context, containerID, processFriendly return -1, err } + // clean up fifos if failed to add process + defer func() { + if err != nil { + p.cleanFifos(processFriendlyName) + } + }() + resp, err := clnt.remote.apiClient.AddProcess(ctx, r) if err != nil { p.closeFifos(iopipe) diff --git a/components/engine/libcontainerd/container_unix.go b/components/engine/libcontainerd/container_unix.go index 9a7dbf01cd..63cb2aa5ee 100644 --- a/components/engine/libcontainerd/container_unix.go +++ b/components/engine/libcontainerd/container_unix.go @@ -69,11 +69,7 @@ func (ctr *container) clean() error { // Caller needs to lock container ID before calling this method. func (ctr *container) cleanProcess(id string) { if p, ok := ctr.processes[id]; ok { - for _, i := range []int{unix.Stdin, unix.Stdout, unix.Stderr} { - if err := os.Remove(p.fifo(i)); err != nil && !os.IsNotExist(err) { - logrus.Warnf("libcontainerd: failed to remove %v for process %v: %v", p.fifo(i), id, err) - } - } + p.cleanFifos(id) } delete(ctr.processes, id) } diff --git a/components/engine/libcontainerd/process_unix.go b/components/engine/libcontainerd/process_unix.go index 3b54e325b5..301c2b3468 100644 --- a/components/engine/libcontainerd/process_unix.go +++ b/components/engine/libcontainerd/process_unix.go @@ -9,8 +9,10 @@ import ( "path/filepath" goruntime "runtime" "strings" + "syscall" containerd "github.com/containerd/containerd/api/grpc/types" + "github.com/sirupsen/logrus" "github.com/tonistiigi/fifo" "golang.org/x/net/context" "golang.org/x/sys/unix" @@ -105,3 +107,12 @@ func (r emptyReader) Read(b []byte) (int, error) { func (p *process) fifo(index int) string { return filepath.Join(p.dir, p.friendlyName+"-"+fdNames[index]) } + +func (p *process) cleanFifos(id string) { + for _, i := range []int{syscall.Stdin, syscall.Stdout, syscall.Stderr} { + if err := os.Remove(p.fifo(i)); err != nil && !os.IsNotExist(err) { + logrus.Warnf("failed to remove %v for process %v: %v", p.fifo(i), id, err) + } + } + +}