Skip to content

Fix race condition in state store cache updates #1753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 8, 2025
Merged
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
21 changes: 11 additions & 10 deletions crates/core/src/wasm_runtime/state_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ where
.map_err(Into::into)?
.ok_or_else(|| StateStoreError::MissingContract(*key))?;
}
self.store
.store(*key, state.clone())
.await
.map_err(Into::into)?;
// Update memory cache first to prevent race condition where GET requests
// could read stale cached data while persistent store is being updated
let cost = state.size() as i64;
self.state_mem_cache.insert(*key, state, cost).await;
self.state_mem_cache.insert(*key, state.clone(), cost).await;

// Then update persistent store
self.store.store(*key, state).await.map_err(Into::into)?;
Ok(())
}

Expand All @@ -99,12 +100,12 @@ where
state: WrappedState,
params: Parameters<'static>,
) -> Result<(), StateStoreError> {
self.store
.store(key, state.clone())
.await
.map_err(Into::into)?;
// Update memory cache first to prevent race condition
let cost = state.size() as i64;
self.state_mem_cache.insert(key, state, cost).await;
self.state_mem_cache.insert(key, state.clone(), cost).await;

// Then update persistent stores
self.store.store(key, state).await.map_err(Into::into)?;
self.store
.store_params(key, params.clone())
.await
Expand Down
Loading