Skip to content

Commit d410901

Browse files
Ian Clarkeclaude
andcommitted
fix: update memory cache before persistent store to prevent race condition
When multiple concurrent operations access the state store, there was a race condition where GET requests could read stale cached data while the persistent store was being updated. This fix ensures the memory cache is updated first, preventing other threads from reading stale data during the update window. This change affects both update() and store() methods in StateStore. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0225d5b commit d410901

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

crates/core/src/wasm_runtime/state_store.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ where
8484
.map_err(Into::into)?
8585
.ok_or_else(|| StateStoreError::MissingContract(*key))?;
8686
}
87-
self.store
88-
.store(*key, state.clone())
89-
.await
90-
.map_err(Into::into)?;
87+
// Update memory cache first to prevent race condition where GET requests
88+
// could read stale cached data while persistent store is being updated
9189
let cost = state.size() as i64;
92-
self.state_mem_cache.insert(*key, state, cost).await;
90+
self.state_mem_cache.insert(*key, state.clone(), cost).await;
91+
92+
// Then update persistent store
93+
self.store.store(*key, state).await.map_err(Into::into)?;
9394
Ok(())
9495
}
9596

@@ -99,12 +100,12 @@ where
99100
state: WrappedState,
100101
params: Parameters<'static>,
101102
) -> Result<(), StateStoreError> {
102-
self.store
103-
.store(key, state.clone())
104-
.await
105-
.map_err(Into::into)?;
103+
// Update memory cache first to prevent race condition
106104
let cost = state.size() as i64;
107-
self.state_mem_cache.insert(key, state, cost).await;
105+
self.state_mem_cache.insert(key, state.clone(), cost).await;
106+
107+
// Then update persistent stores
108+
self.store.store(key, state).await.map_err(Into::into)?;
108109
self.store
109110
.store_params(key, params.clone())
110111
.await

0 commit comments

Comments
 (0)