Wait for client side copy goroutines to start
Make sure we wait for the client side copy goroutines to start coping
from the fifos before returning from the function.
@@ -36,6 +36,7 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
var (
f io.ReadWriteCloser
set []io.Closer
+ cwg sync.WaitGroup
ctx, cancel = context.WithCancel(context.Background())
wg = &sync.WaitGroup{}
)
@@ -52,9 +53,13 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
return nil, err
}
set = append(set, f)
+ cwg.Add(1)
+ wg.Add(1)
go func(w io.WriteCloser) {
+ cwg.Done()
io.Copy(w, ioset.in)
w.Close()
+ wg.Done()
}(f)
if f, err = fifo.OpenFifo(ctx, fifos.Out, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700); err != nil {
@@ -62,7 +67,9 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
}
set = append(set, f)
wg.Add(1)
+ cwg.Add(1)
go func(r io.ReadCloser) {
+ cwg.Done()
io.Copy(ioset.out, r)
r.Close()
wg.Done()
@@ -75,12 +82,15 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
if !tty {
wg.Add(1)
+ cwg.Add(1)
go func(r io.ReadCloser) {
+ cwg.Done()
io.Copy(ioset.err, r)
r.Close()
wg.Done()
}(f)
}
+ cwg.Wait()
return &wgCloser{
wg: wg,
dir: fifos.Dir,
