Merge pull request #130 from tonistiigi/wait-stdio

Wait for stdio to close before exiting
This commit is contained in:
Michael Crosby 2016-03-11 15:03:14 -08:00
commit dfad5da97a
2 changed files with 21 additions and 3 deletions

View file

@ -117,6 +117,7 @@ func main() {
} }
// runtime has exited so the shim can also exit // runtime has exited so the shim can also exit
if exitShim { if exitShim {
p.Wait()
return return
} }
} }

View file

@ -9,6 +9,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"sync"
"syscall" "syscall"
"github.com/docker/containerd/runtime" "github.com/docker/containerd/runtime"
@ -16,6 +17,7 @@ import (
) )
type process struct { type process struct {
sync.WaitGroup
id string id string
bundle string bundle string
stdio *stdio stdio *stdio
@ -131,7 +133,12 @@ func (p *process) start() error {
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL, Pdeathsig: syscall.SIGKILL,
} }
if err := cmd.Run(); err != nil { if err := cmd.Start(); err != nil {
return err
}
p.stdio.stdout.Close()
p.stdio.stderr.Close()
if err := cmd.Wait(); err != nil {
return err return err
} }
data, err := ioutil.ReadFile("pid") data, err := ioutil.ReadFile("pid")
@ -184,9 +191,11 @@ func (p *process) openIO() error {
if err != nil { if err != nil {
return err return err
} }
p.Add(1)
go func() { go func() {
io.Copy(stdout, console) io.Copy(stdout, console)
console.Close() console.Close()
p.Done()
}() }()
return nil return nil
} }
@ -201,10 +210,18 @@ func (p *process) openIO() error {
go io.Copy(i.Stdin, f) go io.Copy(i.Stdin, f)
}, },
p.state.Stdout: func(f *os.File) { p.state.Stdout: func(f *os.File) {
go io.Copy(f, i.Stdout) p.Add(1)
go func() {
io.Copy(f, i.Stdout)
p.Done()
}()
}, },
p.state.Stderr: func(f *os.File) { p.state.Stderr: func(f *os.File) {
go io.Copy(f, i.Stderr) p.Add(1)
go func() {
io.Copy(f, i.Stderr)
p.Done()
}()
}, },
} { } {
f, err := os.OpenFile(name, syscall.O_RDWR, 0) f, err := os.OpenFile(name, syscall.O_RDWR, 0)