diff --git a/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/distributed_lock_manager/lock_manager_client.ts b/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/distributed_lock_manager/lock_manager_client.ts index 18412a17bde30..8fedfd81d978e 100644 --- a/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/distributed_lock_manager/lock_manager_client.ts +++ b/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/distributed_lock_manager/lock_manager_client.ts @@ -285,20 +285,26 @@ export async function withLock( `Lock "${lockId}" acquired. Extending TTL every ${prettyMilliseconds(extendInterval)}` ); + let extendTTlPromise = Promise.resolve(true); const intervalId = setInterval(() => { - lockManager.extendTtl().catch((err) => { - logger.error(`Failed to extend lock "${lockId}":`, err); - }); + // wait for the previous extendTtl request to finish before sending the next one. This is to avoid flooding ES with extendTtl requests in cases where ES is slow to respond. + extendTTlPromise = extendTTlPromise + .then(() => lockManager.extendTtl()) + .catch((err) => { + logger.error(`Failed to extend lock "${lockId}":`, err); + return false; + }); }, extendInterval); try { return await callback(); } finally { - clearInterval(intervalId); try { + clearInterval(intervalId); + await extendTTlPromise; await lockManager.release(); } catch (error) { - logger.error(`Failed to release lock "${lockId}": ${error.message}`); + logger.error(`Failed to release lock "${lockId}" in withLock: ${error.message}`); } } }