Skip to content

Commit 676e8c4

Browse files
wpfleger96Hayt
andauthored
fix(relay-admin): make thread deletions atomic and fence expired action leases under row lock (#7853)
## What Two fixes to `crates/buzz-db/src/store/relay_admin_actions.rs` and one cleanup in `crates/buzz-db/src/store/thread.rs`. ### 1. Thread counter atomicity The admin delete action now calls the canonical `soft_delete_event_and_update_thread_in_tx` body shared with NIP-29 and NIP-09 event-ID deletion. The delete, parent `reply_count` decrement, root `descendant_count` decrement, and step marker commit or roll back together in one transaction. A second action against an already-deleted target is a no-op and does not double-decrement. After a committed delete, `emit_live_thread_summary` refreshes the live thread count the same way `DELETE_EVENT` does. ### 2. Lock-first lease expiry fence (all five admin-action paths) On main, the final marker `UPDATE` in every admin-action path (`execute_ban_with_marker`, `execute_timeout_with_marker`, `execute_kick_with_marker`, `execute_delete_with_marker`, `record_failure`) checks `clock_timestamp()` in the `WHERE` clause. PostgreSQL can evaluate that predicate before waiting to lock the target row. If another transaction holds the action row and releases it unchanged after the lease expires, the update commits under the stale timestamp. On this branch, each path acquires the action row with `SELECT … FOR UPDATE` inside the same transaction before any domain write. The wall-clock expiry check in the marker `UPDATE` runs after that lock is held, not before. The pre-entry ownership `SELECT EXISTS` checks remain — they are early-rejection optimizations that are safe because the correct final fence rolls back any provisional work. `record_failure` on main uses a single-statement `execute(pool)`. On this branch it begins a transaction, acquires the row lock with `SELECT … FOR UPDATE` and an explicit expiry check, then writes `state = 'failed'` — matching the four mutation functions. **Lease validity is checked at the protected ownership check under the action-row lock, not at physical `COMMIT` completion.** | Function | Change | |---|---| | `execute_ban_with_marker` | `SELECT … FOR UPDATE` added after pre-entry check, before domain write | | `execute_timeout_with_marker` | same | | `execute_kick_with_marker` | same | | `execute_delete_with_marker` | same | | `record_failure` | wrapped in transaction; `SELECT … FOR UPDATE` + clock check before state update | ### 3. Unused standalone counter helpers removed `increment_reply_count`, `decrement_reply_count` (both in `thread.rs`), and `Db::decrement_reply_count` have no production callers outside their forwarding wrappers. Both perform unguarded autocommit writes that bypass the deletion-transition/atomicity invariant. Removed to prevent future misuse. --------- Signed-off-by: Will Pfleger <[email protected]> Signed-off-by: Hayt <211b96e6a2b7f45fd4047988976c7bbbeeda0c15f3ae7b32eec20834b5a55118@buzz.block.builderlab.xyz> Co-authored-by: Hayt <211b96e6a2b7f45fd4047988976c7bbbeeda0c15f3ae7b32eec20834b5a55118@buzz.block.builderlab.xyz>
1 parent 8f8c4df commit 676e8c4

4 files changed

Lines changed: 652 additions & 146 deletions

File tree

‎crates/buzz-db/src/store/event.rs‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -995,14 +995,37 @@ pub async fn soft_delete_event_and_update_thread(
995995
parent_event_id: Option<&[u8]>,
996996
root_event_id: Option<&[u8]>,
997997
) -> Result<bool> {
998-
use crate::store::replaceable::event_replacement_lock_key;
999-
1000998
let connection = crate::observability::acquire_writer(
1001999
pool,
10021000
crate::observability::WriterOperation::EventWrite,
10031001
)
10041002
.await?;
10051003
let mut tx = sqlx::Transaction::begin(connection, None).await?;
1004+
let deleted = soft_delete_event_and_update_thread_in_tx(
1005+
&mut tx,
1006+
community_id,
1007+
event_id,
1008+
parent_event_id,
1009+
root_event_id,
1010+
)
1011+
.await?;
1012+
tx.commit().await?;
1013+
Ok(deleted)
1014+
}
1015+
1016+
/// Transaction-scoped body of [`soft_delete_event_and_update_thread`].
1017+
///
1018+
/// Callers that must fence the delete with their own writes (e.g. the admin
1019+
/// action lease/marker) run this inside their transaction; the caller commits.
1020+
pub(crate) async fn soft_delete_event_and_update_thread_in_tx(
1021+
tx: &mut PgConnection,
1022+
community_id: CommunityId,
1023+
event_id: &[u8],
1024+
parent_event_id: Option<&[u8]>,
1025+
root_event_id: Option<&[u8]>,
1026+
) -> Result<bool> {
1027+
use crate::store::replaceable::event_replacement_lock_key;
1028+
10061029
// Derive the target event's kind and channel_id inside the transaction so
10071030
// that the serialization decision cannot be bypassed by any caller.
10081031
let target: Option<(i32, Option<Uuid>)> = sqlx::query_as(
@@ -1065,7 +1088,6 @@ pub async fn soft_delete_event_and_update_thread(
10651088
}
10661089
}
10671090

1068-
tx.commit().await?;
10691091
Ok(deleted)
10701092
}
10711093

0 commit comments

Comments
 (0)