Retry Windows daemon release publication on transient file locks (#50782)

## Why

Executable scanners can briefly hold newly staged files without delete sharing, causing the rename that publishes a managed daemon release to fail on Windows.

## What changed

Retry publication on permission-denied or sharing-violation errors at 50 ms intervals, up to 100 retries. Report the staging and release paths if publication fails.

## Testing

Add a Windows test that holds the staging directory open without delete sharing, releases the handle after a delay, and verifies that publication succeeds with the payload intact.

GitOrigin-RevId: 4fbd50934ec8937d3b440d38203a2cb7356a316d
This commit is contained in:
Eric Traut
2026-10-04 04:01:04 +00:00
committed by copyberry
parent f365d5754b
commit d0759639f2
3 changed files with 66 additions and 2 deletions
@@ -264,6 +264,9 @@ async fn prepare_from_package(
if !stage.path().join("codex").exists() {
std::os::unix::fs::symlink("bin/codex", stage.path().join("codex"))?;
}
#[cfg(windows)]
windows::publish_release(stage.path(), &release).await?;
#[cfg(not(windows))]
std::fs::rename(stage.path(), &release)?;
}
let standalone = home.join("packages/standalone");
@@ -1,12 +1,14 @@
//! Selects a Windows managed release by creating or retargeting the installer junction.
//! Publishes and selects Windows managed releases despite transient filesystem contention.
use std::os::windows::ffi::OsStrExt;
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::AsRawHandle;
use std::path::Path;
use std::time::Duration;
use anyhow::Context;
use anyhow::Result;
use windows_sys::Win32::Foundation::ERROR_SHARING_VIOLATION;
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS;
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OPEN_REPARSE_POINT;
use windows_sys::Win32::Storage::FileSystem::FILE_SHARE_DELETE;
@@ -14,6 +16,36 @@ use windows_sys::Win32::Storage::FileSystem::FILE_SHARE_READ;
use windows_sys::Win32::Storage::FileSystem::FILE_SHARE_WRITE;
use windows_sys::Win32::System::IO::DeviceIoControl;
const PUBLISH_RELEASE_RETRY_INTERVAL: Duration = Duration::from_millis(50);
const PUBLISH_RELEASE_RETRY_LIMIT: usize = 100;
pub(super) async fn publish_release(stage: &Path, release: &Path) -> Result<()> {
let mut retries = 0;
loop {
match std::fs::rename(stage, release) {
Ok(()) => return Ok(()),
// Executable scanners can briefly hold newly staged files without delete sharing.
Err(error)
if retries < PUBLISH_RELEASE_RETRY_LIMIT
&& (error.kind() == std::io::ErrorKind::PermissionDenied
|| error.raw_os_error() == Some(ERROR_SHARING_VIOLATION as i32)) =>
{
retries += 1;
tokio::time::sleep(PUBLISH_RELEASE_RETRY_INTERVAL).await;
}
Err(error) => {
return Err(error).with_context(|| {
format!(
"failed to publish managed daemon release from {} to {}",
stage.display(),
release.display()
)
});
}
}
}
}
pub(super) fn select_release(root: &Path, release: &Path) -> Result<()> {
let release = release.canonicalize()?;
let current = root.join("current");
@@ -1,5 +1,34 @@
//! Verifies Windows selection without requiring an elevated daemon.
//! Verifies Windows package publication and selection without requiring an elevated daemon.
use pretty_assertions::assert_eq;
use std::os::windows::fs::OpenOptionsExt;
use std::time::Duration;
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS;
use windows_sys::Win32::Storage::FileSystem::FILE_SHARE_READ;
use windows_sys::Win32::Storage::FileSystem::FILE_SHARE_WRITE;
#[tokio::test]
async fn publishing_release_retries_transient_access_denied() {
let home = tempfile::TempDir::new().unwrap();
let stage = home.path().join(".staging");
let release = home.path().join("release");
std::fs::create_dir(&stage).unwrap();
std::fs::write(stage.join("payload"), b"complete").unwrap();
let directory = std::fs::OpenOptions::new()
.read(true)
.share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
.open(&stage)
.unwrap();
let holder = tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(150)).await;
drop(directory);
});
super::publish_release(&stage, &release).await.unwrap();
holder.await.unwrap();
assert_eq!(std::fs::read(release.join("payload")).unwrap(), b"complete");
}
#[cfg(windows)]
#[test]