Compare commits

...
Author SHA1 Message Date
jos-alves 4d11924224 fix(archive): retry dropIfEmpty's cleanup delete on a Postgres deadlock (BIT-31680)
event.deleteMany's existence check can get picked as the deadlock victim
against a concurrent write to the same event (P2034); Postgres's own advice
is to retry, so a couple of immediate attempts happen before giving up.

Paperclip-Agent: José Alves (urlKey jos-alves; id 11179220-3cec-4f0f-880d-2771ff64fce8)
Paperclip-Issue: BIT-31680
Paperclip-Run: 67fc6a6b-c1ea-4b5c-bee0-4a3f0aa7b931
2026-10-21 23:11:14 +01:00
spark 06a4a9dd4a Merge pull request 'Fix the app tests that keep main red (LocalFtp, App)' (#107) from task/main-red-app-tests into main
ci / rust (push) Successful in 15m22s
ci / typescript (push) Successful in 17m50s
2026-10-21 22:53:50 +01:00
2 changed files with 72 additions and 3 deletions

No files matched your search

+13 -3
View File
@@ -946,12 +946,22 @@ export class ArchiveService {
* `setFolderEvent` landing in the gap after the count was read) get silently taken back out by the delete, and
* then fail with a foreign key error moving its photos in here — the "empty" check now lives in the delete's own
* `WHERE`, so Postgres re-reads it against whatever just committed (BIT-31531).
*
* That same concurrent commit can instead make Postgres pick this as the deadlock victim (P2034) rather than
* block it: its own advice is to retry, so a couple of immediate attempts happen here before giving up (BIT-31680).
*/
private async dropIfEmpty(eventId: string): Promise<void> {
const { db } = this.deps;
await db.event.deleteMany({
where: { id: eventId, archivedAt: { not: null }, photos: { none: {} }, cloudFolders: { none: {} }, galleries: { none: {} }, deliveries: { none: {} } },
});
for (let attempt = 1; ; attempt++) {
try {
await db.event.deleteMany({
where: { id: eventId, archivedAt: { not: null }, photos: { none: {} }, cloudFolders: { none: {} }, galleries: { none: {} }, deliveries: { none: {} } },
});
return;
} catch (err) {
if ((err as { code?: string }).code !== "P2034" || attempt >= MAX_ATTEMPTS) throw err;
}
}
}
// --- indexing --------------------------------------------------------------------------------------------------
+59
View File
@@ -851,6 +851,65 @@ describe("archive from a Nextcloud", () => {
expect(gamePhotos.every((p) => p.eventId === galaEventId)).toBe(true);
});
it("retries a cleanup delete that Postgres picked as a deadlock victim (BIT-31680)", async () => {
const source = await connect();
cloud.authorised = true;
await ctx.app.inject({ method: "GET", url: `/v1/cloud-sources/${source.id}`, headers: bearer(token) });
await worker.scan(source.id);
for (const id of await worker.claim(10)) await worker.index(id);
const gameFolder = await ctx.db.cloudFolder.findFirstOrThrow({ where: { path: { endsWith: "20250913 - SCP x ABC" } } });
const galaFolder = await ctx.db.cloudFolder.findFirstOrThrow({ where: { path: { endsWith: "Gala 20250614" } } });
const galaEventId = galaFolder.eventId!;
let misses = 2;
const gated = ctx.db.$extends({
query: {
event: {
async deleteMany({ args, query }) {
if (args.where?.id === galaEventId && misses > 0) {
misses--;
throw Object.assign(new Error("Transaction failed due to a write conflict or a deadlock. Please retry your transaction"), { code: "P2034" });
}
return query(args);
},
},
},
});
const flakyWorker = new ArchiveService({ db: gated as unknown as TestContext["db"], secrets: new SecretBox(KEY), log: quiet, fetchImpl: fetchProxy });
await flakyWorker.setFolderEvent(galaFolder.id, { eventId: gameFolder.eventId! });
expect(misses).toBe(0);
expect(await ctx.db.event.findUnique({ where: { id: galaEventId } })).toBeNull();
});
it("gives up after repeated deadlocks on a cleanup delete (BIT-31680)", async () => {
const source = await connect();
cloud.authorised = true;
await ctx.app.inject({ method: "GET", url: `/v1/cloud-sources/${source.id}`, headers: bearer(token) });
await worker.scan(source.id);
for (const id of await worker.claim(10)) await worker.index(id);
const gameFolder = await ctx.db.cloudFolder.findFirstOrThrow({ where: { path: { endsWith: "20250913 - SCP x ABC" } } });
const galaFolder = await ctx.db.cloudFolder.findFirstOrThrow({ where: { path: { endsWith: "Gala 20250614" } } });
const galaEventId = galaFolder.eventId!;
const gated = ctx.db.$extends({
query: {
event: {
async deleteMany({ args, query }) {
if (args.where?.id === galaEventId) throw Object.assign(new Error("Transaction failed due to a write conflict or a deadlock. Please retry your transaction"), { code: "P2034" });
return query(args);
},
},
},
});
const stuckWorker = new ArchiveService({ db: gated as unknown as TestContext["db"], secrets: new SecretBox(KEY), log: quiet, fetchImpl: fetchProxy });
await expect(stuckWorker.setFolderEvent(galaFolder.id, { eventId: gameFolder.eventId! })).rejects.toMatchObject({ code: "P2034" });
expect(await ctx.db.event.findUnique({ where: { id: galaEventId } })).not.toBeNull();
});
it("asks again when the cloud revokes the app password", async () => {
const source = await connect();
cloud.authorised = true;