Skip to content

Commit

Permalink
grpc: Ignore WriteStdin after the standard input has been closed
Browse files Browse the repository at this point in the history
After CloseStdin() has been invoked, there is no reason to accept
writing to stdin anymore. Instead, we can simply ignore those calls.

Fixes kata-containers#283

Signed-off-by: Sebastien Boeuf <[email protected]>
  • Loading branch information
Sebastien Boeuf committed Jul 5, 2018
1 parent 1a52204 commit ed656df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ var initRootfsMounts = []initMount{
}

type process struct {
sync.RWMutex

id string
process libcontainer.Process
stdin *os.File
Expand All @@ -66,6 +68,7 @@ type process struct {
consoleSock *os.File
termMaster *os.File
exitCodeCh chan int
stdinClosed bool
}

type container struct {
Expand Down
15 changes: 15 additions & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,16 @@ func (a *agentGRPC) WriteStdin(ctx context.Context, req *pb.WriteStreamRequest)
return &pb.WriteStreamResponse{}, err
}

proc.RLock()
defer proc.RUnlock()
stdinClosed := proc.stdinClosed

// Ignore this call to WriteStdin() if STDIN has already been closed
// earlier.
if stdinClosed {
return &pb.WriteStreamResponse{}, nil
}

var file *os.File
if proc.termMaster != nil {
file = proc.termMaster
Expand Down Expand Up @@ -1095,10 +1105,15 @@ func (a *agentGRPC) CloseStdin(ctx context.Context, req *pb.CloseStdinRequest) (
return emptyResp, nil
}

proc.Lock()
defer proc.Unlock()

if err := proc.stdin.Close(); err != nil {
return emptyResp, err
}

proc.stdinClosed = true

return emptyResp, nil
}

Expand Down

0 comments on commit ed656df

Please sign in to comment.