Fix deadlock when task's exec start fails

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2017-07-13 11:13:02 +02:00
parent 9dcf725b76
commit a5b3038ccc
No known key found for this signature in database
GPG Key ID: 40CF16616B361216
3 changed files with 27 additions and 11 deletions

19
io.go
View File

@ -1,6 +1,7 @@
package containerd
import (
"context"
"fmt"
"io"
"io/ioutil"
@ -18,6 +19,13 @@ type IO struct {
closer *wgCloser
}
func (i *IO) Cancel() {
if i.closer == nil {
return
}
i.closer.Cancel()
}
func (i *IO) Wait() {
if i.closer == nil {
return
@ -134,9 +142,10 @@ type ioSet struct {
}
type wgCloser struct {
wg *sync.WaitGroup
dir string
set []io.Closer
wg *sync.WaitGroup
dir string
set []io.Closer
cancel context.CancelFunc
}
func (g *wgCloser) Wait() {
@ -152,3 +161,7 @@ func (g *wgCloser) Close() error {
}
return nil
}
func (g *wgCloser) Cancel() {
g.cancel()
}

View File

@ -13,16 +13,17 @@ import (
func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
var (
f io.ReadWriteCloser
set []io.Closer
ctx = context.Background()
wg = &sync.WaitGroup{}
f io.ReadWriteCloser
set []io.Closer
ctx, cancel = context.WithCancel(context.Background())
wg = &sync.WaitGroup{}
)
defer func() {
if err != nil {
for _, f := range set {
f.Close()
}
cancel()
}
}()
@ -55,13 +56,14 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
wg.Add(1)
go func(r io.ReadCloser) {
io.Copy(ioset.err, r)
wg.Done()
r.Close()
wg.Done()
}(f)
}
return &wgCloser{
wg: wg,
dir: fifos.Dir,
set: set,
wg: wg,
dir: fifos.Dir,
set: set,
cancel: cancel,
}, nil
}

View File

@ -45,6 +45,7 @@ func (p *process) Start(ctx context.Context) error {
}
response, err := p.task.client.TaskService().Exec(ctx, request)
if err != nil {
p.io.Cancel()
p.io.Wait()
p.io.Close()
return err