From 77d015de8931a534e7f05d2728008ab494d0a254 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 20:14:34 -0500 Subject: [PATCH] id(event_store): release/present_proof flush expired-buffer rows in the same transaction (#2690) Mirror::release and Mirror::present_proof call pool.arrived, which expires due events into the pool expired buffer as a side effect -- but only Mirror::expire deleted those events rows. A transaction whose waiting-pool work was a release could therefore commit with the pool and the rows disagreeing, and the expiry was reported by a later, unrelated expire (or dropped silently by the reload after a restart). Both methods now flush the expired buffer the way Mirror::expire does (shared flush_expired): the releasing call itself deletes the expired rows, in its own transaction. In present_proof the flush runs before the park, so a held proof that was itself due is still re-held for good rather than deleted. Regression tests: test_a_mirror_release_flushes_the_events_it_expires (mirror-level, driven through transact/hold/release directly) and test_a_release_flushes_the_events_it_expires (ingest-level: pool, rows and the expire count agree immediately after the releasing call). --- pkgs/id/src/event_store/ingest.rs | 99 +++++++++++++++++++++++++ pkgs/id/src/event_store/waiting_rows.rs | 19 +++++ 2 files changed, 118 insertions(+) diff --git a/pkgs/id/src/event_store/ingest.rs b/pkgs/id/src/event_store/ingest.rs index 984e2bdbf6..80916f8430 100644 --- a/pkgs/id/src/event_store/ingest.rs +++ b/pkgs/id/src/event_store/ingest.rs @@ -2188,6 +2188,105 @@ mod tests { assert!(store.waiting_ids().unwrap().is_empty()); } + /// #2690, at the mirror itself: a release whose `arrived` expires another + /// held event must delete that event's row in the releasing transaction -- + /// not leave it for a later, unrelated expire to count, or for a restart + /// to drop silently. Driven through the mirror directly, because the + /// ingest path's own expire-first composition would mask a mirror that + /// forgot the flush. + #[test] + fn test_a_mirror_release_flushes_the_events_it_expires() { + let signer = key(39); + let events = chain_of(&signer, 3); + let (_dir, mut store) = open(); + // events[2] is held at 0, waiting for events[1]. It is due at WEEK_MS. + store + .transact(0, |m| { + m.hold(events[2].clone(), vec![events[1].id], false, 0)?; + Ok(((), true)) + }) + .unwrap(); + // events[1] is held just before the cap, waiting for events[0]: fresh. + store + .transact(WEEK_MS - 1_000, |m| { + m.hold( + events[1].clone(), + vec![events[0].id], + false, + WEEK_MS - 1_000, + )?; + Ok(((), true)) + }) + .unwrap(); + // Releasing events[0] at the cap releases events[1]; inside that + // release, events[2] reaches the age cap and expires. + let released = store + .transact(WEEK_MS, |m| { + let released = m.release(&events[0].id, WEEK_MS)?; + Ok((released, true)) + }) + .unwrap(); + assert_eq!(released.len(), 1); + assert_eq!(released[0].id, events[1].id); + // The released event's row stays until its caller decides it; the + // expired event's row is gone in the same transaction, and nothing + // remains for a later expire to count. + let rows = store.waiting_ids().unwrap(); + assert!(rows.contains(&events[1].id), "rows after release: {rows:?}"); + assert!( + !rows.contains(&events[2].id), + "the expired event's row survived the release: {rows:?}" + ); + // Decide the released event the way its caller would, so the store + // is back at a transaction boundary it accepts; then nothing remains + // for a later expire to count. + store + .transact(WEEK_MS, |m| { + m.drop_row(&events[1].id)?; + Ok(((), true)) + }) + .unwrap(); + assert_eq!(store.expire_waiting(WEEK_MS).unwrap(), 0); + assert!(store.waiting_ids().unwrap().is_empty()); + store.check_invariants().unwrap(); + } + + /// #2690: a release whose `arrived` expires another held event must delete + /// that event's row in the releasing transaction -- not leave it for a + /// later, unrelated expire to count, or for a restart to drop silently. + #[test] + fn test_a_release_flushes_the_events_it_expires() { + let signer = key(38); + let events = chain_of(&signer, 3); + let (_dir, mut store) = open(); + // events[2] is held at 0, waiting for events[1]. It is due at WEEK_MS. + assert_eq!( + ingest(&mut store, &events[2]), + Ingest::Waiting { id: events[2].id } + ); + // events[1] is held just before the cap, waiting for events[0]: fresh. + assert_eq!( + store.ingest(&events[1].bytes, WEEK_MS - 1_000).unwrap(), + Ingest::Waiting { id: events[1].id } + ); + // events[0] arrives at the cap and is accepted, releasing events[1]. + // Inside that release, events[2] reaches the age cap and expires. + assert_eq!( + store.ingest(&events[0].bytes, WEEK_MS).unwrap(), + Ingest::Accepted { + id: events[0].id, + released: vec![events[1].id], + } + ); + // The expiry belonged to the releasing call: nothing remains for a + // later expire to count, the pool holds nothing, and no row for + // events[2] (or any waiting row) survives. + assert_eq!(store.expire_waiting(WEEK_MS).unwrap(), 0); + assert_eq!(store.waiting_len(), 0); + assert!(store.waiting_ids().unwrap().is_empty()); + store.check_invariants().unwrap(); + } + #[test] fn test_a_refused_waiting_event_leaves_no_row() { let dir = tempfile::tempdir().unwrap(); diff --git a/pkgs/id/src/event_store/waiting_rows.rs b/pkgs/id/src/event_store/waiting_rows.rs index 8fa4ecbb0c..d1bc0572e5 100644 --- a/pkgs/id/src/event_store/waiting_rows.rs +++ b/pkgs/id/src/event_store/waiting_rows.rs @@ -189,6 +189,18 @@ impl Mirror<'_> { /// Drops every held event that is due at `now`, with its row. Returns how many were /// dropped. pub fn expire(&mut self, now: u64) -> Result { + self.flush_expired(now) + } + + /// Deletes the rows of the events the pool has expired into its buffer, and returns + /// how many. `pool.arrived` expires due events as a side effect of a release, into + /// the buffer only `pool.expire` drains; a caller that releases without expiring + /// first would commit with the pool and the rows disagreeing -- the expired events + /// gone from the pool, their rows still on disk, and their expiry left for a later, + /// unrelated `expire` to report (or, across a restart, for the reload to drop + /// silently). Flushing here, the way [`expire`](Self::expire) does, makes the + /// releasing call itself the one that effects the expiry, in its own transaction. + fn flush_expired(&mut self, now: u64) -> Result { let expired = self.pool.expire(now); for (_author, event) in &expired { delete(self.txn, &event.id)?; @@ -260,6 +272,9 @@ impl Mirror<'_> { /// caller decides each one. pub fn release(&mut self, id: &Hash, now: u64) -> Result, StoreError> { let released = self.pool.arrived(*id, now); + // `arrived` expired every event due at `now` into the pool's buffer; their + // rows go in this transaction too, not a later one's. + self.flush_expired(now)?; delete(self.txn, id)?; self.drop_dep(id)?; Ok(released) @@ -272,6 +287,10 @@ impl Mirror<'_> { pub fn present_proof(&mut self, id: &Hash, now: u64) -> Result, StoreError> { let held = get(self.txn, id)?; let released = self.pool.arrived(*id, now); + // As in `release`: the events `arrived` expired lose their rows here. The + // flush runs before the park below, so a held proof that was itself due is + // re-held for good by the park rather than deleted by the flush. + self.flush_expired(now)?; self.drop_dep(id)?; if let Some(row) = held { self.park(row)?;