{"record":{"id":"b7e976d44e754429","repo":"clockworklabs/SpacetimeDB","slug":"repo","errorCode":null,"errorMessage":"repo {}: {}: {}","messagePattern":"repo (.+?): (.+?): (.+?)","errorType":"exception","errorClass":"std::io::Error","httpStatus":null,"severity":"error","filePath":"crates/commitlog/src/repo/fs.rs","lineNumber":284,"sourceCode":"            Ok(len)\n        }\n    }\n}\n\nimpl Repo for Fs {\n    type SegmentWriter = File;\n    type SegmentReader = ReadOnlySegment;\n\n    fn create_segment(&self, offset: u64, header: segment::Header) -> io::Result<Self::SegmentWriter> {\n        let path = self.segment_path(offset);\n\n        // We need to check if the segment already exists,\n        // so use file locking to prevent a TOCTOU race.\n        // Using `flock` means we don't need to worry about stale lockfiles.\n        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                    ));","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/3653d2ed498fddbd83b7062aa6bf8970e18635ec/crates/commitlog/src/repo/fs.rs#L266-L302","documentation":"Filesystem Repo::create_segment guards segment creation with an advisory lock file (<segment>.lock, flock-based) to prevent a create/check TOCTOU race. If acquiring the lock fails, the error is re-wrapped as 'repo {path}: {error}: {source}', keeping the original error kind so the real cause (permissions, IO) is still visible. The repo path prefix lets operators tell multiple data directories apart in logs.","triggerScenarios":"Another process holds or created the .lock file concurrently (two nodes, or node plus CLI, on one data directory); the data directory is unwritable so the lock file cannot be created; a hung process left the lock held; disk full or IO error while creating the lock file.","commonSituations":"Accidentally starting a second node on the same database directory; running repair/maintenance tools against a live node; container permission mismatches on mounted volumes.","solutions":["Ensure exactly one writer process per commitlog directory - stop the other node or tool first.","Verify the data directory is writable by the node's user (permissions, mount options, disk space).","If a dead process left a stale lock, confirm it is really dead (ps/lsof), then remove the stale .lock file and retry."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Fail fast if the data directory is not writable before segment operations\nlet probe = repo_dir.join(\".write-probe\");\nstd::fs::write(&probe, b\"\")?;\nstd::fs::remove_file(&probe)?;","typeGuard":null,"tryCatchPattern":"match repo.create_segment(offset, header) {\n    Ok(seg) => seg,\n    Err(e) => {\n        // lock acquisition failure: another writer or a permissions/disk problem - stop, do not retry-loop\n        tracing::error!(\"segment lock failed: {e}\");\n        return Err(e.into());\n    }\n}","preventionTips":["Enforce one process per commitlog directory at the orchestrator/supervisor level.","After hard crashes, check for stale .lock files and clean them only after confirming no live holder (ps/lsof).","Keep the data directory off read-only or permission-mangled mounts."],"tags":["rust","commitlog","file-lock","filesystem","concurrency"],"backgroundTag":"file-lock-acquire-failed","analyzedSha":"3653d2ed498fddbd83b7062aa6bf8970e18635ec","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}