Skip to content

Commit

Permalink
Fix race condition on bidirectional streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Aug 9, 2022
1 parent 3dac4b0 commit b179465
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ ci: depsdev test
test: cert
go test ./... -coverprofile=coverage.out -covermode=count

race:
go test ./... -race

lint:
golangci-lint run ./...

Expand Down
8 changes: 7 additions & 1 deletion grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ L:
return err
}
err = stream.SendMsg(req)
req.Reset()
case grpcOpRecieve:
res, err := stream.RecvMsg()
stat, ok := status.FromError(err)
Expand Down Expand Up @@ -373,7 +374,6 @@ L:
default:
return fmt.Errorf("invalid op: %v", m.op)
}
req.Reset()
}
stat, ok := status.FromError(err)
if !ok {
Expand Down Expand Up @@ -409,6 +409,12 @@ L:
}
}

// If the connection is not disconnected here, it will fall into a race condition when retrieving the trailer.
if err := rnr.cc.Close(); err != nil {
return err
}
rnr.cc = nil

d["messages"] = messages
if h, err := stream.Header(); err == nil {
d["headers"] = h
Expand Down

0 comments on commit b179465

Please sign in to comment.