Skip to content

Commit

Permalink
runtime: guard Close() until both streams are complete
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Wagner <thepwagner@github.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thepwagner authored and thaJeztah committed Apr 1, 2019
1 parent 255da2a commit de85314
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions linux/proc/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"os"
"sync"
"sync/atomic"
"syscall"

"github.com/containerd/containerd/log"
Expand All @@ -39,7 +40,7 @@ var bufPool = sync.Pool{
}

func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, wg, cwg *sync.WaitGroup) error {
var sameFile io.WriteCloser
var sameFile *countingWriteCloser
for _, i := range []struct {
name string
dest func(wc io.WriteCloser, rc io.Closer)
Expand Down Expand Up @@ -101,14 +102,18 @@ func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, w
}
} else {
if sameFile != nil {
sameFile.count++
i.dest(sameFile, nil)
continue
}
if fw, err = os.OpenFile(i.name, syscall.O_WRONLY|syscall.O_APPEND, 0); err != nil {
return fmt.Errorf("containerd-shim: opening %s failed: %s", i.name, err)
}
if stdout == stderr {
sameFile = fw
sameFile = &countingWriteCloser{
WriteCloser: fw,
count: 1,
}
}
}
i.dest(fw, fr)
Expand All @@ -134,6 +139,19 @@ func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, w
return nil
}

// countingWriteCloser masks io.Closer() until close has been invoked a certain number of times.
type countingWriteCloser struct {
io.WriteCloser
count int64
}

func (c *countingWriteCloser) Close() error {
if atomic.AddInt64(&c.count, -1) > 0 {
return nil
}
return c.WriteCloser.Close()
}

// isFifo checks if a file is a fifo
// if the file does not exist then it returns false
func isFifo(path string) (bool, error) {
Expand Down

0 comments on commit de85314

Please sign in to comment.