From be2af226312b80b8fde1c3272ce4b16001540035 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 07:08:29 -0600 Subject: [PATCH] fix(id): drop a ready-queue copy when its key arrives (#4730) WaitingPool::arrived dropped only the held copy of the key it was told about. When the event was still in the ready queue (inserted with all deps already arrived, or with no deps) that copy survived, so take_ready handed the already-applied event out a second time. The ready queue kept only keys (ready_keys) and handed out bare events, so an arrival could not find its copy. Store (key, event) pairs and drop the matching pair in arrived; other ready events keep insert order. The property-test model is updated to the same rule, and regression tests cover both insert forms plus cross-key ordering. --- pkgs/id/crates/id-core/src/waiting.rs | 52 ++++++++++++++++++++++++--- pkgs/id/tests/waiting_properties.rs | 9 +++-- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/pkgs/id/crates/id-core/src/waiting.rs b/pkgs/id/crates/id-core/src/waiting.rs index 7e147351d6..efb2f1ed76 100644 --- a/pkgs/id/crates/id-core/src/waiting.rs +++ b/pkgs/id/crates/id-core/src/waiting.rs @@ -166,8 +166,9 @@ pub struct WaitingPool { /// Keys that have arrived, and stay arrived. A dependency in this set is already satisfied. /// The store calls this present: it is applied, voided by a fork, or a fork proof header. arrived: HashSet, - /// Events whose dependencies had all arrived when they were inserted. - ready: Vec, + /// Events whose dependencies had all arrived when they were inserted, + /// paired with their key so an arrival can drop the copy (#4730). + ready: Vec<(K, E)>, /// The keys of the events in `ready`. The ready queue is the one /// state a key can be in that is neither held (`by_key`) nor /// arrived, and without this index the duplicate guard in @@ -251,8 +252,8 @@ where .filter(|dep| !self.arrived.contains(dep)) .collect(); if missing.is_empty() { - self.ready_keys.insert(key); - self.ready.push(event); + self.ready_keys.insert(key.clone()); + self.ready.push((key, event)); return Ok(()); } self.hold(author, key, missing, event, now_ms) @@ -381,12 +382,18 @@ where /// dependencies but has not arrived itself: apply it, then call `arrived` with /// its key, which can release more events. Events due at `now_ms` expire first, /// so an expired event is never returned. If `key` was itself held, that copy - /// is dropped without being returned, because the caller now holds the event. + /// is dropped without being returned, because the caller now holds the event; + /// a copy still in the ready queue is dropped the same way (#4730). pub fn arrived(&mut self, key: K, now_ms: u64) -> Vec { self.expire_due(now_ms); if let Some(seq) = self.by_key.get(&key).copied() { self.remove(seq); } + if self.ready_keys.remove(&key) { + if let Some(position) = self.ready.iter().position(|(ready, _)| *ready == key) { + self.ready.remove(position); + } + } let waiters = self.by_dep.remove(&key).unwrap_or_default(); let mut released = Vec::new(); for seq in waiters { @@ -421,6 +428,9 @@ where pub fn take_ready(&mut self) -> Vec { self.ready_keys.clear(); std::mem::take(&mut self.ready) + .into_iter() + .map(|(_, event)| event) + .collect() } /// Move the events that have reached the age cap at `now_ms` into `expired`. @@ -687,6 +697,38 @@ mod tests { assert!(pool.arrived(10, 0).is_empty()); } + #[test] + fn test_arrival_of_a_ready_event_drops_the_ready_copy_without_handing_it_out() { + // Both insert forms reach the ready queue: deps that already arrived, + // and no deps at all. In each, applying the event through another path + // must not hand the same event out again (#4730). + let mut pool = pool(); + pool.insert(author(1), 1, [10], "ready-after-dep", 0).unwrap(); + assert!(pool.arrived(1, 0).is_empty()); + assert!( + pool.take_ready().is_empty(), + "a ready copy the pool has applied is not handed out again" + ); + + pool.insert(author(1), 2, [], "ready-at-once", 0).unwrap(); + assert!(pool.arrived(2, 0).is_empty()); + assert!(pool.take_ready().is_empty()); + } + + #[test] + fn test_arrival_of_one_ready_event_leaves_the_others_in_insert_order() { + let mut pool = pool(); + pool.insert(author(1), 1, [], "a", 0).unwrap(); + pool.insert(author(1), 2, [], "b", 0).unwrap(); + pool.insert(author(1), 3, [], "c", 0).unwrap(); + assert!(pool.arrived(2, 0).is_empty()); + assert_eq!( + pool.take_ready(), + vec!["a", "c"], + "only the applied key is dropped, and the rest keep insert order" + ); + } + #[test] fn test_counts_stay_exact_across_insert_release_and_expiry() { let mut pool = pool(); diff --git a/pkgs/id/tests/waiting_properties.rs b/pkgs/id/tests/waiting_properties.rs index 09f6f64e65..592ffc5a79 100644 --- a/pkgs/id/tests/waiting_properties.rs +++ b/pkgs/id/tests/waiting_properties.rs @@ -72,7 +72,8 @@ struct Model { /// Held events in arrival order. held: Vec, arrived: BTreeSet, - ready: Vec, + /// Ready events by key and id, so an arrival can drop its copy (#4730). + ready: Vec<(u32, u32)>, expired: Vec<(u8, u32)>, /// Every event the model has expired, so that none is released later. ever_expired: BTreeSet, @@ -123,7 +124,7 @@ impl Model { .filter(|dep| !self.arrived.contains(dep)) .collect(); if missing.is_empty() { - self.ready.push(id); + self.ready.push((key, id)); return false; } if self.count(author) >= self.per_author { @@ -144,6 +145,7 @@ impl Model { fn arrive(&mut self, key: u32, now: u64) -> Vec { self.expire_due(now); self.held.retain(|held| held.key != key); + self.ready.retain(|(ready_key, _)| *ready_key != key); self.arrived.insert(key); let mut released = Vec::new(); for mut held in std::mem::take(&mut self.held) { @@ -159,6 +161,9 @@ impl Model { fn take_ready(&mut self) -> Vec { std::mem::take(&mut self.ready) + .into_iter() + .map(|(_, id)| id) + .collect() } fn expire(&mut self, now: u64) -> Vec<(u8, u32)> {