| @@ -98,7 +98,7 @@ func TestMain(m *testing.M) { | |||||||
| 		if err := cmd.Process.Signal(syscall.SIGTERM); err != nil { | 		if err := cmd.Process.Signal(syscall.SIGTERM); err != nil { | ||||||
| 			fmt.Fprintln(os.Stderr, err) | 			fmt.Fprintln(os.Stderr, err) | ||||||
| 		} | 		} | ||||||
| 		if _, err := cmd.Process.Wait(); err != nil { | 		if err := cmd.Wait(); err != nil { | ||||||
| 			fmt.Fprintln(os.Stderr, err) | 			fmt.Fprintln(os.Stderr, err) | ||||||
| 		} | 		} | ||||||
| 		if err := os.RemoveAll(defaultRoot); err != nil { | 		if err := os.RemoveAll(defaultRoot); err != nil { | ||||||
|   | |||||||
							
								
								
									
										19
									
								
								io.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								io.go
									
									
									
									
									
								
							| @@ -1,6 +1,7 @@ | |||||||
| package containerd | package containerd | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| @@ -18,6 +19,13 @@ type IO struct { | |||||||
| 	closer *wgCloser | 	closer *wgCloser | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (i *IO) Cancel() { | ||||||
|  | 	if i.closer == nil { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	i.closer.Cancel() | ||||||
|  | } | ||||||
|  |  | ||||||
| func (i *IO) Wait() { | func (i *IO) Wait() { | ||||||
| 	if i.closer == nil { | 	if i.closer == nil { | ||||||
| 		return | 		return | ||||||
| @@ -134,9 +142,10 @@ type ioSet struct { | |||||||
| } | } | ||||||
|  |  | ||||||
| type wgCloser struct { | type wgCloser struct { | ||||||
| 	wg  *sync.WaitGroup | 	wg     *sync.WaitGroup | ||||||
| 	dir string | 	dir    string | ||||||
| 	set []io.Closer | 	set    []io.Closer | ||||||
|  | 	cancel context.CancelFunc | ||||||
| } | } | ||||||
|  |  | ||||||
| func (g *wgCloser) Wait() { | func (g *wgCloser) Wait() { | ||||||
| @@ -152,3 +161,7 @@ func (g *wgCloser) Close() error { | |||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (g *wgCloser) Cancel() { | ||||||
|  | 	g.cancel() | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										18
									
								
								io_unix.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								io_unix.go
									
									
									
									
									
								
							| @@ -13,16 +13,17 @@ import ( | |||||||
|  |  | ||||||
| func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) { | func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) { | ||||||
| 	var ( | 	var ( | ||||||
| 		f   io.ReadWriteCloser | 		f           io.ReadWriteCloser | ||||||
| 		set []io.Closer | 		set         []io.Closer | ||||||
| 		ctx = context.Background() | 		ctx, cancel = context.WithCancel(context.Background()) | ||||||
| 		wg  = &sync.WaitGroup{} | 		wg          = &sync.WaitGroup{} | ||||||
| 	) | 	) | ||||||
| 	defer func() { | 	defer func() { | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			for _, f := range set { | 			for _, f := range set { | ||||||
| 				f.Close() | 				f.Close() | ||||||
| 			} | 			} | ||||||
|  | 			cancel() | ||||||
| 		} | 		} | ||||||
| 	}() | 	}() | ||||||
|  |  | ||||||
| @@ -55,13 +56,14 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) { | |||||||
| 		wg.Add(1) | 		wg.Add(1) | ||||||
| 		go func(r io.ReadCloser) { | 		go func(r io.ReadCloser) { | ||||||
| 			io.Copy(ioset.err, r) | 			io.Copy(ioset.err, r) | ||||||
| 			wg.Done() |  | ||||||
| 			r.Close() | 			r.Close() | ||||||
|  | 			wg.Done() | ||||||
| 		}(f) | 		}(f) | ||||||
| 	} | 	} | ||||||
| 	return &wgCloser{ | 	return &wgCloser{ | ||||||
| 		wg:  wg, | 		wg:     wg, | ||||||
| 		dir: fifos.Dir, | 		dir:    fifos.Dir, | ||||||
| 		set: set, | 		set:    set, | ||||||
|  | 		cancel: cancel, | ||||||
| 	}, nil | 	}, nil | ||||||
| } | } | ||||||
|   | |||||||
| @@ -45,6 +45,8 @@ func (p *process) Start(ctx context.Context) error { | |||||||
| 	} | 	} | ||||||
| 	response, err := p.task.client.TaskService().Exec(ctx, request) | 	response, err := p.task.client.TaskService().Exec(ctx, request) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | 		p.io.Cancel() | ||||||
|  | 		p.io.Wait() | ||||||
| 		p.io.Close() | 		p.io.Close() | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Stephen Day
					Stephen Day