generated from nhcarrigan/template
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:
@@ -154,10 +154,10 @@ export const GameProvider = ({ children }: { children: React.ReactNode }): React
|
|||||||
newlyUnlockedRef.current = [];
|
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) {
|
if (Date.now() - lastSaveRef.current >= AUTO_SAVE_INTERVAL_MS) {
|
||||||
lastSaveRef.current = Date.now();
|
lastSaveRef.current = Date.now();
|
||||||
if (stateRef.current) {
|
if (stateRef.current && !isSyncingRef.current) {
|
||||||
void saveGame({
|
void saveGame({
|
||||||
state: stateRef.current,
|
state: stateRef.current,
|
||||||
signature: signatureRef.current ?? undefined,
|
signature: signatureRef.current ?? undefined,
|
||||||
@@ -203,6 +203,7 @@ export const GameProvider = ({ children }: { children: React.ReactNode }): React
|
|||||||
const forceSync = useCallback(async () => {
|
const forceSync = useCallback(async () => {
|
||||||
if (!stateRef.current || isSyncingRef.current) return;
|
if (!stateRef.current || isSyncingRef.current) return;
|
||||||
isSyncingRef.current = true;
|
isSyncingRef.current = true;
|
||||||
|
lastSaveRef.current = Date.now(); // push auto-save timer back so it doesn't fire concurrently
|
||||||
setIsSyncing(true);
|
setIsSyncing(true);
|
||||||
try {
|
try {
|
||||||
const response = await saveGame({
|
const response = await saveGame({
|
||||||
|
|||||||
Reference in New Issue
Block a user