s2-cli
s2-cli copied to clipboard
[Detail Bug] Pinger appends task continues after append stream error (missing return)
Detail Bug Report
https://app.detail.dev/org_89d327b3-b883-4365-b6a3-46b6701342a9/bugs/bug_7c2d20da-f44c-48f9-a3a7-904efffaa8cb
Summary
- Context: The
Pingerstruct insrc/ping.rsuses two background tasks (appends_handleandreads_handle) to concurrently process append acknowledgments and read records from a stream for latency measurement. - Bug: The
appends_handletask does not terminate when it encounters an error from the append stream, unlike thereads_handletask which correctly terminates on errors. - Actual vs. expected: When
append_stream.next()returns an error, the appends task sends the error to the channel but continues running and processing subsequent messages; it should instead terminate immediately after sending the error, matching the behavior of the reads task. - Impact: The appends task may continue processing append acknowledgments after an error, potentially causing unexpected messages to appear in subsequent pings, leading to incorrect latency measurements or mismatched append/read pairs.
Code with bug
let appends_handle = tokio::spawn(async move {
while let Some(next) = append_stream.next().await {
match next {
Ok(AppendAck { start, end, .. }) => {
let append = Instant::now();
let records = end.seq_num - start.seq_num;
if records != 1 {
appends_tx
.send(Err(S2CliError::PingStreamMutated))
.expect("appends channel open");
return;
}
appends_tx.send(Ok(append)).expect("appends channel open");
}
Err(e) => {
appends_tx
.send(Err(S2CliError::from(ServiceError::new(
ServiceErrorContext::AppendSession,
e,
))))
.expect("appends channel open");
// <-- BUG 🔴 Missing return statement - task continues running!
}
}
}
});
Codebase inconsistency
reads_handleterminates on error, butappends_handledoes not, causing continued polling and potential orphaned messages.
Reference (correct behavior in reads task):
Some(Err(e)) => {
reads_tx.send(Err(
ServiceError::new(ServiceErrorContext::ReadSession, e).into()
)).expect("open reads channel");
return; // Correctly terminates the task
}
Current (incorrect behavior in appends task):
Err(e) => {
appends_tx
.send(Err(S2CliError::from(ServiceError::new(
ServiceErrorContext::AppendSession,
e,
))))
.expect("appends channel open");
// Missing return statement
}
Recommended fix
Add a return in the error branch of appends_handle to match reads_handle:
Err(e) => {
appends_tx
.send(Err(S2CliError::from(ServiceError::new(
ServiceErrorContext::AppendSession,
e,
))))
.expect("appends channel open");
return; // <-- FIX 🟢 Add this to terminate the task
}
ping replaced with bench cmd