openai/codex · error · anyhow::Error

writer already taken

Error message

writer already taken

What it means

take_writer was called on a ConPTY master whose writable half has already been taken (Option::take consumed it). Only one writer can be extracted per PTY instance; a second call is a usage error.

Solutions

  1. Reuse the writer handle obtained from the first take_writer call
  2. Share or clone the existing writer instead of taking again
  3. Restructure ownership so only one component takes the writer
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at codex-rs/utils/pty/src/win/conpty.rs:181 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/c2b5e91cde858dae. Report an issue: GitHub.

Appendix: source

Thrown at codex-rs/utils/pty/src/win/conpty.rs:181

    }

    fn get_size(&self) -> Result<PtySize, Error> {
        let inner = self.inner.lock().unwrap();
        Ok(inner.size)
    }

    fn try_clone_reader(&self) -> anyhow::Result<Box<dyn std::io::Read + Send>> {
        Ok(Box::new(self.inner.lock().unwrap().readable.try_clone()?))
    }

    fn take_writer(&self) -> anyhow::Result<Box<dyn std::io::Write + Send>> {
        Ok(Box::new(
            self.inner
                .lock()
                .unwrap()
                .writable
                .take()
                .ok_or_else(|| anyhow::anyhow!("writer already taken"))?,
        ))
    }
}

impl SlavePty for ConPtySlavePty {
    fn spawn_command(&self, cmd: CommandBuilder) -> anyhow::Result<Box<dyn Child + Send + Sync>> {
        let inner = self.inner.lock().unwrap();
        let child = inner.con.spawn_command(cmd)?;
        Ok(Box::new(child))
    }
}

View on GitHub (pinned to 339751715c)