Skip to content

Commit a46dc69

Browse files
make tryAcquireMutexForSaving saga more resilient against error during refetching mutex
1 parent b1f5918 commit a46dc69

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

frontend/javascripts/viewer/model/sagas/saving/save_mutex_saga.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { ensureWkReady } from "../ready_sagas";
4444
const MUTEX_NOT_ACQUIRED_KEY = "MutexCouldNotBeAcquired";
4545
const MUTEX_ACQUIRED_KEY = "AnnotationMutexAcquired";
4646
const ACQUIRE_MUTEX_INTERVAL = 1000 * 60;
47+
const DELAY_AFTER_FAILED_MUTEX_FETCH = 1000 * 10;
4748
const RETRY_COUNT = 20; // 12 retries with 60/12=5 seconds backup delay
4849
const INITIAL_BACKOFF_TIME = 1000;
4950
const BACKOFF_TIME_MULTIPLIER = 1.5;
@@ -275,16 +276,28 @@ function* tryAcquireMutexForSaving(mutexLogicState: MutexLogicState): Saga<void>
275276
}
276277
// We got the mutex once, now keep it until this saga is cancelled due to saving finished.
277278
while (hasMutex) {
278-
const { canEdit, blockedByUser } = yield* call(acquireAnnotationMutex, annotationId);
279-
if (!canEdit) {
280-
// TODOM: Think of a better way to handle this. This should usually never happen only if a client disconnects for a longer time while saving.
281-
// Maybe its ok to crash / enforce a reload in that case.
282-
// One scenario in which this might happen is when the user disconnects from the internet while having the mutex and later reconnects.
283-
console.error("Failed to continuously acquire mutex.");
279+
try {
280+
const { canEdit, blockedByUser } = yield* call(acquireAnnotationMutex, annotationId);
281+
if (!canEdit) {
282+
// TODOM: Think of a better way to handle this. This should usually never happen only if a client disconnects for a longer time while saving.
283+
// Maybe its ok to crash / enforce a reload in that case.
284+
// One scenario in which this might happen is when the user disconnects from the internet while having the mutex and later reconnects.
285+
throw new Error(
286+
`No longer owner of the annotation mutex. Instead user ${blockedByUser ? `${blockedByUser.firstName} ${blockedByUser?.lastName} (${blockedByUser?.id})` : "unknown user"} has the mutex.`,
287+
);
288+
}
289+
yield* put(setUserHoldingMutexAction(blockedByUser));
290+
yield* put(setIsMutexAcquiredAction(canEdit));
291+
yield* call(delay, ACQUIRE_MUTEX_INTERVAL);
292+
} catch (error) {
293+
console.error("Failed to continuously acquire mutex.", error);
294+
yield* put(setUserHoldingMutexAction(undefined));
295+
yield* put(setIsMutexAcquiredAction(false));
296+
Toast.error(
297+
"Unable to get write-lock needed to update the annotation. Please check your connection to WEBKNOSSOS. See the console for more information. Retrying soon.",
298+
);
299+
yield* call(delay, DELAY_AFTER_FAILED_MUTEX_FETCH);
284300
}
285-
yield* put(setUserHoldingMutexAction(blockedByUser));
286-
yield* put(setIsMutexAcquiredAction(canEdit));
287-
yield* call(delay, ACQUIRE_MUTEX_INTERVAL);
288301
}
289302
}
290303

0 commit comments

Comments
 (0)