Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions pkgs/id/crates/id-core/src/waiting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ pub struct WaitingPool<K, E> {
/// 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<K>,
/// Events whose dependencies had all arrived when they were inserted.
ready: Vec<E>,
/// 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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<E> {
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 {
Expand Down Expand Up @@ -421,6 +428,9 @@ where
pub fn take_ready(&mut self) -> Vec<E> {
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`.
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions pkgs/id/tests/waiting_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ struct Model {
/// Held events in arrival order.
held: Vec<Held>,
arrived: BTreeSet<u32>,
ready: Vec<u32>,
/// 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<u32>,
Expand Down Expand Up @@ -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 {
Expand All @@ -144,6 +145,7 @@ impl Model {
fn arrive(&mut self, key: u32, now: u64) -> Vec<Held> {
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) {
Expand All @@ -159,6 +161,9 @@ impl Model {

fn take_ready(&mut self) -> Vec<u32> {
std::mem::take(&mut self.ready)
.into_iter()
.map(|(_, id)| id)
.collect()
}

fn expire(&mut self, now: u64) -> Vec<(u8, u32)> {
Expand Down
Loading