From 8970d3f1051760dd961794bb21d8e21747305788 Mon Sep 17 00:00:00 2001 From: Declan Joseph Date: Mon, 9 Nov 2026 14:02:54 +0300 Subject: [PATCH] fix(geojob): handle EXDEV when moving staged files to the target directory In k3s the staging dir is on the container filesystem while the target (/occurrence_data) is a mounted PVC, so fs.rename() fails with EXDEV: cross-device link not permitted, and the whole atomic replacement failed. atomicReplaceFile now uses a moveFile helper: rename() as before, but on EXDEV it copies to a temp file inside the target directory, renames there (still atomic on the target device), and unlinks the staging file. Verified the EXDEV path for real inside the dev container (/tmp -> bind-mounted repo). Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- src/OccurrenceGeoJob/src/config/atomic.ts | 27 +++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/OccurrenceGeoJob/src/config/atomic.ts b/src/OccurrenceGeoJob/src/config/atomic.ts index 8a8963f75..aca24313c 100644 --- a/src/OccurrenceGeoJob/src/config/atomic.ts +++ b/src/OccurrenceGeoJob/src/config/atomic.ts @@ -88,6 +88,29 @@ async function createBackup( } } +/** + * Move a file from staging to target. rename() is atomic on POSIX but only + * within the same filesystem — when staging and target live on different + * devices (e.g. the container filesystem vs a mounted PVC in k3s) it fails + * with EXDEV. In that case fall back to copying to a temp file inside the + * target directory and renaming there (still atomic on the target device), + * then remove the staging file. + */ +async function moveFile(stagingPath: string, targetPath: string): Promise { + try { + await fs.rename(stagingPath, targetPath); + } catch (error) { + if ((error as { code?: string }).code !== 'EXDEV') throw error; + logger.debug( + `rename across devices failed (EXDEV), falling back to copy: ${stagingPath} -> ${targetPath}`, + ); + const tmpPath = `${targetPath}.tmp-${process.pid}`; + await fs.copyFile(stagingPath, tmpPath); + await fs.rename(tmpPath, targetPath); + await fs.unlink(stagingPath); + } +} + /** * Atomically replace a single file * Uses rename operation which is atomic on POSIX systems @@ -150,8 +173,8 @@ async function atomicReplaceFile( } } - // Atomic rename operation - await fs.rename(stagingPath, targetPath); + // Atomic rename operation (with EXDEV copy fallback — see moveFile) + await moveFile(stagingPath, targetPath); logger.info(`Atomically replaced ${targetPath}`); return { success: true, checksum: stagingChecksum, skipped: false };