Ensure async/stateful flows are race-free by avoiding overlapping work for the same logical key and by handling events that arrive before the relevant state is ready.
Practical rules:
path, session_id, conversation_id) can be triggered concurrently, allow only one in-flight async task per key. Queue subsequent updates and apply them sequentially after the current task completes.Example (per-key queue to prevent stale overwrites):
// pseudo-pattern
if let Some(queued) = pending_updates.get_mut(&key) {
queued.push(update);
return;
}
pending_updates.insert(key.clone(), Vec::new());
let rules = current_rules.clone();
spawn(async move {
process_update(update, rules, key.clone()).await
}, move |me, result, ctx| {
me.apply(result);
me.drain_pending_updates(&key, ctx); // sequentially process queued updates
});
Enter the URL of a public GitHub repository