Skip to content

Commit 3f30d53

Browse files
sanityclaudeIan Clarke
authored
Fix race condition in state store cache updates (#1753)
Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Ian Clarke <ian@freenet.org>
1 parent 49461bb commit 3f30d53

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)