fix: prevent auto-save race condition during force sync

Skip auto-save when isSyncingRef is true and reset the auto-save timer
at the start of forceSync to prevent concurrent saves from sharing the
same HMAC signature and causing alternating save failures.
This commit is contained in:
2026-03-06 19:30:38 -08:00
committed by Naomi Carrigan
parent 5aae3eb389
commit 4e32709e07
+3 -2
View File
@@ -154,10 +154,10 @@ export const GameProvider = ({ children }: { children: React.ReactNode }): React
newlyUnlockedRef.current = [];
}
// Auto-save every 30 seconds
// Auto-save every 30 seconds (skip if a force sync is in-flight to avoid signature collisions)
if (Date.now() - lastSaveRef.current >= AUTO_SAVE_INTERVAL_MS) {
lastSaveRef.current = Date.now();
if (stateRef.current) {
if (stateRef.current && !isSyncingRef.current) {
void saveGame({
state: stateRef.current,
signature: signatureRef.current ?? undefined,
@@ -203,6 +203,7 @@ export const GameProvider = ({ children }: { children: React.ReactNode }): React
const forceSync = useCallback(async () => {
if (!stateRef.current || isSyncingRef.current) return;
isSyncingRef.current = true;
lastSaveRef.current = Date.now(); // push auto-save timer back so it doesn't fire concurrently
setIsSyncing(true);
try {
const response = await saveGame({