diff --git a/libcontainerd/client_linux.go b/libcontainerd/client_linux.go index 657eda2c0d..190f981865 100644 --- a/libcontainerd/client_linux.go +++ b/libcontainerd/client_linux.go @@ -45,7 +45,7 @@ func (clnt *client) GetServerVersion(ctx context.Context) (*ServerVersion, error // AddProcess is the handler for adding a process to an already running // container. It's called through docker exec. It returns the system pid of the // exec'd process. -func (clnt *client) AddProcess(ctx context.Context, containerID, processFriendlyName string, specp Process, attachStdio StdioCallback) (int, error) { +func (clnt *client) AddProcess(ctx context.Context, containerID, processFriendlyName string, specp Process, attachStdio StdioCallback) (pid int, err error) { clnt.lock(containerID) defer clnt.unlock(containerID) container, err := clnt.getContainer(containerID) @@ -101,7 +101,14 @@ func (clnt *client) AddProcess(ctx context.Context, containerID, processFriendly Rlimits: convertRlimits(sp.Rlimits), } - iopipe, err := p.openFifos(sp.Terminal) + fifoCtx, cancel := context.WithCancel(context.Background()) + defer func() { + if err != nil { + cancel() + } + }() + + iopipe, err := p.openFifos(fifoCtx, sp.Terminal) if err != nil { return -1, err } @@ -335,7 +342,14 @@ func (clnt *client) restore(cont *containerd.Container, lastEvent *containerd.Ev } } - iopipe, err := container.openFifos(terminal) + fifoCtx, cancel := context.WithCancel(context.Background()) + defer func() { + if err != nil { + cancel() + } + }() + + iopipe, err := container.openFifos(fifoCtx, terminal) if err != nil { return err } diff --git a/libcontainerd/container_unix.go b/libcontainerd/container_unix.go index 1bd6050a72..61bab145f2 100644 --- a/libcontainerd/container_unix.go +++ b/libcontainerd/container_unix.go @@ -90,7 +90,7 @@ func (ctr *container) spec() (*specs.Spec, error) { return &spec, nil } -func (ctr *container) start(checkpoint string, checkpointDir string, attachStdio StdioCallback) error { +func (ctr *container) start(checkpoint string, checkpointDir string, attachStdio StdioCallback) (err error) { spec, err := ctr.spec() if err != nil { return nil @@ -100,7 +100,14 @@ func (ctr *container) start(checkpoint string, checkpointDir string, attachStdio defer cancel() ready := make(chan struct{}) - iopipe, err := ctr.openFifos(spec.Process.Terminal) + fifoCtx, cancel := context.WithCancel(context.Background()) + defer func() { + if err != nil { + cancel() + } + }() + + iopipe, err := ctr.openFifos(fifoCtx, spec.Process.Terminal) if err != nil { return err } diff --git a/libcontainerd/process_unix.go b/libcontainerd/process_unix.go index 514ebf643c..506fca6e11 100644 --- a/libcontainerd/process_unix.go +++ b/libcontainerd/process_unix.go @@ -9,7 +9,6 @@ import ( "path/filepath" goruntime "runtime" "strings" - "time" containerd "github.com/docker/containerd/api/grpc/types" "github.com/tonistiigi/fifo" @@ -31,13 +30,11 @@ type process struct { dir string } -func (p *process) openFifos(terminal bool) (pipe *IOPipe, err error) { +func (p *process) openFifos(ctx context.Context, terminal bool) (pipe *IOPipe, err error) { if err := os.MkdirAll(p.dir, 0700); err != nil { return nil, err } - ctx, _ := context.WithTimeout(context.Background(), 15*time.Second) - io := &IOPipe{} io.Stdin, err = fifo.OpenFifo(ctx, p.fifo(unix.Stdin), unix.O_WRONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)