Skip to content

Commit 73376d4

Browse files
committed
fix race condition
1 parent 3fa049c commit 73376d4

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

apps/hermes/server/src/state/aggregate.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,23 @@ where
348348
tracing::info!(len = message_states.len(), "Storing Message States.");
349349
self.store_message_states(message_states).await?;
350350

351-
// Update the aggregate state
351+
// First, get the data we need without holding the lock
352+
let message_state_keys_clone = message_state_keys.clone();
353+
let should_prune = {
354+
let aggregate_state = self.into().data.read().await;
355+
match aggregate_state.latest_completed_slot {
356+
None => false,
357+
Some(latest) if slot > latest => true,
358+
_ => false,
359+
}
360+
};
361+
362+
// Do the pruning outside the critical section if needed
363+
if should_prune {
364+
self.prune_removed_keys(message_state_keys_clone).await;
365+
}
366+
367+
// Now acquire the write lock for the state update and event sending
352368
let mut aggregate_state = self.into().data.write().await;
353369

354370
// Atomic check and update
@@ -358,7 +374,6 @@ where
358374
AggregationEvent::New { slot }
359375
}
360376
Some(latest) if slot > latest => {
361-
self.prune_removed_keys(message_state_keys).await;
362377
aggregate_state.latest_completed_slot = Some(slot);
363378
AggregationEvent::New { slot }
364379
}

0 commit comments

Comments
 (0)