{"record":{"id":"fb1443cfb3f438e7","repo":"openai/codex","slug":"refusing-to-overwrite-existing-file","errorCode":null,"errorMessage":"refusing to overwrite existing file {}","messagePattern":"refusing to overwrite existing file (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"codex-rs/network-proxy/src/certs.rs","lineNumber":717,"sourceCode":"    let tmp_path = parent.join(format!(\".{file_name}.tmp.{pid}.{nanos}\"));\n\n    let mut file = open_create_new_with_mode(&tmp_path, mode)?;\n    file.write_all(contents)\n        .with_context(|| format!(\"failed to write {}\", tmp_path.display()))?;\n    file.sync_all()\n        .with_context(|| format!(\"failed to fsync {}\", tmp_path.display()))?;\n    drop(file);\n\n    // Create the final file using \"create-new\" semantics (no overwrite). `rename` on Unix can\n    // overwrite existing files, so prefer a hard-link, which fails if the destination exists.\n    match fs::hard_link(&tmp_path, path) {\n        Ok(()) => {\n            fs::remove_file(&tmp_path)\n                .with_context(|| format!(\"failed to remove {}\", tmp_path.display()))?;\n        }\n        Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {\n            let _ = fs::remove_file(&tmp_path);\n            return Err(anyhow!(\n                \"refusing to overwrite existing file {}\",\n                path.display()\n            ));\n        }\n        Err(_) => {\n            // Best-effort fallback for environments where hard links are not supported.\n            // This is still subject to a TOCTOU race, but the typical case is a private per-user\n            // config directory, where other users cannot create files anyway.\n            if path.exists() {\n                let _ = fs::remove_file(&tmp_path);\n                return Err(anyhow!(\n                    \"refusing to overwrite existing file {}\",\n                    path.display()\n                ));\n            }\n            fs::rename(&tmp_path, path).with_context(|| {\n                format!(\n                    \"failed to rename {} -> {}\",","sourceCodeStart":699,"sourceCodeEnd":735,"githubUrl":"https://github.com/openai/codex/blob/339751715c64496cb86246bfb3935f40e309dd3d/codex-rs/network-proxy/src/certs.rs#L699-L735","documentation":"write_atomic_create_new completed the temp-file write but the final hard_link from tmp to the destination failed because the destination already exists. The create-new semantics are deliberate (no silent overwrite), so this fires when another process raced the same artifact into place.","triggerScenarios":"Thrown at codex-rs/network-proxy/src/certs.rs:717 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify the existing file's content and reuse it if identical (use the _or_reuse variant)","Remove the stale destination file and retry the write","Treat concurrent writers of the same artifact as the root cause and serialize them"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"339751715c64496cb86246bfb3935f40e309dd3d","analyzedAt":"2026-08-25T05:35:09.876Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}