mirror of
https://github.com/openai/codex.git
synced 2026-11-29 00:00:54 +00:00
## Why Managed stderr logs previously retained a tail only before a new launch, allowing logs to grow during a long-running process. Direct stderr writes could also block lifecycle operations on disk I/O. ## What changed - Route managed app-server and updater logs through a shared nonblocking worker with a bounded queue. Drop records when the queue is full or a record exceeds 16 KiB, and avoid waiting for the worker at shutdown. - Prune live logs when they exceed 1 MiB, preserving the latest 256 KiB in `.log.previous` before truncating the current file. Keep inherited stderr handles usable after truncation. - Pause predecessor pruning during Windows updater handoff and resume it after a failed handoff only when ownership has been restored. - Keep ordinary stderr output when managed logging has not been initialized. ## Testing Add regression tests for stalled workers and queue overflow, tail retention, inherited append and nonappend handles, and pruning exclusion during updater handoff. GitOrigin-RevId: b6301962b1180fa6cdb231a56edce2429dc20b54
120 lines
4.5 KiB
Rust
120 lines
4.5 KiB
Rust
//! Windows updater publication handshake. The predecessor holds the operation
|
|
//! and reservation locks until its successor initializes and acknowledges readiness.
|
|
|
|
use super::super::windows::Process;
|
|
use super::PidBackend;
|
|
use super::PidFileState;
|
|
use super::PidRecord;
|
|
use super::START_TIMEOUT;
|
|
use super::STOP_POLL_INTERVAL;
|
|
use super::process_matches_record;
|
|
use super::read_process_start_time;
|
|
use anyhow::Context;
|
|
use anyhow::Result;
|
|
use anyhow::bail;
|
|
use tokio::fs;
|
|
use tokio::time::sleep;
|
|
|
|
impl PidBackend {
|
|
pub(crate) async fn wait_for_ownership(&self) -> Result<()> {
|
|
let deadline = tokio::time::Instant::now() + START_TIMEOUT;
|
|
loop {
|
|
if let PidFileState::Running(record) = self.read_pid_file_state().await?
|
|
&& record.pid == std::process::id()
|
|
&& self.record_is_active(&record).await?
|
|
{
|
|
return Ok(());
|
|
}
|
|
if tokio::time::Instant::now() >= deadline {
|
|
bail!("updater was not published by its launcher");
|
|
}
|
|
sleep(STOP_POLL_INTERVAL).await;
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn mark_ready(&self) -> Result<()> {
|
|
fs::write(self.pid_file.with_extension("ready"), b"")
|
|
.await
|
|
.context("failed to acknowledge updater startup")
|
|
}
|
|
|
|
// Keep pruning excluded until successor startup and any ownership rollback finish.
|
|
#[allow(clippy::await_holding_invalid_type)]
|
|
pub(crate) async fn replace_current_updater(&self) -> Result<()> {
|
|
// Retry busy pruning instead of waiting on the logging worker. Keep it paused
|
|
// before open_stderr_log() truncates the shared log and throughout startup.
|
|
let mut pruning_enabled = super::super::stderr_log::PRUNING_ENABLED
|
|
.try_lock()
|
|
.context("stderr pruning is busy; retry updater handoff")?;
|
|
let record = PidRecord {
|
|
pid: std::process::id(),
|
|
process_start_time: read_process_start_time(std::process::id()).await?,
|
|
process_identity: None,
|
|
executable_identity: None,
|
|
};
|
|
*pruning_enabled = false;
|
|
let result = self.start_inner(Some(record.clone())).await;
|
|
// Resume only after ownership is restored. Failed cleanup may leave the
|
|
// successor alive, and successful handoff leaves it in charge of retention.
|
|
if result.is_err()
|
|
&& matches!(
|
|
self.read_pid_file_state().await,
|
|
Ok(PidFileState::Running(previous)) if previous == record
|
|
)
|
|
{
|
|
*pruning_enabled = true;
|
|
}
|
|
result?;
|
|
Ok(())
|
|
}
|
|
|
|
pub(super) async fn finish_updater_start(
|
|
&self,
|
|
record: &PidRecord,
|
|
replacement: Option<&PidRecord>,
|
|
) -> Result<()> {
|
|
let temp_pid_file = self.pid_file.with_extension("pid.tmp");
|
|
let ready = self.pid_file.with_extension("ready");
|
|
let deadline = tokio::time::Instant::now() + START_TIMEOUT;
|
|
let startup = async {
|
|
loop {
|
|
if !process_matches_record(record).await? {
|
|
bail!("updater exited before becoming ready");
|
|
}
|
|
match fs::remove_file(&ready).await {
|
|
Ok(()) => return Ok(()),
|
|
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
|
|
Err(err) => return Err(err.into()),
|
|
}
|
|
if tokio::time::Instant::now() >= deadline {
|
|
bail!("updater did not become ready");
|
|
}
|
|
sleep(STOP_POLL_INTERVAL).await;
|
|
}
|
|
}
|
|
.await;
|
|
if let Err(err) = startup {
|
|
if let Some(process) = Process::open(record.pid)?
|
|
&& process.start_time()? == record.process_start_time
|
|
{
|
|
process.terminate()?;
|
|
let deadline = tokio::time::Instant::now() + START_TIMEOUT;
|
|
while process.is_running()? {
|
|
if tokio::time::Instant::now() >= deadline {
|
|
bail!("failed updater did not exit; ownership was not restored");
|
|
}
|
|
sleep(STOP_POLL_INTERVAL).await;
|
|
}
|
|
}
|
|
if let Some(previous) = replacement {
|
|
fs::write(&temp_pid_file, serde_json::to_vec(previous)?).await?;
|
|
fs::rename(&temp_pid_file, &self.pid_file).await?;
|
|
} else {
|
|
let _ = fs::remove_file(&self.pid_file).await;
|
|
}
|
|
return Err(err);
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|