Skip to content

conformance: grpc.DefaultClient is not safe for concurrent SendRPC and is not reusable after Close() #4941

@lexfrei

Description

@lexfrei

What happened:

conformance/utils/grpc.DefaultClient (the default Options.GRPCClient) has two robustness bugs, both reproduced locally:

  1. Not concurrency-safe. ensureConnection/resetConnection read and write c.Conn with no lock, and SendRPC reads it (pb.NewGrpcEchoClient(c.Conn)). go test -race on N concurrent SendRPC calls against one shared client reports a data race:
WARNING: DATA RACE
Write at ... ensureConnection() grpc.go:164   (c.Conn, err = grpc.NewClient(...))
Previous read at ... grpc.go:155              (if c.Conn != nil)
Previous read at ... grpc.go:201              (pb.NewGrpcEchoClient(c.Conn))
  1. Not reusable after Close(). Close() shuts the *grpc.ClientConn but does not nil c.Conn, so a later SendRPC short-circuits in ensureConnection and dials the closed connection. Reuse-after-Close() returns codes.Canceled; a fresh client returns OK against the same server.

What you expected to happen:

The documented default client should be safe to use concurrently, and either reusable after Close() or clearly single-use.

How to reproduce it (as minimally and precisely as possible):

(1) go test -race with N goroutines calling SendRPC on one DefaultClient. (2) SendRPCClose()SendRPC against an in-process echo server: the second call returns Canceled while a fresh client returns OK. I have both as Go tests, ready to include.

Anything else we need to know?:

Both are latent today (the suite constructs a fresh DefaultClient per request and rarely reuses one after Close()), but:

Suggested fix: guard c.Conn with a sync.Mutex (concurrency); set c.Conn = nil in Close() (reuse). I have a fix plus the two regression tests ready — would you prefer them as one PR or two separate PRs (one per bug)? Happy either way.

/kind bug
/area conformance-machinery

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/conformance-machineryIssues or PRs related to the machinery and the suite used to run conformance tests.kind/bugCategorizes issue or PR as related to a bug.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions