Skip to content

Commit

Permalink
Merge pull request containerd#3462 from crosbymichael/ctr-hang
Browse files Browse the repository at this point in the history
Call CloseIO when stdin closes in ctr
  • Loading branch information
estesp authored Jul 26, 2019
2 parents e00ebfb + f543f2f commit eabb536
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
30 changes: 28 additions & 2 deletions cmd/ctr/commands/tasks/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package tasks

import (
"errors"
"io"
"net/url"
"os"

"github.com/containerd/console"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -91,7 +94,12 @@ var execCommand = cli.Command{
pspec.Terminal = tty
pspec.Args = args

var ioCreator cio.Creator
var (
ioCreator cio.Creator
stdinC = &stdinCloser{
stdin: os.Stdin,
}
)

if logURI := context.String("log-uri"); logURI != "" {
uri, err := url.Parse(logURI)
Expand All @@ -109,7 +117,7 @@ var execCommand = cli.Command{

ioCreator = cio.LogURI(uri)
} else {
cioOpts := []cio.Opt{cio.WithStdio, cio.WithFIFODir(context.String("fifo-dir"))}
cioOpts := []cio.Opt{cio.WithStreams(stdinC, os.Stdout, os.Stderr), cio.WithFIFODir(context.String("fifo-dir"))}
if tty {
cioOpts = append(cioOpts, cio.WithTerminal)
}
Expand All @@ -120,6 +128,9 @@ var execCommand = cli.Command{
if err != nil {
return err
}
stdinC.closer = func() {
process.CloseIO(ctx, containerd.WithStdinCloser)
}
// if detach, we should not call this defer
if !detach {
defer process.Delete(ctx)
Expand Down Expand Up @@ -166,3 +177,18 @@ var execCommand = cli.Command{
return nil
},
}

type stdinCloser struct {
stdin *os.File
closer func()
}

func (s *stdinCloser) Read(p []byte) (int, error) {
n, err := s.stdin.Read(p)
if err == io.EOF {
if s.closer != nil {
s.closer()
}
}
return n, err
}
14 changes: 12 additions & 2 deletions cmd/ctr/commands/tasks/tasks_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ func HandleConsoleResize(ctx gocontext.Context, task resizer, con console.Consol

// NewTask creates a new task
func NewTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, checkpoint string, con console.Console, nullIO bool, logURI string, ioOpts []cio.Opt, opts ...containerd.NewTaskOpts) (containerd.Task, error) {
stdio := cio.NewCreator(append([]cio.Opt{cio.WithStdio}, ioOpts...)...)
stdinC := &stdinCloser{
stdin: os.Stdin,
}
stdio := cio.NewCreator(append([]cio.Opt{cio.WithStreams(stdinC, os.Stdout, os.Stderr)}, ioOpts...)...)
if checkpoint != "" {
im, err := client.GetImage(ctx, checkpoint)
if err != nil {
Expand All @@ -94,7 +97,14 @@ func NewTask(ctx gocontext.Context, client *containerd.Client, container contain
}
ioCreator = cio.LogURI(u)
}
return container.NewTask(ctx, ioCreator, opts...)
t, err := container.NewTask(ctx, ioCreator, opts...)
if err != nil {
return nil, err
}
stdinC.closer = func() {
t.CloseIO(ctx, containerd.WithStdinCloser)
}
return t, nil
}

func getNewTaskOpts(context *cli.Context) []containerd.NewTaskOpts {
Expand Down

0 comments on commit eabb536

Please sign in to comment.