Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(secret-sync): fix race condition in daily retry for failed syncs

Igor Correa922cf03ef3a3
diff --git a/backend/src/services/secret-sync/secret-sync-dal.ts b/backend/src/services/secret-sync/secret-sync-dal.ts
index cf39e8d839..9a256b4f0a 100644
--- a/backend/src/services/secret-sync/secret-sync-dal.ts
+++ b/backend/src/services/secret-sync/secret-sync-dal.ts
@@ -207,6 +207,22 @@ export const secretSyncDALFactory = (
}
};
+ const updateAndReturnIds = async (
+ filter: Parameters<(typeof secretSyncOrm)["find"]>[0],
+ data: Parameters<(typeof secretSyncOrm)["update"]>[1],
+ tx?: Knex
+ ): Promise<string[]> => {
+ try {
+ const rows = await (tx || db)(TableName.SecretSync)
+ .where(buildFindFilter(filter))
+ .update(data)
+ .returning("id");
+ return rows.map((r) => r.id);
+ } catch (error) {
+ throw new DatabaseError({ error, name: "Update And Return IDs - Secret Sync" });
+ }
+ };
+
const findByDestinationAndOrgId = async (destination: string, orgId: string, tx?: Knex) => {
try {
const response = await (tx || db.replicaNode())(TableName.SecretSync)
@@ -221,5 +237,5 @@ export const secretSyncDALFactory = (
}
};
- return { ...secretSyncOrm, findById, findOne, find, create, updateById, findByDestinationAndOrgId };
+ return { ...secretSyncOrm, findById, findOne, find, create, updateById, updateAndReturnIds, findByDestinationAndOrgId };
};
diff --git a/backend/src/services/secret-sync/secret-sync-queue.ts b/backend/src/services/secret-sync/secret-sync-queue.ts
index 2e626c7fa4..8f6819bb31 100644
--- a/backend/src/services/secret-sync/secret-sync-queue.ts
+++ b/backend/src/services/secret-sync/secret-sync-queue.ts
@@ -1182,25 +1182,21 @@ export const secretSyncQueueFactory = ({
});
const queueDailyRetryForFailedSyncs = async () => {
- const failedSyncs = await secretSyncDAL.find({
- syncStatus: SecretSyncStatus.Failed,
- isAutoSyncEnabled: true,
- $in: { destination: [...SECRET_SYNC_DAILY_RETRY_DESTINATIONS] }
- });
-
- if (!failedSyncs.length) return;
-
- await secretSyncDAL.update(
+ const updatedIds = await secretSyncDAL.updateAndReturnIds(
{
- $in: { id: failedSyncs.map((sync) => sync.id) }
+ syncStatus: SecretSyncStatus.Failed,
+ isAutoSyncEnabled: true,
+ $in: { destination: [...SECRET_SYNC_DAILY_RETRY_DESTINATIONS] }
},
{ syncStatus: SecretSyncStatus.Pending }
);
+ if (!updatedIds.length) return;
+
await Promise.all(
- failedSyncs.map((sync) =>
+ updatedIds.map((syncId) =>
queueSecretSyncSyncSecretsById({
- syncId: sync.id
+ syncId
})
)
);