Streaming tablet gRPC clients hold RLock across stream receive and deadlock Close
perfloop-oss/vitess · Cyclic wait
https://perfloop.ai/t/oss/case_rb6ktfges3
Verdict
VERIFIED · settled 2026-11-25
What happened: The check fails before this change and passes with it.
Hypothesis
Four streaming RPC methods on the tablet gRPC client acquire the connection read-write mutex twice recursively and keep the outer read lock held for the entire stream receive loop. Because closing a tablet connection takes the write lock on that same mutex to shut down the underlying gRPC channel, closing a stalled or failing tablet connection blocks waiting for the stream to end while the stream blocks waiting for the closed channel, and an interleaved close between the two read locks permanently deadlocks Go's non-reentrant `sync.RWMutex`. How often this triggers in production deployments is unmeasured, but it directly affects default VTGate schema tracking and streaming transactions during tablet stalls, failovers, and shutdown.
In `go/vt/vttablet/grpctabletconn/conn.go`, `StreamExecute` (lines 153-188) documents the required two-phase pattern: hold `conn.mu.RLock()` only inside the inner closure that opens the gRPC stream, then release it before calling `stream.Recv()`. Four sibling methods cite that comment yet still acquire an outer `conn.mu.RLock()` with `defer conn.mu.RUnlock()` before calling the inner closure that acquires `conn.mu.RLock()` a second time: - `gRPCQueryClient.BeginStreamExecute` (lines 514-525) - `gRPCQueryClient.ReserveBeginStreamExecute` (lines 1035-1046) - `gRPCQueryClient.ReserveStreamExecute` (lines 1149-1160) - `gRPCQueryClient.GetSchema` (lines 1245-1256)
This creates two cross-boundary cyclic waits with `discovery.tabletHealthCheck.closeConnection` (`go/vt/discovery/tablet_health_check.go:371-386`) and `finalizeConn` (`tablet_health_check.go:390-409`), which call `conn.Close(ctx)` -> `conn.mu.Lock()` (`conn.go:1286-1296`) when a tablet's healthcheck fails, times out, or the tablet is removed. First, if `Close()` runs while one of the four methods is blocked in `stream.Recv()`, `Close()` cannot acquire `conn.mu.Lock()` to call `cc.Close()`, so the stream is never aborted; for `schema.Tracker.loadTables` / `loadViews` / `loadUDFs` (`go/vt/vtgate/schema/tracker.go:107-180`), VTGate calls `conn.GetSchema(t.ctx, ...)` with a deadline-free background context while holding `Tracker.mu`, freezing both schema tracking and tablet healthcheck reconnection indefinitely. Second, if `Close()` calls `conn.mu.Lock()` between the outer and inner `conn.mu.RLock()` calls, Go's `sync.RWMutex` blocks the inner `RLock()` behind the pending writer while the writer waits for the outer `RLock()`, permanently deadlocking even if the RPC context is canceled.
Running `go test -v -count=1 ./go/vt/vttablet/grpctabletconn -run '^TestPerfloopStreamCloseDoesNotDeadlock$'` over a real local `grpcqueryservice` server and `DialTablet` client confirms the defect deterministically: `StreamExecute_control` passes in 0.00s (Close returns immediately and unblocks `stream.Recv`), while `BeginStreamExecute`, `ReserveBeginStreamExecute`, `ReserveStreamExecute`, and `GetSchema` all fail with `deadlock: Close blocked in sync.(*RWMutex).Lock while <method> held conn.mu.RLock across stream.Recv`. Removing the redundant outer lock/check in an experimental edit makes all five subtests and the full `go/vt/vttablet/grpctabletconn` test suite pass.
The provisional check is `TestPerfloopStreamCloseDoesNotDeadlock` in `go/vt/vttablet/grpctabletconn/perfloop_stream_close_deadlock_test.go`, which confirms the defect if any of the four methods blocks `Close` on `sync.(*RWMutex).Lock` and rejects it once all five methods pass with `-race`. Upstream check gap: two pages of open GitHub pull requests filtered to `go/vt/vttablet/grpctabletconn/conn.go` returned no matches, leaving older open PR pages unchecked.
Change to test: Remove the redundant outer read lock, deferred unlock, and nil-connection check in `BeginStreamExecute`, `ReserveBeginStreamExecute`, `ReserveStreamExecute`, and `GetSchema` so they follow `StreamExecute`'s lock discipline by holding `conn.mu.RLock` only while starting the gRPC stream and releasing it before `stream.Recv`. Keep the inner closure's error unwrapped when returning from `ReserveBeginStreamExecute` and `ReserveStreamExecute` so `tabletconn.ConnClosed` and already-converted errors are preserved.
Where it lives
perfloop-oss/vitess · go/vt/vtgate/schema/tracker.go
Evidence
The check fails before this change and passes with it: `On a real local gRPC channel stalled before its first response, Close completes without canceling the caller context and terminates the pending receive and server RPC for BeginStreamExecute, ReserveBeginStreamExecute, ReserveStreamExecute and GetSchema; StreamExecute retains this behavior.`
Idle tablet channel Close · 92 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
148983 |
151269 |
+2374 |
−2293 to +7186 |
no regression (noise ≤ 0.05 × |baseline median|) |
PASSED |
Checks: 2 of 2 passed. Verification: no defect found.
Timeline
2026-11-24· Case opened