From b40383e72e24963bc045c39c188a475e0485a3d0 Mon Sep 17 00:00:00 2001 From: Alan Grainger <16197738+alangrainger@users.noreply.github.com> Date: Sat, 6 Jun 2026 21:14:36 +1200 Subject: [PATCH] Clean up any broken staged downloads --- app/src/index.ts | 5 +- app/src/stream/download.ts | 27 ++++++++++- app/tests/sweepStaleStagingDirs.test.ts | 63 +++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 app/tests/sweepStaleStagingDirs.test.ts diff --git a/app/src/index.ts b/app/src/index.ts index bde346d..cb8d499 100644 --- a/app/src/index.ts +++ b/app/src/index.ts @@ -13,7 +13,7 @@ import { } from './immich' import crypto from 'crypto' import { assetBuffer } from './stream/asset' -import { downloadAssets } from './stream/download' +import { downloadAssets, sweepStaleStagingDirs } from './stream/download' import dayjs from 'dayjs' import { NextFunction, Request, Response } from 'express-serve-static-core' import { Asset, AssetType, ImageSize, KeyType } from './types' @@ -282,4 +282,7 @@ process.on('SIGTERM', () => { const port = Number(process.env.IPP_PORT) || 3000 const server = app.listen(port, () => { console.log(dayjs().format() + ' Server started on port ' + port) + // Clean up any zip-download staging dirs left behind by a previous + // run that crashed before its finally block could run + sweepStaleStagingDirs().catch(e => console.error('sweepStaleStagingDirs failed:', e)) }) diff --git a/app/src/stream/download.ts b/app/src/stream/download.ts index d5815de..f1697b2 100644 --- a/app/src/stream/download.ts +++ b/app/src/stream/download.ts @@ -16,6 +16,8 @@ import { getFilename } from '../gallery/filename' import { createLimiter } from '../utils/limiter' import { createIdleTimeoutStream } from '../utils/idleTimeoutStream' +const STAGING_DIR_PREFIX = 'ipp-zip-' + /** * Download all assets in a share as a zip file. */ @@ -23,6 +25,29 @@ export async function downloadAll (res: Response, share: SharedLink) { await downloadAssets(res, share, share.assets) } +/** + * Delete staging directories left over from a prior run that crashed before + * its `finally` block could clean up. Intended to run once at startup. + * + * A live download holds its staging dir open for the duration of the zip; + * `maxAgeMs` should comfortably exceed any realistic download time so we + * never delete a dir from a still-running download in another worker. + */ +export async function sweepStaleStagingDirs (maxAgeMs = 60 * 60 * 1000) { + const root = tmpdir() + const cutoff = Date.now() - maxAgeMs + const entries = await fs.readdir(root).catch(() => [] as string[]) + for (const name of entries) { + if (!name.startsWith(STAGING_DIR_PREFIX)) continue + const path = join(root, name) + const stat = await fs.stat(path).catch(() => null) + if (!stat || stat.mtimeMs >= cutoff) continue + await fs.rm(path, { recursive: true, force: true }).catch(e => { + log.warn(`Failed to sweep stale staging dir ${path}: ${e instanceof Error ? e.message : String(e)}`) + }) + } +} + type StagedAsset = { tempfile: string, asset: Asset, endpoint: ImageEndpoint } type Failure = { asset: Asset, url: string, status?: number, error?: unknown } type StageOutcome = StagedAsset | { failure: Failure } | null @@ -70,7 +95,7 @@ export async function downloadAssets (res: Response, share: SharedLink, assets: archive.pipe(res) const options: StagingOptions = { - stagingDir: await fs.mkdtemp(join(tmpdir(), 'ipp-zip-')), + stagingDir: await fs.mkdtemp(join(tmpdir(), STAGING_DIR_PREFIX)), concurrency: Math.max(1, getConfigOption('ipp.downloadFromImmichConcurrencyLimit', 8) as number), maxAttempts: 3, headerTimeoutMs: 20_000, diff --git a/app/tests/sweepStaleStagingDirs.test.ts b/app/tests/sweepStaleStagingDirs.test.ts new file mode 100644 index 0000000..c836276 --- /dev/null +++ b/app/tests/sweepStaleStagingDirs.test.ts @@ -0,0 +1,63 @@ +import { describe, it, expect, beforeEach, afterEach } from 'vitest' +import { promises as fs } from 'fs' +import { tmpdir } from 'os' +import { join } from 'path' +import { sweepStaleStagingDirs } from '../src/stream/download' + +const PREFIX = 'ipp-zip-' + +async function makeDir (name: string, mtimeMsAgo: number) { + const path = join(tmpdir(), name) + await fs.mkdir(path, { recursive: true }) + await fs.writeFile(join(path, 'inside.bin'), 'x') + const mtime = new Date(Date.now() - mtimeMsAgo) + await fs.utimes(path, mtime, mtime) + return path +} + +async function exists (path: string) { + try { + await fs.access(path) + return true + } catch { + return false + } +} + +const created: string[] = [] + +afterEach(async () => { + await Promise.all(created.splice(0).map(p => fs.rm(p, { recursive: true, force: true }).catch(() => { /* best effort */ }))) +}) + +describe('sweepStaleStagingDirs', () => { + it('deletes stale staging dirs older than the cutoff', async () => { + const stale = await makeDir(PREFIX + 'stale-' + process.pid + '-' + Math.random().toString(36).slice(2), 2 * 60 * 60 * 1000) + created.push(stale) + await sweepStaleStagingDirs(60 * 60 * 1000) + expect(await exists(stale)).toBe(false) + }) + + it('preserves staging dirs younger than the cutoff', async () => { + const fresh = await makeDir(PREFIX + 'fresh-' + process.pid + '-' + Math.random().toString(36).slice(2), 10 * 60 * 1000) + created.push(fresh) + await sweepStaleStagingDirs(60 * 60 * 1000) + expect(await exists(fresh)).toBe(true) + }) + + it('ignores directories that do not match the staging prefix', async () => { + const unrelated = await makeDir('not-ipp-zip-' + process.pid + '-' + Math.random().toString(36).slice(2), 24 * 60 * 60 * 1000) + created.push(unrelated) + await sweepStaleStagingDirs(60 * 60 * 1000) + expect(await exists(unrelated)).toBe(true) + }) + + it('uses its default 1h cutoff when called with no argument', async () => { + const stale = await makeDir(PREFIX + 'default-' + process.pid + '-' + Math.random().toString(36).slice(2), 2 * 60 * 60 * 1000) + const fresh = await makeDir(PREFIX + 'default2-' + process.pid + '-' + Math.random().toString(36).slice(2), 30 * 60 * 1000) + created.push(stale, fresh) + await sweepStaleStagingDirs() + expect(await exists(stale)).toBe(false) + expect(await exists(fresh)).toBe(true) + }) +})