fix: tolerate consecutive stale client revisions
Rust CI fader_master passed its selected CI mode.
MadGobblins/fader_master/pipeline/head This commit looks good Details

Test: cargo test -p tui --all-targets --all-features --locked
Test: cargo clippy -p tui --all-targets --all-features --locked -- -D warnings
Test: cargo fmt --all -- --check
This commit is contained in:
Dandaedre 2026-10-16 00:15:35 +02:00
parent 51b23b8b25
commit 164be169f0
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
3 changed files with 116 additions and 20 deletions

View File

@ -401,8 +401,9 @@ work without waiting for a socket response. Pending volume work is
latest-value-per-object; mute presses remain a bounded FIFO. Confirmed snapshots
replace the local base and retain optimistic overlays until observation settles
them. Poll quickly while writes are pending, slowly while idle, and resnapshot
then retry once after a stale edit. Start immediately with a disconnected
placeholder when the socket is absent, retain the last confirmed items across
then retry a bounded number of times after stale mutations so consecutive
server revisions cannot roll an optimistic edit back. Start immediately with a
disconnected placeholder when the socket is absent, retain the last confirmed items across
transport failures, surface the connection error inside the terminal, and keep
retrying so starting or restarting the server reconnects without restarting the
TUI.

View File

@ -246,8 +246,9 @@ Dependencies: M3, M2; may proceed after the fake-adapter slice.
cannot monopolize the state owner.
- [x] **M7.3 Client resync:** full snapshot on attach/lag and revision checks for edits. Tests: TUI exit never stops the server; reconnect shows current observations; concurrent clients cannot overwrite each other's edits unnoticed.
Each request receives a complete current snapshot; stale edit revisions
trigger a background resnapshot and one retry. The TUI's IPC worker exits
independently of the server, starts with a disconnected placeholder when the
trigger bounded background resnapshot/retry attempts, covering consecutive
server revisions without allowing an unbounded retry loop. The TUI's IPC
worker exits independently of the server, starts with a disconnected placeholder when the
socket is absent, retains the last confirmed view during failures, and retries
until a server appears again. Competing edits from one attached revision prove
that only the first is accepted and that a lagging client obtains the newer

View File

@ -676,6 +676,8 @@ fn execute_action<C: MixerClient>(
confirmed: &mut MixerSnapshot,
action: &BufferedAction,
) -> io::Result<MixerSnapshot> {
const MAX_STALE_ATTEMPTS: usize = 8;
let execute = |client: &mut C, revision, configuration_revision| match action {
BufferedAction::SetVolume { id, gain } => client.set_volume(id.clone(), *gain, revision),
BufferedAction::ToggleMute { id } => client.toggle_mute(id.clone(), revision),
@ -691,24 +693,36 @@ fn execute_action<C: MixerClient>(
BufferedAction::CancelMidiLearn => client.cancel_midi_learn(),
BufferedAction::SaveConfiguration => client.save_configuration(configuration_revision),
};
match execute(client, confirmed.revision, confirmed.configuration.revision) {
Ok(snapshot) => Ok(snapshot),
Err(first)
if matches!(action, BufferedAction::SetVolume { .. })
|| (matches!(action, BufferedAction::EditConfiguration { .. })
&& first.kind() == io::ErrorKind::InvalidData)
|| (matches!(action, BufferedAction::SaveConfiguration)
&& first.kind() == io::ErrorKind::InvalidData)
|| (matches!(action, BufferedAction::ToggleMute { .. })
&& first.kind() == io::ErrorKind::InvalidData) =>
{
*confirmed = client.snapshot()?;
execute(client, confirmed.revision, confirmed.configuration.revision).map_err(
|second| io::Error::other(format!("{first}; retry after resync failed: {second}")),
)
let can_retry_stale = matches!(
action,
BufferedAction::SetVolume { .. }
| BufferedAction::ToggleMute { .. }
| BufferedAction::EditConfiguration { .. }
| BufferedAction::SaveConfiguration
);
let mut first_error = None;
for attempt in 0..MAX_STALE_ATTEMPTS {
match execute(client, confirmed.revision, confirmed.configuration.revision) {
Ok(snapshot) => return Ok(snapshot),
Err(error)
if can_retry_stale
&& error.kind() == io::ErrorKind::InvalidData
&& attempt + 1 < MAX_STALE_ATTEMPTS =>
{
first_error.get_or_insert_with(|| error.to_string());
*confirmed = client.snapshot()?;
}
Err(error) => {
return Err(match first_error {
Some(first) => {
io::Error::other(format!("{first}; retry after resync failed: {error}"))
}
None => error,
});
}
}
Err(error) => Err(error),
}
unreachable!("bounded action retry loop always returns")
}
fn publish_confirmed(state: &mut BufferedState, confirmed: &MixerSnapshot) {
@ -5580,4 +5594,84 @@ mod tests {
assert_eq!(result.revision, 6);
assert_eq!(result.items[0].gain, Some(0.8));
}
#[test]
fn buffered_configuration_edit_survives_consecutive_revision_races() {
struct RacingConfigurationClient {
snapshot: MixerSnapshot,
attempts: Vec<u64>,
}
impl MixerClient for RacingConfigurationClient {
fn snapshot(&mut self) -> io::Result<MixerSnapshot> {
Ok(self.snapshot.clone())
}
fn set_volume(
&mut self,
_id: MixerObjectId,
_gain: f64,
_expected_revision: u64,
) -> io::Result<MixerSnapshot> {
unreachable!()
}
fn toggle_mute(
&mut self,
_id: MixerObjectId,
_expected_revision: u64,
) -> io::Result<MixerSnapshot> {
unreachable!()
}
fn edit_configuration(
&mut self,
edit: ConfigurationEdit,
expected_revision: u64,
) -> io::Result<MixerSnapshot> {
self.attempts.push(expected_revision);
if self.attempts.len() <= 2 {
self.snapshot.configuration.revision += 1;
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"stale configuration revision {expected_revision}; current revision is {}",
self.snapshot.configuration.revision
),
));
}
apply_optimistic_configuration_edit(&mut self.snapshot, &edit);
self.snapshot.configuration.revision += 1;
Ok(self.snapshot.clone())
}
}
let layout = |id: &str| MixerLayoutEntry::Separator {
id: id.to_owned(),
label: id.to_owned(),
};
let mut confirmed = DemoClient::default().view();
confirmed.configuration.revision = 2;
confirmed.configuration.mixer_layout = vec![layout("a"), layout("b")];
let mut client = RacingConfigurationClient {
snapshot: confirmed.clone(),
attempts: Vec::new(),
};
let result = execute_action(
&mut client,
&mut confirmed,
&BufferedAction::EditConfiguration {
edit: ConfigurationEdit::MoveLayoutEntry {
id: "a".to_owned(),
position: 1,
},
},
)
.unwrap();
assert_eq!(client.attempts, [2, 3, 4]);
assert_eq!(layout_ids(&result), ["b", "a"]);
assert_eq!(result.configuration.revision, 5);
}
}