Skip to content

Commit 31324e5

Browse files
authored
Merge pull request containerd#1181 from mlaventure/fix-races
Fix races
2 parents bf67906 + a5b3038 commit 31324e5

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestMain(m *testing.M) {
9898
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
9999
fmt.Fprintln(os.Stderr, err)
100100
}
101-
if _, err := cmd.Process.Wait(); err != nil {
101+
if err := cmd.Wait(); err != nil {
102102
fmt.Fprintln(os.Stderr, err)
103103
}
104104
if err := os.RemoveAll(defaultRoot); err != nil {

io.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package containerd
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"io/ioutil"
@@ -18,6 +19,13 @@ type IO struct {
1819
closer *wgCloser
1920
}
2021

22+
func (i *IO) Cancel() {
23+
if i.closer == nil {
24+
return
25+
}
26+
i.closer.Cancel()
27+
}
28+
2129
func (i *IO) Wait() {
2230
if i.closer == nil {
2331
return
@@ -134,9 +142,10 @@ type ioSet struct {
134142
}
135143

136144
type wgCloser struct {
137-
wg *sync.WaitGroup
138-
dir string
139-
set []io.Closer
145+
wg *sync.WaitGroup
146+
dir string
147+
set []io.Closer
148+
cancel context.CancelFunc
140149
}
141150

142151
func (g *wgCloser) Wait() {
@@ -152,3 +161,7 @@ func (g *wgCloser) Close() error {
152161
}
153162
return nil
154163
}
164+
165+
func (g *wgCloser) Cancel() {
166+
g.cancel()
167+
}

io_unix.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ import (
1313

1414
func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
1515
var (
16-
f io.ReadWriteCloser
17-
set []io.Closer
18-
ctx = context.Background()
19-
wg = &sync.WaitGroup{}
16+
f io.ReadWriteCloser
17+
set []io.Closer
18+
ctx, cancel = context.WithCancel(context.Background())
19+
wg = &sync.WaitGroup{}
2020
)
2121
defer func() {
2222
if err != nil {
2323
for _, f := range set {
2424
f.Close()
2525
}
26+
cancel()
2627
}
2728
}()
2829

@@ -55,13 +56,14 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
5556
wg.Add(1)
5657
go func(r io.ReadCloser) {
5758
io.Copy(ioset.err, r)
58-
wg.Done()
5959
r.Close()
60+
wg.Done()
6061
}(f)
6162
}
6263
return &wgCloser{
63-
wg: wg,
64-
dir: fifos.Dir,
65-
set: set,
64+
wg: wg,
65+
dir: fifos.Dir,
66+
set: set,
67+
cancel: cancel,
6668
}, nil
6769
}

process.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func (p *process) Start(ctx context.Context) error {
4545
}
4646
response, err := p.task.client.TaskService().Exec(ctx, request)
4747
if err != nil {
48+
p.io.Cancel()
49+
p.io.Wait()
4850
p.io.Close()
4951
return err
5052
}

0 commit comments

Comments
 (0)