Skip to content

fix(state): backfill on connect for the gRPC transport too - #177

Merged
transfix merged 1 commit into
masterfrom
fix/state-grpc-replication-race
Aug 5, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/state-grpc-replication-race

Conversation

@transfix

@transfix transfix commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Replaces #172, which GitHub auto-closed when its base branch was deleted on #171's merge. Same commit, rebased onto master so it is now a single commit; #171 is already in.

Same bug, other transport

state_transport_grpc::pump_shard() had the identical defect #171 fixed for IPC: it drains a shard's journal and advances the publish cursor whether or not any stream exists, and publish() only reaches the streams open at that instant. Anything pumped before a peer connected is dropped and never retried.

This reuses state_cluster_shard::replay_local() from #171 (reads the journal without touching the publish cursor), so drain_local() keeps sole ownership of the cursor and already-connected peers are unaffected.

Both establishment paths

gRPC opens streams in two places and both were broken — the server-side Channel() handler and the client-side connect_to_peer(). There is a test for each, and both fail without backfill:

test backfill off on
LateJoinerReceivesWritesMadeBeforeItConnected (B dials A → server admit) FAILED "" vs "v1" OK
LateJoinerBackfilledWhenLocalNodeDials (A dials B → client admit) FAILED "" vs "v1" OK
BackfillPrecedesConcurrentLiveTraffic FAILED OK

Two things worth reviewing closely

write_fn is split. It took write_mu internally, so a caller could not hold the lock across several frames without self-deadlocking. It becomes write_raw (caller holds the lock) plus write() / write_locked() wrappers on connection. All nine existing senders move to write(); behaviour is unchanged. I checked every write site first to confirm none holds _shards_mu or _conns_mu while taking a connection's write_mu, so registering under write_mu introduces no cycle.

connect_to_peer() now spawns the client reader before publishing the connection. admit_connection() makes it visible to stop(), which joins client_reader — so the thread has to already be assigned, or stop() could snapshot the connection, skip the not-yet-joinable thread, and leave a joinable std::thread to destruct (std::terminate). Registering after the spawn restores that invariant.

On the ordering test

BackfillPrecedesConcurrentLiveTraffic has B dial A specifically so the replay runs on A's handler thread while A keeps publishing. Dialling from A instead completes the backfill inside connect_to_peer() before any live write can race it — an earlier version of the test did exactly that and caught nothing. With the write-mutex hold removed it now fails 3/3 (B's value moved backwards).

Verification

  • 824 State* ctest cases pass with -DCVC_ENABLE_GRPC=ON.
  • gRPC suite run 40x: 0 failures.
  • Re-verified after the rebase onto master: 21/21 pass.
  • state_transport_grpc_test.cpp:563 (EXPECT_EQ(0u, tA.total_received_mutations()), in BearerTokenRejectsMissingMetadata) is unaffected — the stream is rejected at the auth layer, so no backfill reaches it.

Same defect #171 fixed for the IPC transport: pump_shard() drains a
shard's journal and advances its publish cursor whether or not any
stream exists, and publish() only reaches the streams open at that
instant. Anything pumped before a peer connected is dropped and never
retried, so a peer that connects later never sees it.

gRPC establishes streams in two places and both needed the replay:
the server-side Channel() handler and the client-side
connect_to_peer(). Both now go through admit_connection(), which
registers the connection and sends Hello followed by the shards'
journaled local mutations, all under the connection's write mutex —
the ordering matters because state_replica::seen() is exact
membership, not a high-water mark, so a live frame overtaking the
replay would leave the older values applied last.

To hold that lock across several frames, connection::write_fn (which
took write_mu internally, so a caller could not hold it) is split
into write_raw (caller holds the lock) plus write()/write_locked()
wrappers. Every existing sender moves to write(); behaviour is
unchanged.

connect_to_peer() now spawns the client reader before publishing the
connection. admit_connection() makes it visible to stop(), which
joins client_reader, so the thread has to be assigned first or stop()
could miss it and leave a joinable std::thread to destruct.

All three new replication tests fail with backfill disabled and pass
with it, covering both establishment paths.
BackfillPrecedesConcurrentLiveTraffic has B dial A so the replay runs
on A's handler thread while A keeps publishing — dialling from A
instead completes the backfill inside connect_to_peer() before any
live write can race it, which is why an earlier version of that test
caught nothing. With the write-mutex hold removed it fails 3/3.

824 State* ctest cases pass with -DCVC_ENABLE_GRPC=ON; the gRPC suite
ran 40x clean.
@transfix
transfix merged commit 13cea5b into master Aug 5, 2026
7 checks passed
@transfix
transfix deleted the fix/state-grpc-replication-race branch August 5, 2026 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant