@@ -84,12 +84,13 @@ where
84
84
. map_err ( Into :: into) ?
85
85
. ok_or_else ( || StateStoreError :: MissingContract ( * key) ) ?;
86
86
}
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
91
89
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) ?;
93
94
Ok ( ( ) )
94
95
}
95
96
@@ -99,12 +100,12 @@ where
99
100
state : WrappedState ,
100
101
params : Parameters < ' static > ,
101
102
) -> 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
106
104
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) ?;
108
109
self . store
109
110
. store_params ( key, params. clone ( ) )
110
111
. await
0 commit comments