Skip to content

Commit 2bc3144

Browse files
fix(infra): report actual backup tar attempt count in failure message (#119197)
Punchcard-Session: quiet-orchard-orchard-9t Co-authored-by: Peter Steinberger <[email protected]>
1 parent 58aab74 commit 2bc3144

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎src/infra/backup-create.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,37 @@ describe("writeTarArchiveWithRetry", () => {
372372
expect(sleep).not.toHaveBeenCalled();
373373
});
374374

375+
it("reports the actual attempt count when bailing out before the retry limit", async () => {
376+
const nonEofErr = new Error("permission denied");
377+
const eofErr = Object.assign(new Error("did not encounter expected EOF"), {
378+
path: "/state/logs/gateway.jsonl",
379+
});
380+
const sleep = vi.fn<(ms: number) => Promise<void>>().mockResolvedValue(undefined);
381+
382+
const singleAttempt = vi.fn<() => Promise<void>>().mockRejectedValue(nonEofErr);
383+
await expect(
384+
writeTarArchiveWithRetry({
385+
tempArchivePath: "/tmp/backup.tar.gz.tmp",
386+
runTar: singleAttempt,
387+
sleepMs: sleep,
388+
}),
389+
).rejects.toThrow(/after 1 attempt\)/);
390+
expect(singleAttempt).toHaveBeenCalledOnce();
391+
392+
const twoAttempts = vi
393+
.fn<() => Promise<void>>()
394+
.mockRejectedValueOnce(eofErr)
395+
.mockRejectedValueOnce(nonEofErr);
396+
await expect(
397+
writeTarArchiveWithRetry({
398+
tempArchivePath: "/tmp/backup.tar.gz.tmp",
399+
runTar: twoAttempts,
400+
sleepMs: sleep,
401+
}),
402+
).rejects.toThrow(/after 2 attempts\)/);
403+
expect(twoAttempts).toHaveBeenCalledTimes(2);
404+
});
405+
375406
it("retries on EOF-class errors and eventually succeeds", async () => {
376407
const eofErr = Object.assign(new Error("did not encounter expected EOF"), {
377408
path: "/state/sessions/s-abc/transcript.jsonl",

‎src/infra/backup-tar-retry.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export async function writeTarArchiveWithRetry<T>(params: {
3131
}): Promise<T> {
3232
const sleepFn = params.sleepMs ?? sleep;
3333
let lastErr: unknown;
34+
let attempts = 0;
3435
for (let attempt = 1; attempt <= BACKUP_TAR_MAX_ATTEMPTS; attempt += 1) {
36+
attempts = attempt;
3537
const attemptTempArchivePath = resolveBackupTarAttemptTempPath(params.tempArchivePath, attempt);
3638
try {
3739
return await params.runTar(attemptTempArchivePath);
@@ -54,8 +56,9 @@ export async function writeTarArchiveWithRetry<T>(params: {
5456
}
5557
const final = lastErr instanceof Error ? lastErr : new Error(String(lastErr));
5658
const offendingPath = (lastErr as NodeJS.ErrnoException | undefined)?.path;
59+
const attemptSuffix = `after ${attempts} attempt${attempts === 1 ? "" : "s"}`;
5760
const suffix = offendingPath
58-
? ` (last offending path: ${offendingPath}, after ${BACKUP_TAR_MAX_ATTEMPTS} attempts)`
59-
: ` (after ${BACKUP_TAR_MAX_ATTEMPTS} attempts)`;
61+
? ` (last offending path: ${offendingPath}, ${attemptSuffix})`
62+
: ` (${attemptSuffix})`;
6063
throw new Error(`Backup archive write failed: ${final.message}${suffix}`, { cause: final });
6164
}

0 commit comments

Comments
 (0)