openai/codex · error · anyhow::Error

refusing to overwrite existing file

Error message

refusing to overwrite existing file {}

What it means

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.

Solutions

  1. Verify the existing file's content and reuse it if identical (use the _or_reuse variant)
  2. Remove the stale destination file and retry the write
  3. Treat concurrent writers of the same artifact as the root cause and serialize them
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at codex-rs/network-proxy/src/certs.rs:717 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of openai/codex@339751715c (2026-08-25). Data as JSON: /api/errors/fb1443cfb3f438e7. Report an issue: GitHub.

Appendix: source

Thrown at codex-rs/network-proxy/src/certs.rs:717

    let tmp_path = parent.join(format!(".{file_name}.tmp.{pid}.{nanos}"));

    let mut file = open_create_new_with_mode(&tmp_path, mode)?;
    file.write_all(contents)
        .with_context(|| format!("failed to write {}", tmp_path.display()))?;
    file.sync_all()
        .with_context(|| format!("failed to fsync {}", tmp_path.display()))?;
    drop(file);

    // Create the final file using "create-new" semantics (no overwrite). `rename` on Unix can
    // overwrite existing files, so prefer a hard-link, which fails if the destination exists.
    match fs::hard_link(&tmp_path, path) {
        Ok(()) => {
            fs::remove_file(&tmp_path)
                .with_context(|| format!("failed to remove {}", tmp_path.display()))?;
        }
        Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
            let _ = fs::remove_file(&tmp_path);
            return Err(anyhow!(
                "refusing to overwrite existing file {}",
                path.display()
            ));
        }
        Err(_) => {
            // Best-effort fallback for environments where hard links are not supported.
            // This is still subject to a TOCTOU race, but the typical case is a private per-user
            // config directory, where other users cannot create files anyway.
            if path.exists() {
                let _ = fs::remove_file(&tmp_path);
                return Err(anyhow!(
                    "refusing to overwrite existing file {}",
                    path.display()
                ));
            }
            fs::rename(&tmp_path, path).with_context(|| {
                format!(
                    "failed to rename {} -> {}",

View on GitHub (pinned to 339751715c)