{"record":{"id":"eeaeb452495e1ab0","repo":"Hmbown/CodeWhale","slug":"this-runtime-is-already-active-in-another-process-close-the","errorCode":null,"errorMessage":"This runtime is already active in another process. Close the other Codewhale session and try again, or set CODEWHALE_RUNTIME_DIR to a different directory.","messagePattern":"This runtime is already active in another process\\. Close the other Codewhale session and try again, or set CODEWHALE_RUNTIME_DIR to a different directory\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/runtime_threads.rs","lineNumber":4602,"sourceCode":"            options.create(true).truncate(false).read(true).write(true);\n        })?;\n        #[cfg(unix)]\n        {\n            use std::os::unix::fs::PermissionsExt as _;\n            file.set_permissions(fs::Permissions::from_mode(0o600))\n                .context(\"Failed to protect Runtime process owner lock\")?;\n        }\n        // Same-process drop-then-reopen can observe WouldBlock for a brief\n        // window while the previous fd is still closing — the same close-\n        // release race #5735 hit on the Runtime Chat scope lock. Retry only\n        // that contention; a lock that stays held still belongs to its owner.\n        let deadline = Instant::now() + Duration::from_millis(25);\n        loop {\n            match Self::try_lock_exclusive(&file) {\n                Ok(()) => break,\n                Err(error) if Self::is_contention(&error) => {\n                    if Instant::now() >= deadline {\n                        bail!(\"{RUNTIME_PROCESS_OWNER_LOCK_HELD}\");\n                    }\n                    std::thread::yield_now();\n                    std::thread::sleep(Duration::from_millis(1));\n                }\n                Err(error) => {\n                    return Err(error).context(\"Failed to acquire Runtime process owner lock\");\n                }\n            }\n        }\n        Ok(Self { _file: file })\n    }\n\n    fn is_contention(error: &std::io::Error) -> bool {\n        error.kind() == std::io::ErrorKind::WouldBlock\n            || matches!(error.raw_os_error(), Some(32 | 33))\n    }\n\n    fn try_lock_exclusive(file: &File) -> std::io::Result<()> {","sourceCodeStart":4584,"sourceCodeEnd":4620,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/runtime_threads.rs#L4584-L4620","documentation":"The Runtime takes an exclusive OS-level file lock (Runtime process owner lock) to ensure only one Codewhale process owns a runtime directory. After a short contention deadline (~25ms) it bails with RUNTIME_PROCESS_OWNER_LOCK_HELD, telling the user another session holds the directory.","triggerScenarios":"Starting a Codewhale TUI/CLI session pointed at a runtime directory whose owner lock file is still locked by another live process.","commonSituations":"Two terminal windows open on the same CODEWHALE_RUNTIME_DIR; a previous session crashed leaving a stale process holding the lock; shared home dir used from two machines.","solutions":["Close the other Codewhale session holding the runtime directory","Set CODEWHALE_RUNTIME_DIR to a different directory for the second instance","Verify no orphaned codewhale process still runs (ps/kill it) if no other session is visibly open"],"exampleFix":"// before\nCODEWHALE_RUNTIME_DIR=~/.codewhale codewhale  # while another session runs\n// after\nCODEWHALE_RUNTIME_DIR=~/.codewhale-work codewhale","handlingStrategy":"retry","validationCode":"// check for an existing owner process before starting\nstd::env::var(\"CODEWHALE_RUNTIME_DIR\").map(|dir| format!(\"{dir}/owner.lock\")).map(|_| ()).map_err(|_| \"CODEWHALE_RUNTIME_DIR not set\".to_string())","typeGuard":null,"tryCatchPattern":"match start_runtime(dir) {\n    Err(e) if e.to_string().contains(\"already active in another process\") => {\n        eprintln!(\"Another Codewhale session owns {}; use a different CODEWHALE_RUNTIME_DIR\", dir);\n    }\n    other => other?,\n}","preventionTips":["One session per runtime directory","Give parallel sessions distinct CODEWHALE_RUNTIME_DIR values","After a crash, confirm no orphan process holds the lock before restarting"],"tags":["locking","concurrency","process"],"backgroundTag":"address-already-in-use","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}