From fd0d63bda322ece433027a6f61b1ba5ed448b966 Mon Sep 17 00:00:00 2001 From: kikootwo Date: Tue, 27 Oct 2026 01:49:38 -0400 Subject: [PATCH 1/2] fix: the seeding cleanup removes a torrent shared by finished requests (#283) The cleanup job's shared-torrent check counted every non-deleted request on the torrent, so several finished requests (a series pack) kept it for each other forever. In the cleanup job, another request now keeps the torrent unless it is finished (audiobook available, ebook downloaded), recorded for the configured client, and done seeding by its own indexer's rule. When the torrent goes, the other requests' rows are marked removed too. Request deletion keeps today's rule: any other active request keeps the torrent. Co-Authored-By: Claude Opus 5.5 --- .../admin-features/request-deletion.md | 15 +- documentation/phase3/download-clients.md | 2 +- documentation/phase3/file-organization.md | 4 +- .../cleanup-seeded-torrents.processor.ts | 35 ++-- src/lib/utils/shared-torrent.ts | 94 +++++++-- .../cleanup-seeded-torrents.shared.test.ts | 197 ++++++++++++++++++ tests/services/request-delete.seeding.test.ts | 18 ++ 7 files changed, 336 insertions(+), 29 deletions(-) create mode 100644 tests/processors/cleanup-seeded-torrents.shared.test.ts diff --git a/documentation/admin-features/request-deletion.md b/documentation/admin-features/request-deletion.md index 4f4987e00..14ce6e0c5 100644 --- a/documentation/admin-features/request-deletion.md +++ b/documentation/admin-features/request-deletion.md @@ -92,8 +92,8 @@ model Request { (src/lib/utils/seeding-policy.ts, same rule as cleanup_seeded_torrents: every set limit met; 0 turns a limit off; unreported ratio with ratioLimit > 0 = not met) IF another active request uses the same torrentHash (otherActiveRequestsUsingTorrent, - src/lib/utils/shared-torrent.ts, shared with the cleanup job): - → Keep torrent → torrentsKeptShared++ + src/lib/utils/shared-torrent.ts), whatever its status, even finished and done seeding: + → Keep torrent → torrentsKeptShared++ (the cleanup job may remove it later, see below) ELSE: → Delete torrent + files → torrentsRemoved++ → row downloadStatus = 'removed' @@ -162,6 +162,12 @@ where: { - **Soft-deleted requests:** Delete torrent + hard-delete request when seeding complete - **Unlimited seeding:** Hard-delete orphaned request immediately (no monitoring) - **Not RMAB's torrent:** never removed; a soft-deleted request is hard-deleted, the torrent stays. Details: [scheduler.md](../backend/services/scheduler.md) (cleanup_seeded_torrents) +- **Shared torrent (#283):** after ownership and this row's seeding check, `otherActiveRequestsSharingTorrent()` lists the other non-deleted requests on the torrent (same hash forms as the delete guard). Each keeps it (logged with the reason) unless `siblingKeepsTorrent()` (`src/lib/utils/shared-torrent.ts`) finds it: + - finished: `FINISHED_REQUESTS` (audiobook `available`, ebook `downloaded`; also the candidate query's statuses). Any other status keeps it (`downloading`, `processing`, audiobook `downloaded`, `failed`, `cancelled`, ...) + - every row it has for the torrent recorded for the configured client type + - its own indexer's requirement met (`evaluateSeeding()` with its row's indexer, by the same probe answer); unlimited or unconfigured indexer keeps it + - None keeps it → one `deleteDownload`, this row `removed` (or soft-deleted: hard-deleted), the siblings' rows `removed` in one `updateMany` (also rows RMAB did not add, which never reach the guard themselves). Same run: `deletedHashes`; later runs: the probe's not-found → `removed` + - Soft-deleted requests never keep it (unchanged) ### API Endpoint @@ -288,6 +294,11 @@ where: { - **Issue:** a release already in the client (added by hand or another app) was grabbed by its existing hash; deleting the request (or the seeding cleanup) then removed it with its files - **Fix:** `DownloadHistory.addedByRmab` recorded at the grab; delete and cleanup act only on RMAB's torrents (`resolveOwnership()`); rows from before the fix are judged by the client's added time +**6. Torrent Shared by Several Finished Requests Never Cleaned Up (#283)** +- **Issue:** a series pack (or re-request) used by several finished requests stayed in the client forever +- **Cause:** the cleanup job's shared-torrent check counted every non-deleted sibling, so each finished request kept it for the others +- **Fix:** cleanup job only: a sibling keeps it unless finished and done seeding by its own indexer's rule (`siblingKeepsTorrent()`); request deletion unchanged + ## File Structure ``` diff --git a/documentation/phase3/download-clients.md b/documentation/phase3/download-clients.md index e0e317b4c..158c77e0d 100644 --- a/documentation/phase3/download-clients.md +++ b/documentation/phase3/download-clients.md @@ -197,7 +197,7 @@ finalPath = config.customPath ? path.join(downloadDir, config.customPath) : down - Bull retry after an add that succeeded (lost response, history insert/enqueue failed, worker died) or two download jobs for one request → RMAB's own torrent recorded `false` - Admin retry-download (`src/app/api/admin/requests/[id]/retry-download/route.ts`) re-adding a stored link while RMAB's torrent is still there → recorded `false` - Re-request/replace/series pack of a hash whose owner row was hard-deleted - - A `false` re-request row counts as sharing the torrent (`otherActiveRequestsUsingTorrent`), so cleanup hard-deletes the soft-deleted owner row and keeps the torrent; the `false` row is never cleaned (same "owner row hard-deleted" leak, caused by RMAB's own cleanup) + - A `false` re-request row that is not finished or not done seeding counts as sharing the torrent (#283, [request-deletion.md](../admin-features/request-deletion.md)), so cleanup hard-deletes the soft-deleted owner row and keeps the torrent; the `false` row is never cleaned (same "owner row hard-deleted" leak, caused by RMAB's own cleanup). Finished and done seeding: the owner row removes it and marks the `false` row `removed` - Switching to a new client instance of the same type: its well-formed "not here" answers mark those rows `removed` for good (nothing clears the mark), so RMAB never cleans them after the torrents are migrated - Client migrated (added times reset) or without added time (legacy rows); clock drift between client and DB > 5 min - Deluge loads torrents asynchronously at startup (`load_state` → `add_async`), so a probe during startup can miss a torrent → row marked `removed` (never cleaned, never lost); Transmission 4.0.6 loads all torrents in one session-thread task before RPC answers (no partial list), 3.x not verified diff --git a/documentation/phase3/file-organization.md b/documentation/phase3/file-organization.md index b2a163e31..d9f742231 100644 --- a/documentation/phase3/file-organization.md +++ b/documentation/phase3/file-organization.md @@ -271,8 +271,8 @@ exiftool "audiobook.m4b" | grep -i asin 1. Find requests with status 'available' or soft-deleted (orphaned downloads) 2. Query qBittorrent for actual `seeding_time` field 3. **CRITICAL: Check if torrent hash is shared by other active requests** - - If yes → Skip torrent deletion, only hard-delete the soft-deleted request record - - If no → Delete torrent + files + - Any of them not finished, or not done seeding by its own indexer (#283) → Skip torrent deletion, only hard-delete the soft-deleted request record + - Else → Delete torrent + files, mark every sharing row removed ([request-deletion.md](../admin-features/request-deletion.md)) 4. Delete torrent + files only after seeding requirement met 5. Respects config (0 = never cleanup) diff --git a/src/lib/processors/cleanup-seeded-torrents.processor.ts b/src/lib/processors/cleanup-seeded-torrents.processor.ts index 476cbd99c..e1165c544 100644 --- a/src/lib/processors/cleanup-seeded-torrents.processor.ts +++ b/src/lib/processors/cleanup-seeded-torrents.processor.ts @@ -13,7 +13,7 @@ import { prisma } from '../db'; import { RMABLogger } from '../utils/logger'; import { CLIENT_PROTOCOL_MAP, DownloadClientType, IDownloadClient } from '../interfaces/download-client.interface'; import { evaluateSeeding, isUnlimitedSeeding } from '../utils/seeding-policy'; -import { otherActiveRequestsUsingTorrent } from '../utils/shared-torrent'; +import { FINISHED_REQUESTS, otherActiveRequestsSharingTorrent, siblingKeepsTorrent } from '../utils/shared-torrent'; import { createIndexerConfigLookup, describeIgnoredIndexerId, parseSavedIndexers } from '../utils/indexer-config'; import { knownNotOwned, resolveOwnership } from '../utils/torrent-ownership'; @@ -28,16 +28,14 @@ const MAX_PROBE_ERRORS_IN_A_ROW = 3; // Completed requests + soft-deleted requests (orphaned downloads), each with a finished selected download. // IMPORTANT: Only cleanup requests that are truly complete and not being actively processed. -// NOTE: Multiple requests can share the same torrent hash (e.g., re-requesting same audiobook); -// before deleting a torrent we check if other active requests are using it. +// NOTE: Multiple requests can share the same torrent hash (e.g., re-requesting same audiobook, a series pack); +// a torrent is deleted only when every other active request using it is finished and done seeding too (#283). // NOTE: Ebooks downloaded via indexer search use torrent clients and need seeding cleanup too. // Direct HTTP ebook downloads are naturally skipped (no torrent hash / unknown client type). const CANDIDATES = { OR: [ - // Audiobook requests that are fully available (matched in Plex/ABS) - { type: 'audiobook', status: 'available', deletedAt: null }, - // Ebook requests that are fully downloaded (terminal state for ebooks) - { type: 'ebook', status: 'downloaded', deletedAt: null }, + // Finished: audiobook available (matched in Plex/ABS), ebook downloaded (terminal state for ebooks) + ...FINISHED_REQUESTS.map((finished) => ({ ...finished, deletedAt: null })), // Soft-deleted requests of any type (orphaned downloads) { deletedAt: { not: null } }, ], @@ -235,10 +233,17 @@ export async function processCleanupSeededTorrents(payload: CleanupSeededTorrent logger.info(`Download ${downloadInfo.name} (${indexerName}) has met seeding requirement: ${progress}`); - // CRITICAL: Check if any other active (non-deleted) request is using this same download - const otherActiveRequests = await otherActiveRequestsUsingTorrent(request.id, downloadHistory.torrentHash); - if (otherActiveRequests.length > 0) { - logger.info(`Skipping download deletion - ${otherActiveRequests.length} other active request(s) still using this download (IDs: ${otherActiveRequests.map(r => r.id).join(', ')})`); + // CRITICAL: every other active (non-deleted) request using this download must be finished, and + // done seeding by its own indexer's rule (same client answer); any other one keeps it + const siblings = await otherActiveRequestsSharingTorrent(request.id, downloadHistory.torrentHash); + const keeping = siblings.flatMap((sibling) => { + const reason = siblingKeepsTorrent(sibling, clientType, (row) => evaluateSeeding( + lookupIndexer({ indexerId: row.indexerId, indexerName: row.indexerName ?? '', protocol }).config, downloadInfo + )); + return reason ? [`${sibling.id} (${reason})`] : []; + }); + if (keeping.length > 0) { + logger.info(`Skipping download deletion - ${keeping.length} other active request(s) still using this download: ${keeping.join(', ')}`); // If this is a soft-deleted request, hard delete it but DON'T delete the download if (request.deletedAt) { @@ -249,9 +254,15 @@ export async function processCleanupSeededTorrents(payload: CleanupSeededTorrent return 'seeding'; } - // Safe to delete - no other active requests using this download + // Safe to delete - no other active request needs this download await client.deleteDownload(clientId, true); // true = delete files deletedHashes.add(clientId.toLowerCase()); + if (siblings.length > 0) { + // Their rows say the torrent is gone too (a row RMAB did not add never gets here on its own) + const siblingRowIds = siblings.flatMap((sibling) => sibling.downloadHistory.map((row) => row.id)); + await prisma.downloadHistory.updateMany({ where: { id: { in: siblingRowIds } }, data: { downloadStatus: 'removed' } }); + logger.info(`Download was shared with ${siblings.length} finished request(s) (${siblings.map((s) => s.id).join(', ')}): marked removed for them too`); + } // If this is a soft-deleted request (orphaned download), hard delete it now if (request.deletedAt) { diff --git a/src/lib/utils/shared-torrent.ts b/src/lib/utils/shared-torrent.ts index 0d7e05d1c..3d301257e 100644 --- a/src/lib/utils/shared-torrent.ts +++ b/src/lib/utils/shared-torrent.ts @@ -2,23 +2,57 @@ * Component: Shared Torrent Guard * Documentation: documentation/admin-features/request-deletion.md * - * Several requests can share one torrent (e.g. re-requesting the same book). - * Before a torrent is removed from the client, every caller (scheduled - * seeding cleanup, request deletion) checks that no other active request - * still uses it. + * Several requests can share one torrent (e.g. re-requesting the same book, a series pack). + * Before a torrent is removed from the client, every caller checks the other active requests + * that use it. Request deletion: any of them keeps it. Scheduled seeding cleanup: each of them + * keeps it unless it is finished and its own indexer's seeding requirement is met (#283). */ import { prisma } from '../db'; import { torrentHashForms } from '../integrations/torrent-ids'; +import type { SeedingEvaluation } from './seeding-policy'; export interface ActiveRequestRef { id: string; status: string; } +/** Finished requests (the seeding cleanup's active candidates): nothing more will read the torrent */ +export const FINISHED_REQUESTS = [ + { type: 'audiobook', status: 'available' }, + { type: 'ebook', status: 'downloaded' }, +] as const; + +/** Another active request using the torrent, with its selected row(s) for that torrent */ +export interface TorrentSibling extends ActiveRequestRef { + type: string; + downloadHistory: TorrentSiblingDownload[]; +} + +export interface TorrentSiblingDownload { + id: string; + indexerId: number | null; + indexerName: string | null; + downloadClient: string | null; +} + +function otherActiveRequestsWhere(requestId: string, torrentHash: string) { + return { + id: { not: requestId }, + deletedAt: null, + downloadHistory: { + some: { + torrentHash: { in: torrentHashForms(torrentHash) }, + selected: true, + }, + }, + }; +} + /** * Active (not deleted) requests other than `requestId` whose selected * download is the torrent `torrentHash` (hex and base32 forms, in either letter case, match each other). Empty when there is no hash. + * Request deletion keeps the torrent while this is not empty. */ export async function otherActiveRequestsUsingTorrent( requestId: string, @@ -26,16 +60,52 @@ export async function otherActiveRequestsUsingTorrent( ): Promise { if (!torrentHash) return []; return prisma.request.findMany({ - where: { - id: { not: requestId }, - deletedAt: null, + where: otherActiveRequestsWhere(requestId, torrentHash), + select: { id: true, status: true }, + }); +} + +/** Same requests as `otherActiveRequestsUsingTorrent`, with type and their selected rows for this torrent (seeding cleanup) */ +export async function otherActiveRequestsSharingTorrent( + requestId: string, + torrentHash: string | null | undefined +): Promise { + if (!torrentHash) return []; + return prisma.request.findMany({ + where: otherActiveRequestsWhere(requestId, torrentHash), + select: { + id: true, + status: true, + type: true, downloadHistory: { - some: { - torrentHash: { in: torrentHashForms(torrentHash) }, - selected: true, - }, + where: { torrentHash: { in: torrentHashForms(torrentHash) }, selected: true }, + select: { id: true, indexerId: true, indexerName: true, downloadClient: true }, }, }, - select: { id: true, status: true }, }); } + +/** + * Seeding cleanup only (#283): why `sibling` still needs the torrent, or null when it lets it go. + * It lets it go only when it is finished, every row it has for the torrent was recorded for + * `clientType`, and its own indexer's requirement is met (`seeding(row)`, by the same client answer). + */ +export function siblingKeepsTorrent( + sibling: TorrentSibling, + clientType: string, + seeding: (row: TorrentSiblingDownload) => SeedingEvaluation +): string | null { + if (!FINISHED_REQUESTS.some((f) => f.type === sibling.type && f.status === sibling.status)) { + return `${sibling.type ?? 'request'} ${sibling.status}`; + } + const rows = sibling.downloadHistory ?? []; + if (rows.length === 0) return 'no download row for this torrent'; + for (const row of rows) { + const recordedFor = row.downloadClient || 'qbittorrent'; + if (recordedFor !== clientType) return `recorded for ${recordedFor}`; + const { unlimited, met, progress } = seeding(row); + if (unlimited) return `unlimited seeding (${row.indexerName ?? 'no indexer'})`; + if (!met) return `still seeding for ${row.indexerName ?? 'its indexer'}: ${progress}`; + } + return null; +} diff --git a/tests/processors/cleanup-seeded-torrents.shared.test.ts b/tests/processors/cleanup-seeded-torrents.shared.test.ts new file mode 100644 index 000000000..5afff74db --- /dev/null +++ b/tests/processors/cleanup-seeded-torrents.shared.test.ts @@ -0,0 +1,197 @@ +/** + * Component: Cleanup Seeded Torrents Processor Tests - torrents shared by several requests (#283) + * Documentation: documentation/admin-features/request-deletion.md + * + * The seeding cleanup removes a shared torrent only when every request using it would let it go: + * each other active request is finished and its own indexer's seeding requirement is met. + * `findMany` answers the page query and the shared-torrent query from fixture requests. + */ + +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { createPrismaMock } from '../helpers/prisma'; + +const prismaMock = createPrismaMock(); +const configMock = vi.hoisted(() => ({ get: vi.fn() })); +const downloadClientManagerMock = vi.hoisted(() => ({ getClientServiceForProtocol: vi.fn() })); + +vi.mock('@/lib/db', () => ({ prisma: prismaMock })); +vi.mock('@/lib/services/config.service', () => ({ getConfigService: () => configMock })); +vi.mock('@/lib/services/download-client-manager.service', () => ({ + getDownloadClientManager: () => downloadClientManagerMock, +})); + +const HASH = 'abcdef0123456789abcdef0123456789abcdef01'; +// 40 minutes seeded: IndexerA (30 min) is met, Slow (600 min) is not, Unconfigured seeds forever +const INDEXERS = [ + { id: 1, name: 'IndexerA', protocol: 'torrent', seedingTimeMinutes: 30, ratioLimit: 0 }, + { id: 2, name: 'Slow', protocol: 'torrent', seedingTimeMinutes: 600, ratioLimit: 0 }, +]; + +interface Fixture { + id: string; + type: string; + status: string; + deletedAt: Date | null; + downloadHistory: Record[]; +} + +let requests: Fixture[] = []; + +/** The parts of the two findMany calls (page, shared-torrent) the fixtures answer */ +interface FindManyArgs { + where?: { + AND?: { id?: { gt?: string } }[]; + id?: { not?: string }; + downloadHistory?: { some?: { torrentHash?: { in?: string[] } } }; + }; +} +interface UpdateArgs { where: { id: string }; data?: { downloadStatus?: string } } +interface UpdateManyArgs { where: { id: { in: string[] } }; data?: { downloadStatus?: string } } + +function req(id: string, fields: Partial = {}, row: Record = {}): Fixture { + return { + id, + type: 'audiobook', + status: 'available', + deletedAt: null, + downloadHistory: [{ + id: `dh-${id}`, + selected: true, + downloadStatus: 'completed', + indexerName: 'IndexerA', + indexerId: 1, + torrentHash: HASH, + downloadClientId: HASH, + downloadClient: 'qbittorrent', + addedByRmab: true, + createdAt: new Date('2026-10-01T12:00:00Z'), + ...row, + }], + ...fields, + }; +} + +function setup(fixtures: Fixture[]) { + requests = fixtures; + configMock.get.mockResolvedValue(JSON.stringify(INDEXERS)); + const client = { + clientType: 'qbittorrent', + protocol: 'torrent', + probeDownload: vi.fn(async () => (client.deleteDownload.mock.calls.length + ? { found: false } + : { found: true, info: { id: HASH, name: 'Series Pack', status: 'seeding', progress: 1, seedingTime: 60 * 40, ratio: 0 } })), + deleteDownload: vi.fn().mockResolvedValue(undefined), + }; + downloadClientManagerMock.getClientServiceForProtocol.mockResolvedValue(client); + return client; +} + +/** Download rows the run marked 'removed' (one by one or in one updateMany) */ +function rowsMarkedRemoved(): string[] { + const one = (prismaMock.downloadHistory.update.mock.calls as [UpdateArgs][]) + .filter(([args]) => args.data?.downloadStatus === 'removed') + .map(([args]) => args.where.id); + const many = (prismaMock.downloadHistory.updateMany.mock.calls as [UpdateManyArgs][]) + .filter(([args]) => args.data?.downloadStatus === 'removed') + .flatMap(([args]) => args.where.id.in); + return [...new Set([...one, ...many])].sort(); +} + +async function run() { + const { processCleanupSeededTorrents } = await import('@/lib/processors/cleanup-seeded-torrents.processor'); + return processCleanupSeededTorrents({ jobId: 'job-283' }); +} + +beforeEach(() => { + vi.clearAllMocks(); + requests = []; + prismaMock.request.findMany.mockImplementation(async (args: FindManyArgs) => { + const where = args?.where ?? {}; + if (where.AND) { + // The page query: one page, every fixture with a completed selected row, in the order given + return where.AND.some((c) => c.id?.gt) ? [] : requests + .filter((r) => r.downloadHistory.some((h) => h.selected && h.downloadStatus === 'completed')); + } + // The shared-torrent query: other non-deleted requests whose selected row is this torrent + const forms = where.downloadHistory?.some?.torrentHash?.in ?? []; + return requests + .filter((r) => r.id !== where.id?.not && r.deletedAt === null) + .filter((r) => r.downloadHistory.some((h) => h.selected && forms.includes(h.torrentHash as string))) + .map((r) => ({ ...r, downloadHistory: r.downloadHistory.filter((h) => forms.includes(h.torrentHash as string)) })); + }); +}); + +describe('cleanup of a torrent shared by several requests (#283)', () => { + it('removes it once when three finished requests have all met the seeding requirement', async () => { + const client = setup([req('req-a'), req('req-b'), req('req-c')]); + + const result = await run(); + + expect(client.deleteDownload).toHaveBeenCalledTimes(1); + expect(client.deleteDownload).toHaveBeenCalledWith(HASH, true); + expect(rowsMarkedRemoved()).toEqual(['dh-req-a', 'dh-req-b', 'dh-req-c']); + expect(result.errors).toBe(0); + expect(prismaMock.request.delete).not.toHaveBeenCalled(); + }); + + it('marks the rows of requests that took the torrent over (series pack) when the owner removes it', async () => { + // The 2nd and 3rd request found the pack already in the client: never removed through their own rows + const client = setup([ + req('req-a', {}, { addedByRmab: false }), + req('req-b', { type: 'ebook', status: 'downloaded' }, { addedByRmab: false }), + req('req-c'), + ]); + + const result = await run(); + + expect(client.deleteDownload).toHaveBeenCalledTimes(1); + expect(rowsMarkedRemoved()).toEqual(['dh-req-a', 'dh-req-b', 'dh-req-c']); + expect(result.errors).toBe(0); + }); + + it('marks a sibling whose torrent is already gone in a later run, without errors', async () => { + const client = setup([req('req-a'), req('req-b')]); + client.probeDownload.mockImplementation(async () => ({ found: false })); + + const result = await run(); + + expect(client.deleteDownload).not.toHaveBeenCalled(); + expect(rowsMarkedRemoved()).toEqual(['dh-req-a', 'dh-req-b']); + expect(result.errors).toBe(0); + }); + + it.each(['downloading', 'processing', 'downloaded', 'failed'])('keeps it while another request is %s', async (status) => { + const client = setup([req('req-a'), req('req-b'), req('req-c', { status }, { downloadStatus: 'downloading' })]); + + const result = await run(); + + expect(client.deleteDownload).not.toHaveBeenCalled(); + expect(rowsMarkedRemoved()).toEqual([]); + expect(result.errors).toBe(0); + }); + + it.each([ + ['its own indexer asks for longer seeding', { indexerName: 'Slow', indexerId: 2 }], + ['its own indexer is not configured (seeds forever)', { indexerName: 'Unconfigured', indexerId: 9 }], + ['it was recorded for another download client', { downloadClient: 'transmission' }], + ])('keeps it when a finished request still needs it: %s', async (_label, row) => { + const client = setup([req('req-a'), req('req-b', {}, row)]); + + const result = await run(); + + expect(client.deleteDownload).not.toHaveBeenCalled(); + expect(rowsMarkedRemoved()).toEqual([]); + expect(result.errors).toBe(0); + }); + + it('still ignores soft-deleted requests: the active owner removes it and the soft-deleted one is hard-deleted', async () => { + const client = setup([req('req-a'), req('req-b', { deletedAt: new Date() })]); + + const result = await run(); + + expect(client.deleteDownload).toHaveBeenCalledTimes(1); + expect(prismaMock.request.delete).toHaveBeenCalledWith({ where: { id: 'req-b' } }); + expect(rowsMarkedRemoved()).toEqual(['dh-req-a']); + expect(result.errors).toBe(0); + }); +}); diff --git a/tests/services/request-delete.seeding.test.ts b/tests/services/request-delete.seeding.test.ts index efab72eba..a04a2caaa 100644 --- a/tests/services/request-delete.seeding.test.ts +++ b/tests/services/request-delete.seeding.test.ts @@ -164,6 +164,24 @@ describe('deleteRequest seeding rule', () => { })); }); + it('keeps a torrent another request uses even when that request is finished and done seeding (#283)', async () => { + // The seeding cleanup may remove it then; deleting one request never does + prismaMock.request.findMany.mockImplementation(async (args: { where?: { downloadHistory?: { some?: { torrentHash?: { in?: string[] } } } } }) => + args?.where?.downloadHistory?.some?.torrentHash?.in?.includes('hash-1') + ? [{ + id: 'req-2', type: 'audiobook', status: 'available', + downloadHistory: [{ id: 'dh-2', indexerId: 1, indexerName: 'Tracker', downloadClient: 'qbittorrent' }], + }] + : [] + ); + + const { result, client } = await deleteWith({ seedingTimeMinutes: 60, ratioLimit: 1, seedingTime: 3600, ratio: 2 }); + + expect(client.deleteDownload).not.toHaveBeenCalled(); + expect(result.torrentsKeptShared).toBe(1); + expect(result.torrentsRemoved).toBe(0); + }); + it('keeps a torrent for unlimited seeding only when both criteria are 0', async () => { const { result, client } = await deleteWith({ seedingTimeMinutes: 0, ratioLimit: 0, seedingTime: 99999, ratio: 9 }); From 125c5cdc131f963108fe13a43588eab4d7222f94 Mon Sep 17 00:00:00 2001 From: kikootwo Date: Tue, 27 Oct 2026 02:34:24 -0400 Subject: [PATCH 2/2] fix: a shared torrent stays while an available sibling has no RMAB import from it (#283) A library scan can mark an audiobook available while its organize is still copying. The seeding cleanup now lets such a sibling release the torrent only when its book record was completed by the organizer (with a file path) after the sibling's row took the torrent. Also tests a soft-deleted owner removing a torrent whose only active sibling is finished and done seeding. Co-Authored-By: Claude Opus 5.5 --- .../admin-features/request-deletion.md | 1 + src/lib/utils/shared-torrent.ts | 31 +++++++++++-- .../cleanup-seeded-torrents.shared.test.ts | 45 ++++++++++++++++++- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/documentation/admin-features/request-deletion.md b/documentation/admin-features/request-deletion.md index 14ce6e0c5..b74791f15 100644 --- a/documentation/admin-features/request-deletion.md +++ b/documentation/admin-features/request-deletion.md @@ -164,6 +164,7 @@ where: { - **Not RMAB's torrent:** never removed; a soft-deleted request is hard-deleted, the torrent stays. Details: [scheduler.md](../backend/services/scheduler.md) (cleanup_seeded_torrents) - **Shared torrent (#283):** after ownership and this row's seeding check, `otherActiveRequestsSharingTorrent()` lists the other non-deleted requests on the torrent (same hash forms as the delete guard). Each keeps it (logged with the reason) unless `siblingKeepsTorrent()` (`src/lib/utils/shared-torrent.ts`) finds it: - finished: `FINISHED_REQUESTS` (audiobook `available`, ebook `downloaded`; also the candidate query's statuses). Any other status keeps it (`downloading`, `processing`, audiobook `downloaded`, `failed`, `cancelled`, ...) + - an audiobook also imported by RMAB since it took the torrent: its book record `completed` with a `filePath` (written only by the organizer after the copy) and `completedAt` later than the row's `createdAt`. A library scan can mark it `available` mid-copy, and a library match alone never passes, so either keeps it - every row it has for the torrent recorded for the configured client type - its own indexer's requirement met (`evaluateSeeding()` with its row's indexer, by the same probe answer); unlimited or unconfigured indexer keeps it - None keeps it → one `deleteDownload`, this row `removed` (or soft-deleted: hard-deleted), the siblings' rows `removed` in one `updateMany` (also rows RMAB did not add, which never reach the guard themselves). Same run: `deletedHashes`; later runs: the probe's not-found → `removed` diff --git a/src/lib/utils/shared-torrent.ts b/src/lib/utils/shared-torrent.ts index 3d301257e..c25f96ec7 100644 --- a/src/lib/utils/shared-torrent.ts +++ b/src/lib/utils/shared-torrent.ts @@ -26,14 +26,23 @@ export const FINISHED_REQUESTS = [ /** Another active request using the torrent, with its selected row(s) for that torrent */ export interface TorrentSibling extends ActiveRequestRef { type: string; + /** Its book record (shared by every request for the book); the organizer completes it after a successful copy */ + audiobook: SiblingBookRecord | null; downloadHistory: TorrentSiblingDownload[]; } +export interface SiblingBookRecord { + status: string; + filePath: string | null; + completedAt: Date | null; +} + export interface TorrentSiblingDownload { id: string; indexerId: number | null; indexerName: string | null; downloadClient: string | null; + createdAt: Date; } function otherActiveRequestsWhere(requestId: string, torrentHash: string) { @@ -77,18 +86,31 @@ export async function otherActiveRequestsSharingTorrent( id: true, status: true, type: true, + audiobook: { select: { status: true, filePath: true, completedAt: true } }, downloadHistory: { where: { torrentHash: { in: torrentHashForms(torrentHash) }, selected: true }, - select: { id: true, indexerId: true, indexerName: true, downloadClient: true }, + select: { id: true, indexerId: true, indexerName: true, downloadClient: true, createdAt: true }, }, }, }); } +/** + * A library scan can mark an audiobook `available` while its organize is still copying (#283). + * Only the organizer completes the book record with a `filePath`, after the copy; it must have done + * so after this row took the torrent, since the record is shared by every request for the book. + * A book only matched in the library (no RMAB import) never passes. + */ +function importedSince(book: SiblingBookRecord | null, row: TorrentSiblingDownload): boolean { + if (book?.status !== 'completed' || !book.filePath || !book.completedAt) return false; + return new Date(book.completedAt).getTime() > new Date(row.createdAt).getTime(); +} + /** * Seeding cleanup only (#283): why `sibling` still needs the torrent, or null when it lets it go. - * It lets it go only when it is finished, every row it has for the torrent was recorded for - * `clientType`, and its own indexer's requirement is met (`seeding(row)`, by the same client answer). + * It lets it go only when it is finished (an audiobook also imported by RMAB since it took the + * torrent), every row it has for the torrent was recorded for `clientType`, and its own indexer's + * requirement is met (`seeding(row)`, by the same client answer). */ export function siblingKeepsTorrent( sibling: TorrentSibling, @@ -103,6 +125,9 @@ export function siblingKeepsTorrent( for (const row of rows) { const recordedFor = row.downloadClient || 'qbittorrent'; if (recordedFor !== clientType) return `recorded for ${recordedFor}`; + if (sibling.type === 'audiobook' && !importedSince(sibling.audiobook, row)) { + return 'no RMAB import since it took this torrent'; + } const { unlimited, met, progress } = seeding(row); if (unlimited) return `unlimited seeding (${row.indexerName ?? 'no indexer'})`; if (!met) return `still seeding for ${row.indexerName ?? 'its indexer'}: ${progress}`; diff --git a/tests/processors/cleanup-seeded-torrents.shared.test.ts b/tests/processors/cleanup-seeded-torrents.shared.test.ts index 5afff74db..f982a3870 100644 --- a/tests/processors/cleanup-seeded-torrents.shared.test.ts +++ b/tests/processors/cleanup-seeded-torrents.shared.test.ts @@ -3,7 +3,8 @@ * Documentation: documentation/admin-features/request-deletion.md * * The seeding cleanup removes a shared torrent only when every request using it would let it go: - * each other active request is finished and its own indexer's seeding requirement is met. + * each other active request is finished (an audiobook also imported by RMAB since it took the torrent) + * and its own indexer's seeding requirement is met. * `findMany` answers the page query and the shared-torrent query from fixture requests. */ @@ -32,6 +33,7 @@ interface Fixture { type: string; status: string; deletedAt: Date | null; + audiobook: { status: string; filePath: string | null; completedAt: Date | null } | null; downloadHistory: Record[]; } @@ -54,6 +56,8 @@ function req(id: string, fields: Partial = {}, row: Record { expect(result.errors).toBe(0); }); + // A late joiner on a pack that has already seeded: it took the torrent days after the owner + const LATE_JOINER_ROW = { addedByRmab: false, createdAt: new Date('2026-10-05T12:00:00Z') }; + + it.each([ + ['a library scan marked it available while its organize is still copying', { status: 'processing', filePath: null, completedAt: null }], + ['it is only matched in the library (no RMAB import)', { status: 'requested', filePath: null, completedAt: null }], + ['the only import of the book is older than its row', { status: 'completed', filePath: '/library/Book 2', completedAt: new Date('2026-09-01T12:00:00Z') }], + ])('keeps it for an available audiobook RMAB has not imported from it: %s', async (_label, audiobook) => { + const client = setup([req('req-a'), req('req-b', { audiobook }, LATE_JOINER_ROW)]); + + const result = await run(); + + expect(client.deleteDownload).not.toHaveBeenCalled(); + expect(rowsMarkedRemoved()).toEqual([]); + expect(result.errors).toBe(0); + }); + + it('lets it go once RMAB has imported the late joiner from it', async () => { + const imported = { status: 'completed', filePath: '/library/Book 2', completedAt: new Date('2026-10-05T12:30:00Z') }; + const client = setup([req('req-a'), req('req-b', { audiobook: imported }, LATE_JOINER_ROW)]); + + const result = await run(); + + expect(client.deleteDownload).toHaveBeenCalledTimes(1); + expect(rowsMarkedRemoved()).toEqual(['dh-req-a', 'dh-req-b']); + expect(result.errors).toBe(0); + }); + + it('removes it through a soft-deleted owner when the only active sibling is finished and done seeding', async () => { + const client = setup([req('req-a', { deletedAt: new Date() }), req('req-b', {}, { addedByRmab: false })]); + + const result = await run(); + + expect(client.deleteDownload).toHaveBeenCalledTimes(1); + expect(rowsMarkedRemoved()).toEqual(['dh-req-b']); + expect(prismaMock.request.delete).toHaveBeenCalledWith({ where: { id: 'req-a' } }); + expect(result.errors).toBe(0); + }); + it('still ignores soft-deleted requests: the active owner removes it and the soft-deleted one is hard-deleted', async () => { const client = setup([req('req-a'), req('req-b', { deletedAt: new Date() })]);