{"record":{"id":"50ab5d8b35921f7f","repo":"clockworklabs/SpacetimeDB","slug":"alreadyexists","errorCode":"AlreadyExists","errorMessage":"repo {}: segment {} already exists and is non-empty","messagePattern":"repo (.+?): segment (.+?) already exists and is non-empty","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"crates/commitlog/src/repo/fs.rs","lineNumber":258,"sourceCode":"        let lock_path = path.0.with_extension(\"lock\");\n        let _lock = scopeguard::guard(\n            lockfile::advisory::LockedFile::lock(&lock_path)\n                .map_err(|e| io::Error::new(e.source.kind(), format!(\"repo {}: {}: {}\", self, e, e.source)))?,\n            |lockfile| {\n                if let Err(e) = lockfile.release(true) {\n                    // It's ok if removing the file fails, but print a warning\n                    // anyways.\n                    warn!(\"repo {}: failed to remove {}: {}\", self, lock_path.display(), e);\n                }\n            },\n        );\n\n        // Check whether the segment already exists.\n        // Overwrite it if its length is zero.\n        match fs::metadata(&path) {\n            Ok(stat) => {\n                if stat.len() > 0 {\n                    return Err(io::Error::new(\n                        io::ErrorKind::AlreadyExists,\n                        format!(\"repo {}: segment {} already exists and is non-empty\", self, offset),\n                    ));\n                }\n            }\n            Err(e) => {\n                if e.kind() != io::ErrorKind::NotFound {\n                    return Err(io::Error::new(\n                        e.kind(),\n                        format!(\n                            \"repo {}: error getting file metadata for segment {}: {}\",\n                            self, offset, e\n                        ),\n                    ));\n                }\n            }\n        }\n","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/commitlog/src/repo/fs.rs#L240-L276","documentation":"create_segment found the target segment file already on disk with length > 0 and refuses to overwrite it (AlreadyExists). Zero-length crash remnants ARE silently overwritten; only files that actually contain data are protected. The guard exists so re-initializing a used directory can never silently destroy committed data.","triggerScenarios":"Creating a segment whose base offset already has a non-empty file on disk: re-opening or re-initializing a Commitlog in a directory that already holds segments, running a second instance against the same directory, or manually copying segment files into the directory.","commonSituations":"Reusing a data directory for a 'fresh' log; double-starting a service; restoring files into a directory a new log intends to use.","solutions":["If you meant to continue the existing log, open it normally - the library resumes existing segments instead of re-creating them","If you really want an empty log, move or delete the old directory contents first (deliberately, after confirming nothing needs them)","Verify no second writer/process is running against the same directory"],"exampleFix":"// before: reusing a dirty directory for a fresh log\nlet log = Commitlog::open(old_dir, Options::default(), None)?; // AlreadyExists\n\n// after: give each log its own directory\nlet dir = CommitLogDir::new(root.join(\"log-2026-08-16\"))?;\nlet log = Commitlog::open(dir, Options::default(), None)?;","handlingStrategy":"validation","validationCode":"// refuse to initialize into a dirty directory\nlet is_empty = dir.as_path().read_dir()?.next().is_none();\nif !is_empty {\n    // open/resume the existing log instead of re-creating, or fail loudly\n    return Err(io::Error::new(io::ErrorKind::AlreadyExists, \"log directory is not empty\"));\n}","typeGuard":"fn is_nonempty_segment_exists(e: &io::Error) -> bool {\n    e.kind() == io::ErrorKind::AlreadyExists\n        && e.to_string().contains(\"already exists and is non-empty\")\n}","tryCatchPattern":"let log = match Commitlog::open(dir, opts, None) {\n    Ok(log) => log,\n    Err(e) if is_nonempty_segment_exists(&e) => {\n        // the directory already holds a log: resume it or pick another directory\n        return Err(e);\n    }\n    Err(e) => return Err(e),\n};","preventionTips":["One directory per log instance; never reuse data dirs for new logs","Treat AlreadyExists from open as a configuration bug, not a retryable error","Automate directory provisioning so operators cannot point two logs at one path"],"tags":["rust","commitlog","segment","already-exists","filesystem"],"backgroundTag":"file-already-exists","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}