From 06dc87ae59cca1536a3a98741a267af687b6dcdd Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 16 Aug 2017 16:45:29 -0400 Subject: [PATCH] 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. Signed-off-by: Michael Crosby --- io_unix.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/io_unix.go b/io_unix.go index cde30d452..47bf71932 100644 --- a/io_unix.go +++ b/io_unix.go @@ -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,