Skip to content

Commit

Permalink
[bytestream] be careful not to hang goroutines writing to putResult
Browse files Browse the repository at this point in the history
If this goroutine blocks trying to write to putResult after Write
exits then it will leak.

We can avoid this by making putResult a buffered channel with
length 1, since a Write call will send to this channel at most
once. Then that single send will never block, and the goroutine
can exit.

While we're at it, let's make recvResult buffered too, just in case.

Fixes #444.
  • Loading branch information
mostynb committed Jun 7, 2021
1 parent c94bea5 commit dca223d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions server/grpc_bytestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ func (s *grpcServer) Write(srv bytestream.ByteStream_WriteServer) error {
var resp bytestream.WriteResponse
pr, pw := io.Pipe()

putResult := make(chan error)
recvResult := make(chan error)
putResult := make(chan error, 1)
recvResult := make(chan error, 1)
resourceNameChan := make(chan string, 1)

go func() {
Expand Down Expand Up @@ -297,7 +297,8 @@ func (s *grpcServer) Write(srv bytestream.ByteStream_WriteServer) error {
}

go func() {
putResult <- s.cache.Put(cache.CAS, hash, size, pr)
err := s.cache.Put(cache.CAS, hash, size, pr)
putResult <- err
}()

firstIteration = false
Expand Down

0 comments on commit dca223d

Please sign in to comment.