{"record":{"id":"938a8183ae7ff9a2","repo":"can1357/oh-my-pi","slug":"unable-to-allocate-unique-backup-path-in-destina","errorCode":null,"errorMessage":"Unable to allocate unique backup path in ${destinationDir}","messagePattern":"Unable to allocate unique backup path in (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mnemopi/src/dr/recovery.ts","lineNumber":123,"sourceCode":"\t\ttypeof error === \"object\" &&\n\t\t\"code\" in error &&\n\t\t(error as { readonly code?: unknown }).code === code\n\t);\n}\n\nfunction writeBackupFile(destinationDir: string, timestamp: string, bytes: Uint8Array): string {\n\tfor (let attempt = 0; attempt < 64; attempt += 1) {\n\t\tconst suffix = attempt === 0 ? \"\" : `_${nextUniqueToken()}`;\n\t\tconst backupPath = join(destinationDir, `mnemopi_backup_${timestamp}${suffix}.db.gz`);\n\t\ttry {\n\t\t\twriteFileSync(backupPath, bytes, { flag: \"wx\" });\n\t\t\treturn backupPath;\n\t\t} catch (error) {\n\t\t\tif (hasErrorCode(error, \"EEXIST\")) continue;\n\t\t\tthrow error;\n\t\t}\n\t}\n\tthrow new Error(`Unable to allocate unique backup path in ${destinationDir}`);\n}\n\nfunction restoreTempPath(targetPath: string): string {\n\treturn join(dirname(targetPath), `.${basename(targetPath)}.${process.pid}.${nextUniqueToken()}.restore.tmp`);\n}\n\nfunction defaultBackupDir(env: Env = process.env): string {\n\tconst explicit = env.MNEMOPI_BACKUP_DIR;\n\tif (explicit !== undefined && explicit.length > 0) return explicit;\n\tconst dir = configuredDataDir(env);\n\treturn join(dirname(dir), \"backups\");\n}\n\nexport function getDefaultPaths(env: Env = process.env): RecoveryPaths {\n\treturn {\n\t\tdataDir: configuredDataDir(env),\n\t\tbackupDir: defaultBackupDir(env),\n\t\tdbPath: configuredDbPath(env),","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/mnemopi/src/dr/recovery.ts#L105-L141","documentation":"writeBackupFile tries repeatedly to find a non-existing backup filename (with unique suffix tokens) via exclusive create; if every attempt collides with EEXIST it exhausts its attempts and throws this Error. It effectively means the backup directory is full of colliding names or the uniqueness scheme failed.","triggerScenarios":"Many backups within the same timestamp window exhausting the unique-token attempts; a destinationDir stuffed with thousands of files from prior runs; a filesystem where exclusive-create semantics misbehave; extremely long backup filenames plus busy directory causing persistent collisions.","commonSituations":"Cron jobs writing backups every second into one directory; archived backup dirs migrated onto systems that preserved all files; clock skew producing identical timestamps with exhausted random tokens.","solutions":["Clean up old backup files from destinationDir and retry the backup","Use a fresh/empty backup directory (pass backupDir to createBackup)","Increase spacing between backup runs or include more entropy in filenames"],"exampleFix":"// before\ncreateBackup(dbPath, \"/var/backups/mnemopi\"); // thousands of stale files\n// after\nfs.rmSync(\"/var/backups/mnemopi\", { /* prune files older than N days */ });\ncreateBackup(dbPath, \"/var/backups/mnemopi\");","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n\tconst result = createBackup(dbPath, backupDir);\n} catch (err) {\n\tif (err.message.startsWith(\"Unable to allocate unique backup path\")) {\n\t\tpruneOldBackups(backupDir, { keep: 10 });\n\t\tconst result = createBackup(dbPath, backupDir);\n\t}\n}","preventionTips":["Rotate/prune old backups regularly","Use distinct backup directories per run or per environment","Avoid backing up many times per second into the same directory","Monitor backup directory file counts"],"tags":["filesystem","backup","collision"],"backgroundTag":"path-already-exists","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}