Skip to content

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

remove infinite loop

Thiago Araujo da Silvaed55b59e5b09
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
index 3cce2d6dc2..cc9e874bef 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-dal.ts
@@ -136,25 +136,60 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
// Stale-claim recovery: rows whose worker crashed mid-batch never had their
// 'processing' status flipped back, so they're invisible to the drain query.
- // Flip them back to 'retry' with nextRetryAt=NOW() so the next debounce-driven
- // flush picks them up. Attempts is left unchanged — a stale claim is a worker
- // fault, not a delivery failure, and shouldn't burn retry budget.
- const requeueStaleClaims = async (thresholdMs: number): Promise<number> => {
+ // Treat a stale claim as a consumed attempt — a payload that crashes the worker
+ // is indistinguishable from one that fails delivery, and either way must respect
+ // MAX_ATTEMPTS so a poison pill can't loop forever. Rows that hit the cap are
+ // moved to DLQ in the same transaction.
+ const recoverStaleClaims = async (
+ thresholdMs: number,
+ maxAttempts: number
+ ): Promise<{ retried: number; movedToDlq: number }> => {
+ const errorMessage = "stale-claim-recovered: worker did not release lock before threshold";
try {
- const updated = await db(TableName.AuditLogStreamOutbox)
- .where("status", AuditLogStreamOutboxStatus.Processing)
- .andWhereRaw(`"lockedAt" < NOW() - (? || ' milliseconds')::INTERVAL`, [thresholdMs])
- .update({
- status: AuditLogStreamOutboxStatus.Retry,
- nextRetryAt: db.fn.now(),
- lockedAt: null,
- workerId: null,
- lastError: "stale-claim-recovered: worker did not release lock before threshold"
- });
-
- return updated;
+ return await db.transaction(async (tx) => {
+ const staleRows = await tx(TableName.AuditLogStreamOutbox)
+ .where("status", AuditLogStreamOutboxStatus.Processing)
+ .andWhereRaw(`"lockedAt" < NOW() - (? || ' milliseconds')::INTERVAL`, [thresholdMs])
+ .forUpdate()
+ .skipLocked()
+ .select<TAuditLogStreamOutboxRow[]>("*");
+
+ if (staleRows.length === 0) return { retried: 0, movedToDlq: 0 };
+
+ const exhausted: TAuditLogStreamOutboxRow[] = [];
+ const retriable: TAuditLogStreamOutboxRow[] = [];
+ for (const row of staleRows) {
+ if (row.attempts + 1 >= maxAttempts) {
+ exhausted.push(row);
+ } else {
+ retriable.push(row);
+ }
+ }
+
+ if (retriable.length > 0) {
+ await tx(TableName.AuditLogStreamOutbox)
+ .whereIn(
+ "id",
+ retriable.map((row) => row.id)
+ )
+ .update({
+ status: AuditLogStreamOutboxStatus.Retry,
+ attempts: db.raw('"attempts" + 1'),
+ nextRetryAt: tx.fn.now(),
+ lockedAt: null,
+ workerId: null,
+ lastError: errorMessage
+ });
+ }
+
+ if (exhausted.length > 0) {
+ await moveToDlq(exhausted, errorMessage, tx);
+ }
+
+ return { retried: retriable.length, movedToDlq: exhausted.length };
+ });
} catch (error) {
- throw new DatabaseError({ error, name: "AuditLogStreamOutbox: requeueStaleClaims" });
+ throw new DatabaseError({ error, name: "AuditLogStreamOutbox: recoverStaleClaims" });
}
};
@@ -164,6 +199,6 @@ export const auditLogStreamOutboxDALFactory = (db: TDbClient) => {
deleteByIds,
markBatchForRetry,
moveToDlq,
- requeueStaleClaims
+ recoverStaleClaims
};
};
diff --git a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
index a519b9fd4f..40b6b0ffab 100644
--- a/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
+++ b/backend/src/ee/services/audit-log-stream-outbox/audit-log-stream-outbox-service.ts
@@ -238,10 +238,13 @@ export const auditLogStreamOutboxServiceFactory = ({
// forever and never re-enter the drain query. Flip them back to 'retry' so
// the next event for that stream picks them up via the normal debounce path.
const sweepStaleClaims = async () => {
- const recovered = await auditLogStreamOutboxDAL.requeueStaleClaims(STALE_CLAIM_THRESHOLD_MS);
- if (recovered > 0) {
+ const { retried, movedToDlq } = await auditLogStreamOutboxDAL.recoverStaleClaims(
+ STALE_CLAIM_THRESHOLD_MS,
+ MAX_ATTEMPTS
+ );
+ if (retried > 0 || movedToDlq > 0) {
logger.warn(
- `audit-log-stream-outbox: recovered ${recovered} stale claims [thresholdMs=${STALE_CLAIM_THRESHOLD_MS}]`
+ `audit-log-stream-outbox: recovered stale claims [retried=${retried}] [movedToDlq=${movedToDlq}] [thresholdMs=${STALE_CLAIM_THRESHOLD_MS}]`
);
}
};