From ed656dfa67ebf6910968c712196a2ed45aa841d7 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 2 Jul 2018 11:01:57 -0700 Subject: [PATCH] grpc: Ignore WriteStdin after the standard input has been closed After CloseStdin() has been invoked, there is no reason to accept writing to stdin anymore. Instead, we can simply ignore those calls. Fixes #283 Signed-off-by: Sebastien Boeuf --- agent.go | 3 +++ grpc.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/agent.go b/agent.go index ec01250695..a529a17770 100644 --- a/agent.go +++ b/agent.go @@ -58,6 +58,8 @@ var initRootfsMounts = []initMount{ } type process struct { + sync.RWMutex + id string process libcontainer.Process stdin *os.File @@ -66,6 +68,7 @@ type process struct { consoleSock *os.File termMaster *os.File exitCodeCh chan int + stdinClosed bool } type container struct { diff --git a/grpc.go b/grpc.go index 37f9f2d96c..4937054525 100644 --- a/grpc.go +++ b/grpc.go @@ -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 @@ -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 }