Ended replication stream deadlocks session restart and steals later requests

perfloop-oss/openraft · Uncatalogued correctness

https://perfloop.ai/t/oss/case_y2wc8fx078

Verdict

VERIFIED · settled 2026-11-25 · pull request opened as databendlabs/openraft#2519

What happened: The check fails before this change and passes with it.

Hypothesis

Replication keeps one shared request-generator state across consecutive streaming sessions without telling a finished session's request stream to stop. When a streaming transport polls that request stream on its own task, as bidirectional gRPC clients do, an ended session can either hold the shared state lock indefinitely after a clean stream close or wake up later and steal entries from the next session.

In a two-node cluster with default heartbeats enabled, closing an idle pipeline response stream cleanly causes replication to stall on the shared lock, steal the next client write into the dead stream, and time out the write while heartbeats keep the leader alive. In a second schedule, a request stream from a failed session wakes up after a replacement session starts and consumes entries from the active session's state.

Each `ReplicationCore::run_stream_session` call in `openraft/src/replication/mod.rs` (lines 393-476) clones `self.stream_state` into a `StreamContext` (`openraft/src/replication/stream_context.rs`) and hands `Self::new_request_stream(stream_context)` (`futures_util::stream::unfold(..., Self::next_append_request)`) to `network.stream_append(req_strm, option)`. `next_append_request` (lines 199-224) locks `stream_context.stream_state` across `StreamState::next_request()`, which blocks inside `StreamState::get_log_id_range()` waiting on `cancel_rx`, `replicate_rx`, `io_submitted_rx`, and `committed_rx` while holding the mutex.

When `handle_response_stream` sees response-stream EOF (`Ok(())`), `run_stream_session` returns `Ok(Some(remaining))` without notifying `RaftCore` to clear the plan and without signalling `req_strm` to stop. The outer loop `ReplicationCore::replicate` then calls `reconcile_backoff()` (line 271), which awaits `self.stream_state.lock().await` and blocks behind the ended session's `req_strm`. When a new write arrives, the ended `req_strm` wakes up, steals the entry, advances `stream_state.payload`, and re-enters `get_log_id_range()` holding the lock again. Even when a new session does open (such as after an open-time `RPCError` and `ClearReplicate`), `run_stream_session` overwrites `payload` and `inflight_id` on the same shared `StreamState`, so a delayed poll of the prior session's `req_strm` passes `is_plan_current()` and consumes requests belonging to the new session.

A provisional integration check (`cargo test -p tests --test replication_stream_session_isolation`) reproduced both failures on the unchanged revision (`clean_eof_does_not_deadlock_replication_or_steal_writes` observed the ended session stealing `[(Some(2), [3])]` and stalling `client_write`; `failed_session_stream_does_not_steal_from_replacement_session` observed session 3 stealing `[(Some(0), [1])]` from session 4). With an experimental per-session watch cancellation channel dropped on `run_stream_session` exit, checked in `next_append_request` and selected in `StreamState::get_log_id_range`, both tests passed in 0.67s. Note that existing unit tests in `openraft/src/replication/clear_plan_test.rs` (`clear_is_confirmed_once_a_read_that_outlived_its_session_returns` and `closed_task_returns_once_a_running_read_returns`) start a read on a failed session's stream after `Session::Fail` to exercise in-flight read completion before purge; the Case should preserve purge-safety while updating or verifying how in-flight reads are tested once post-session polling is stopped.

Upstream check: open pull requests on `databendlabs/openraft` contain only PR #2517 (gRPC heartbeat connection separation), and recent replication commits through `cb04941` address `ClearReplicate`, `lingering_replications`, and open-time task cancellation without adding per-session stream isolation. The Case should also verify that per-session cancellation interrupts waits in `backoff_if_enabled` and `wait_for_send_period` as well as `get_log_id_range` without cutting off an already-running storage read before `confirm_clear` or task exit.

Change to test: Isolate each `stream_append()` session with a per-session stop signal that ends its request stream as soon as `run_stream_session()` exits. Cancel waits in `StreamState` on that session signal and return `None` from `next_append_request()` once the session has ended so a lingering transport cannot hold the shared `StreamState` lock or consume requests from a later session.

Where it lives

perfloop-oss/openraft · openraft/src/replication/mod.rs

Evidence

The check fails before this change and passes with it: `In the two-voter tonic gRPC regression with heartbeats enabled, clean replication-response EOF and a replication-open error release the ended request generators and allow the next write to commit.`

The check fails before this change and passes with it: `When a transport delays polling a failed replication session until its replacement has installed a plan, the failed session terminates without reading or consuming that replacement plan.`

The check fails before this change and passes with it: `When a replication session ends after starting a storage read, that session discards the read result and terminates without starting another read.`

Checks: 7 of 7 passed. Verification: no defect found.

Timeline