From 434535bddfaf405a032f57be3c1096dd25ff6312 Mon Sep 17 00:00:00 2001 From: "Adam Perry @ OpenAI" Date: Tue, 15 Sep 2026 17:37:43 -0700 Subject: [PATCH 1/3] Release 0.155.0-alpha.9 --- codex-rs/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index 21d86ee677..485915634f 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -151,7 +151,7 @@ members = [ resolver = "2" [workspace.package] -version = "0.0.0" +version = "0.155.0-alpha.9" # Track the edition for all workspace crates in one place. Individual # crates can still override this value, but keeping it here means new # crates created with `cargo new -w ...` automatically inherit the 2024 From 9c8e46d32db35c8bac2724e3304a9e6f02f1c53d Mon Sep 17 00:00:00 2001 From: chess Date: Thu, 17 Sep 2026 15:17:05 +0000 Subject: [PATCH 2/3] Improve Windows sandbox error details and registry cleanup (#46237) ## Why Provisioning errors omitted underlying causes, and removing a legacy installation record could leave an empty registry key behind. ## What changed - Include error chains in provisioning responses and failure logs, retaining control-character replacement and the response byte limit. - Attempt to delete the legacy installation registry key after removing and flushing its record, preserving any remaining values or subkeys. ## Testing Add a regression test for nested error causes, control-character replacement, and UTF-8 truncation within the response byte limit. GitOrigin-RevId: 8fccfb49c708be5f5d5be7180289cb74a66498a2 (cherry picked from commit c11fdc944f116b5f995059db58ef01f179733122) --- .../src/runtime_ownership.rs | 13 ++++++++++++- codex-rs/windows-sandbox-service/src/ipc.rs | 2 +- .../windows-sandbox-service/src/ipc_tests.rs | 18 ++++++++++++++++++ .../src/provisioning/registered.rs | 2 +- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/codex-rs/windows-sandbox-rs/src/runtime_ownership.rs b/codex-rs/windows-sandbox-rs/src/runtime_ownership.rs index eea65af888..7b208833ef 100644 --- a/codex-rs/windows-sandbox-rs/src/runtime_ownership.rs +++ b/codex-rs/windows-sandbox-rs/src/runtime_ownership.rs @@ -10,6 +10,7 @@ use serde::Deserialize; use serde::Serialize; use windows_sys::Win32::Foundation as foundation; use windows_sys::Win32::System::Registry as registry; +use windows_sys::Win32::UI::Shell::SHDeleteEmptyKeyW; /// Selects legacy helper materialization or verified app-contained Core for setup. #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] @@ -188,7 +189,17 @@ pub fn remove_installation() -> Result<()> { match status { foundation::ERROR_SUCCESS | foundation::ERROR_FILE_NOT_FOUND - | foundation::ERROR_PATH_NOT_FOUND => flush_installation(INSTALLATION_KEY), + | foundation::ERROR_PATH_NOT_FOUND => { + flush_installation(INSTALLATION_KEY)?; + // Best-effort pruning preserves any remaining values or subkeys. + let _ = unsafe { + SHDeleteEmptyKeyW( + registry::HKEY_LOCAL_MACHINE, + to_wide(INSTALLATION_KEY).as_ptr(), + ) + }; + Ok(()) + } status => Err(io::Error::from_raw_os_error(status as i32)) .context("remove protected legacy sandbox installation record"), } diff --git a/codex-rs/windows-sandbox-service/src/ipc.rs b/codex-rs/windows-sandbox-service/src/ipc.rs index c60c862107..57680e09e5 100644 --- a/codex-rs/windows-sandbox-service/src/ipc.rs +++ b/codex-rs/windows-sandbox-service/src/ipc.rs @@ -190,7 +190,7 @@ fn write_response( fn response_error_message(error: &anyhow::Error) -> String { let mut message = String::new(); - for character in error.to_string().chars() { + for character in format!("{error:#}").chars() { let character = if character.is_control() { ' ' } else { diff --git a/codex-rs/windows-sandbox-service/src/ipc_tests.rs b/codex-rs/windows-sandbox-service/src/ipc_tests.rs index 92839d3e17..00718a6db9 100644 --- a/codex-rs/windows-sandbox-service/src/ipc_tests.rs +++ b/codex-rs/windows-sandbox-service/src/ipc_tests.rs @@ -1,3 +1,4 @@ +use super::MAX_RESPONSE_MESSAGE_BYTES; use super::OwnedHandle; use super::PipeConnection; use super::ServiceRequest; @@ -8,6 +9,7 @@ use super::pin_existing_ancestors; use super::pipe_security_descriptor; use super::refresh_session; use super::request::ProvisioningRequest; +use super::response_error_message; use super::validate_request; use super::wake; use codex_windows_sandbox::DirectoryOpenDisposition; @@ -37,6 +39,22 @@ use windows_sys::Win32::Storage::FileSystem as filesystem; use windows_sys::Win32::Storage::Packaging::Appx; use windows_sys::Win32::System::Pipes as pipes; +#[test] +fn provisioning_error_response_preserves_causes_within_message_limits() { + let error = anyhow::anyhow!("Windows error\t5\n") + .context("load profile") + .context("registered sandbox provisioning failed"); + assert_eq!( + response_error_message(&error), + "registered sandbox provisioning failed: load profile: Windows error 5 " + ); + + let error = anyhow::anyhow!("é".repeat(MAX_RESPONSE_MESSAGE_BYTES)).context("setup"); + let message = response_error_message(&error); + assert!(message.starts_with("setup: é")); + assert_eq!(message.len(), MAX_RESPONSE_MESSAGE_BYTES - 1); +} + #[test] fn session_refresh_observes_cleanup_completion_before_dispatch() { let shutdown = AtomicBool::new(false); diff --git a/codex-rs/windows-sandbox-service/src/provisioning/registered.rs b/codex-rs/windows-sandbox-service/src/provisioning/registered.rs index 13c53721d1..9ae0449fe4 100644 --- a/codex-rs/windows-sandbox-service/src/provisioning/registered.rs +++ b/codex-rs/windows-sandbox-service/src/provisioning/registered.rs @@ -115,7 +115,7 @@ pub(super) fn run( .inspect_err(|error| { crate::service::log_error( crate::service::EVENT_PROVISIONING_FAILED, - &format!("Codex sandbox provisioning failed: {error}"), + &format!("Codex sandbox provisioning failed: {error:#}"), ); }) .context("registered sandbox provisioning failed")?; From a2004f1a5fdab24a8e03f2ff9f23bc403c601f4d Mon Sep 17 00:00:00 2001 From: chess Date: Fri, 18 Sep 2026 00:23:12 +0000 Subject: [PATCH 3/3] Handle disabled Windows sandbox accounts during cleanup (#46333) ## Why Cleanup needs fresh logon tokens for sandbox accounts that may already be disabled. Temporarily enabling those accounts must leave a durable obligation to disable them again if the service exits unexpectedly. ## What changed - Persist `cleanup_logon_pending` before enabling an account, then disable it again after the logon attempt before clearing the marker. - Recover pending account disables before owner restoration or IPC admission, validating account SIDs before restoration. Block runtime readiness and provisioning while recovery is pending, and defer retirement until cleanup logons are prepared. - Include the blocking logon details in cleanup timeout errors and suppress repeated identical cleanup errors in the Windows event log. ## Testing Add receipt tests covering backward-compatible defaults and pending cleanup state surviving serialization, blocking readiness and owner admission until cleared. GitOrigin-RevId: 801bec408a27ac85ccdc3eb5ca2bdb2ccb3d5827 (cherry picked from commit fd875b188bde23782419c62dc00c4412fa1e4f91) --- .../src/runtime_ownership.rs | 12 ++- .../src/runtime_ownership_tests.rs | 34 ++++++++- .../src/uninstall_windows/processes.rs | 24 +++--- .../src/package_lifecycle/registered.rs | 4 +- .../src/registered_runtime.rs | 32 +++++--- .../src/registered_runtime/removal.rs | 74 +++++++++++++++++-- .../src/service/runtime_lifecycle.rs | 30 +++++--- 7 files changed, 168 insertions(+), 42 deletions(-) diff --git a/codex-rs/windows-sandbox-rs/src/runtime_ownership.rs b/codex-rs/windows-sandbox-rs/src/runtime_ownership.rs index 7b208833ef..ace6baaa05 100644 --- a/codex-rs/windows-sandbox-rs/src/runtime_ownership.rs +++ b/codex-rs/windows-sandbox-rs/src/runtime_ownership.rs @@ -93,7 +93,7 @@ impl RuntimeRegistration { && self .accounts .iter() - .all(|account| account.alias_path.is_some()) + .all(|account| account.alias_path.is_some() && !account.cleanup_logon_pending) } } @@ -119,7 +119,12 @@ impl InstallationRecord { "registered sandbox resources belong to a different owner or package" ); ensure!( - self.runtime()?.retiring.is_none(), + self.runtime()?.retiring.is_none() + && self + .runtime()? + .accounts + .iter() + .all(|account| !account.cleanup_logon_pending), "registered sandbox cleanup must finish before provisioning" ); Ok(()) @@ -128,6 +133,9 @@ impl InstallationRecord { #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub struct RuntimeAccountRegistration { + /// Persisted before temporarily enabling an account; cleared only after re-disabling it. + #[serde(default)] + pub cleanup_logon_pending: bool, pub account: SandboxRuntimeAccount, pub user_sid: String, /// OS-resolved alias written by the service, never inferred from a user name. diff --git a/codex-rs/windows-sandbox-rs/src/runtime_ownership_tests.rs b/codex-rs/windows-sandbox-rs/src/runtime_ownership_tests.rs index 9ef77269af..6fade70a0d 100644 --- a/codex-rs/windows-sandbox-rs/src/runtime_ownership_tests.rs +++ b/codex-rs/windows-sandbox-rs/src/runtime_ownership_tests.rs @@ -19,6 +19,7 @@ fn record() -> InstallationRecord { metadata_roots: Vec::new(), ready_package: None, accounts: vec![RuntimeAccountRegistration { + cleanup_logon_pending: false, account: SandboxRuntimeAccount::Offline, user_sid: "S-1-5-21-1-2-3-1001".into(), alias_path: None, @@ -41,6 +42,7 @@ fn ready_runtime() -> RuntimeRegistration { runtime.ready_package = Some(READY_PACKAGE.into()); runtime.accounts[0].alias_path = Some(PathBuf::from(r"C:\aliases\offline.exe")); runtime.accounts.push(RuntimeAccountRegistration { + cleanup_logon_pending: false, account: SandboxRuntimeAccount::Online, user_sid: "S-1-5-21-1-2-3-1002".into(), alias_path: Some(PathBuf::from(r"C:\aliases\online.exe")), @@ -200,9 +202,13 @@ fn metadata_root_history_survives_record_round_trip() { } #[test] -fn older_runtime_receipts_default_to_no_recorded_metadata_roots() { +fn older_runtime_receipts_default_to_no_metadata_roots_or_cleanup_logon() { let expected = record(); - let json = serde_json::to_value(&expected).unwrap(); + let mut json = serde_json::to_value(&expected).unwrap(); + json["runtime"]["accounts"][0] + .as_object_mut() + .unwrap() + .remove("cleanup_logon_pending"); assert!(json["runtime"].get("metadata_roots").is_none()); assert_eq!( serde_json::from_value::(json).unwrap(), @@ -243,3 +249,27 @@ fn legacy_non_null_cleanup_journal_cannot_silently_drop_its_fence() { assert!(serde_json::from_value::(json).is_err()); } } + +#[test] +fn interrupted_cleanup_logon_blocks_admission_after_record_reload() { + let mut original = record(); + original.runtime = Some(ready_runtime()); + original.runtime_mut().unwrap().accounts[0].cleanup_logon_pending = true; + let mut restored: InstallationRecord = + serde_json::from_str(&serde_json::to_string(&original).unwrap()).unwrap(); + assert_eq!(restored, original); + assert!(!restored.runtime().unwrap().ready_for_package(READY_PACKAGE)); + assert!( + restored + .admit_owner(&original, "OpenAI.Codex_publisher") + .is_err() + ); + + restored.runtime_mut().unwrap().accounts[0].cleanup_logon_pending = false; + assert!(restored.runtime().unwrap().ready_for_package(READY_PACKAGE)); + assert!( + restored + .admit_owner(&original, "OpenAI.Codex_publisher") + .is_ok() + ); +} diff --git a/codex-rs/windows-sandbox-rs/src/uninstall_windows/processes.rs b/codex-rs/windows-sandbox-rs/src/uninstall_windows/processes.rs index 84752b2030..5cb57079e0 100644 --- a/codex-rs/windows-sandbox-rs/src/uninstall_windows/processes.rs +++ b/codex-rs/windows-sandbox-rs/src/uninstall_windows/processes.rs @@ -83,11 +83,11 @@ pub(super) fn stop(users: &DisabledSandboxUsers, retained: &RetainedLogons) -> R // A token can outlive its process or exist before a runner starts. Account disable does // not revoke it, so check for remaining logins before removing protections. Only // explicitly pinned, private SYSTEM-finalizer tokens may remain; no process is exempt. - if !has_sandbox_logon_session(users, retained)? { + let Some(blocker) = blocking_logon_session(users, retained)? else { return Ok(()); - } + }; if Instant::now() >= deadline { - bail!("sandbox processes or logon tokens remain after the uninstall deadline"); + bail!("sandbox cleanup blocked after the uninstall deadline: {blocker}"); } // Repeat to catch descendants created during the previous snapshot. std::thread::sleep( @@ -159,10 +159,10 @@ fn sandbox_processes(users: &DisabledSandboxUsers) -> Result> { Ok(handles) } -fn has_sandbox_logon_session( +fn blocking_logon_session( users: &DisabledSandboxUsers, retained: &RetainedLogons, -) -> Result { +) -> Result> { let mut count = 0; let mut logons = null_mut(); let status = unsafe { LsaEnumerateLogonSessions(&mut count, &mut logons) }; @@ -171,7 +171,7 @@ fn has_sandbox_logon_session( } let logons = LsaBuffer(logons.cast()); if count == 0 { - return Ok(false); + return Ok(None); } for logon in unsafe { std::slice::from_raw_parts(logons.0.cast::(), count as usize) } { // LocalSystem uses the reserved logon ID 0:0x3e7 and has no normal logon data. @@ -188,17 +188,23 @@ fn has_sandbox_logon_session( } // Keep protections when LSA returns no session data. if data.is_null() { - return Ok(true); + return Ok(Some(format!( + "LSA returned no data for logon {:08x}:{:08x}", + logon.HighPart, logon.LowPart + ))); } let data = LsaBuffer(data.cast()); let data = unsafe { &*data.0.cast::() }; // LSA also lists sessions without a user SID, even before sandbox accounts exist. // Only sessions identified as sandbox users are evidence of remaining sandbox tokens. if is_sandbox_user(users, data.Sid) && !retained.contains(*logon) { - return Ok(true); + return Ok(Some(format!( + "sandbox logon {:08x}:{:08x} remains (logon type {}, session {})", + logon.HighPart, logon.LowPart, data.LogonType, data.Session + ))); } } - Ok(false) + Ok(None) } fn is_sandbox_user(users: &DisabledSandboxUsers, sid: *mut c_void) -> bool { diff --git a/codex-rs/windows-sandbox-service/src/package_lifecycle/registered.rs b/codex-rs/windows-sandbox-service/src/package_lifecycle/registered.rs index 4b9d10492b..3b0cdc294f 100644 --- a/codex-rs/windows-sandbox-service/src/package_lifecycle/registered.rs +++ b/codex-rs/windows-sandbox-service/src/package_lifecycle/registered.rs @@ -75,13 +75,13 @@ pub(super) fn clean_up(lifecycle: &PackageLifecycle, record: InstallationRecord) }), "waiting for the authenticated runtime owner and home before sandbox file cleanup" ); - let record = crate::registered_runtime::prepare_cleanup(record)?; + let mut record = crate::registered_runtime::prepare_cleanup(record)?; let removal = { let installation = lifecycle.installation.borrow(); let installation = installation .as_ref() .context("authenticated installation is missing")?; - crate::registered_runtime::prepare_removal(installation.user_token.0, &record)? + crate::registered_runtime::prepare_removal(installation.user_token.0, &mut record)? }; let prepared = prepare_packaged_windows_sandbox_cleanup_with_retained_tokens(&removal.tokens())?; diff --git a/codex-rs/windows-sandbox-service/src/registered_runtime.rs b/codex-rs/windows-sandbox-service/src/registered_runtime.rs index f9fc438d40..e9b82eb3fb 100644 --- a/codex-rs/windows-sandbox-service/src/registered_runtime.rs +++ b/codex-rs/windows-sandbox-service/src/registered_runtime.rs @@ -43,6 +43,7 @@ mod removal; pub(crate) use metadata::remove as remove_metadata; pub(crate) use removal::prepare as prepare_removal; +pub(crate) use removal::restore_disabled_accounts; /// Must escape request handling: pending resources are retained until this service exits. #[derive(Debug)] @@ -132,6 +133,7 @@ pub(crate) fn provision( .join(&family) .join(APP_CORE_RUNNER_ALIAS); accounts.push(RuntimeAccountRegistration { + cleanup_logon_pending: false, account, user_sid: profile.user_sid.clone(), alias_path: Some(alias_path), @@ -202,28 +204,38 @@ pub(crate) fn provision( Ok(()) } -/// Prepare one retirement fence under the caller's setup lock. Removal preparation -/// then retains exact account logons before the native guard disables those users. -pub(crate) fn prepare_cleanup(mut record: InstallationRecord) -> Result { +/// Validate cleanup ownership under the caller's setup lock before obtaining logons. +pub(crate) fn prepare_cleanup(record: InstallationRecord) -> Result { validate_record(&record)?; for account in &record.runtime()?.accounts { if codex_windows_sandbox::local_user_flags(account.account.username())?.is_some() { - let sid = lookup_sid(account.account.username())?; - ensure!( - string_from_sid_bytes(&sid).map_err(anyhow::Error::msg)? == account.user_sid, - "managed runtime account was replaced before cleanup" - ); + validate_account_sid(account)?; } } ensure!( record.runtime()?.retiring.is_none(), "interrupted runtime cleanup requires repair" ); - record.runtime_mut()?.ready_package = None; - record.runtime_mut()?.retiring = Some(format!("{:?}", windows::core::GUID::new()?)); + ensure!( + record + .runtime()? + .accounts + .iter() + .all(|account| !account.cleanup_logon_pending), + "cleanup logon recovery is pending" + ); Ok(record) } +fn validate_account_sid(account: &RuntimeAccountRegistration) -> Result<()> { + let sid = lookup_sid(account.account.username())?; + ensure!( + string_from_sid_bytes(&sid).map_err(anyhow::Error::msg)? == account.user_sid, + "managed runtime account was replaced before cleanup" + ); + Ok(()) +} + fn validate_record(record: &InstallationRecord) -> Result<()> { let runtime = record.runtime()?; ensure!( diff --git a/codex-rs/windows-sandbox-service/src/registered_runtime/removal.rs b/codex-rs/windows-sandbox-service/src/registered_runtime/removal.rs index 54de22bb25..507cfad4b6 100644 --- a/codex-rs/windows-sandbox-service/src/registered_runtime/removal.rs +++ b/codex-rs/windows-sandbox-service/src/registered_runtime/removal.rs @@ -22,6 +22,7 @@ use anyhow::ensure; use windows_sys::Win32::Foundation::DUPLICATE_SAME_ACCESS; use windows_sys::Win32::Foundation::DuplicateHandle; use windows_sys::Win32::Foundation::HANDLE; +use windows_sys::Win32::NetworkManagement::NetManagement::UF_ACCOUNTDISABLE; use windows_sys::Win32::System::Pipes::PeekNamedPipe; use windows_sys::Win32::System::SystemInformation::GetSystemDirectoryW; use windows_sys::Win32::System::Threading as threading; @@ -54,23 +55,67 @@ impl PreparedRemoval { } } +/// Restore durable temporary-enable intent before owner restoration or IPC admission. +pub(crate) fn restore_disabled_accounts() -> Result<()> { + let _lock = codex_windows_sandbox::acquire_sandbox_setup_lock(/*timeout_ms*/ 5_000)?; + let Some(mut record) = crate::installation_record::load_runtime()? else { + return Ok(()); + }; + if !crate::installation_record::is_current_package_family(&record)? { + return Ok(()); + } + super::validate_record(&record)?; + for index in 0..record.runtime()?.accounts.len() { + let account = &record.runtime()?.accounts[index]; + if !account.cleanup_logon_pending { + continue; + } + super::validate_account_sid(account)?; + let flags = codex_windows_sandbox::local_user_flags(account.account.username())? + .context("cleanup account disappeared before flag restoration")?; + codex_windows_sandbox::set_local_user_flags( + account.account.username(), + flags | UF_ACCOUNTDISABLE, + )?; + record.runtime_mut()?.accounts[index].cleanup_logon_pending = false; + crate::installation_record::save_runtime(&record)?; + } + Ok(()) +} + // The cleanup entry checked the service family; prepare_cleanup validated this record // under the same setup lock. Fresh logons below still require exact account-SID checks. -pub(crate) fn prepare(owner_token: HANDLE, record: &InstallationRecord) -> Result { +pub(crate) fn prepare( + owner_token: HANDLE, + record: &mut InstallationRecord, +) -> Result { ensure!( - record.runtime()?.retiring.is_some(), - "runtime is not retiring" + record.runtime()?.retiring.is_none(), + "runtime is already retiring" ); let mut tokens = Vec::new(); let mut targets = Vec::new(); - for account in &record.runtime()?.accounts { - if codex_windows_sandbox::local_user_flags(account.account.username())?.is_none() { + for index in 0..record.runtime()?.accounts.len() { + let account = record.runtime()?.accounts[index].clone(); + let Some(flags) = codex_windows_sandbox::local_user_flags(account.account.username())? + else { ensure!( super::registered_packages(&account.user_sid, &record.runtime()?.package_family)? .is_empty(), "runtime account is missing while its package remains registered" ); continue; + }; + // Persist the obligation before changing SAM, so process death cannot lose it. + if flags & UF_ACCOUNTDISABLE != 0 { + record.runtime_mut()?.accounts[index].cleanup_logon_pending = true; + // Older bundled clients also fail closed while account recovery is pending. + record.runtime_mut()?.ready_package = None; + crate::installation_record::save_runtime(record)?; + codex_windows_sandbox::set_local_user_flags( + account.account.username(), + flags & !UF_ACCOUNTDISABLE, + )?; } let token = with_owner_impersonation(owner_token, || { let mut pins = Vec::new(); @@ -82,7 +127,21 @@ pub(crate) fn prepare(owner_token: HANDLE, record: &InstallationRecord) -> Resul &record.codex_home, account.account, ) - })?; + }); + if flags & UF_ACCOUNTDISABLE != 0 { + super::validate_account_sid(&account)?; + let current_flags = + codex_windows_sandbox::local_user_flags(account.account.username())? + .context("cleanup account disappeared before flag restoration")?; + codex_windows_sandbox::set_local_user_flags( + account.account.username(), + current_flags | UF_ACCOUNTDISABLE, + ) + .context("restore disabled sandbox account after cleanup logon")?; + record.runtime_mut()?.accounts[index].cleanup_logon_pending = false; + crate::installation_record::save_runtime(record)?; + } + let token = token?; super::validate_target( token.as_raw_handle() as _, account.account, @@ -94,6 +153,9 @@ pub(crate) fn prepare(owner_token: HANDLE, record: &InstallationRecord) -> Resul })); tokens.push(token); } + // Flag recovery is durable before the in-memory retirement generation exists. + record.runtime_mut()?.ready_package = None; + record.runtime_mut()?.retiring = Some(format!("{:?}", windows::core::GUID::new()?)); let group_sid = if tokens.is_empty() { None } else { diff --git a/codex-rs/windows-sandbox-service/src/service/runtime_lifecycle.rs b/codex-rs/windows-sandbox-service/src/service/runtime_lifecycle.rs index f6f3341c19..76727cc21d 100644 --- a/codex-rs/windows-sandbox-service/src/service/runtime_lifecycle.rs +++ b/codex-rs/windows-sandbox-service/src/service/runtime_lifecycle.rs @@ -53,7 +53,9 @@ pub(super) fn foreground_owner( pub(super) fn run(state: &ServiceState, package_lifecycle: &PackageLifecycle) -> Result<()> { let cleaned = Cell::new(false); - let restore_owner = || { + let last_cleanup_error = Cell::new(None); + let restore_owner = || -> Result<()> { + crate::registered_runtime::restore_disabled_accounts()?; let restored = crate::installation_record::load().and_then(|record| { record.map_or(Ok(()), |record| { package_lifecycle.restore_logged_in_owner(record.session_id) @@ -65,11 +67,12 @@ pub(super) fn run(state: &ServiceState, package_lifecycle: &PackageLifecycle) -> &format!("unable to restore package uninstall listener: {error:#}"), ); } + Ok(()) }; crate::ipc::run( Arc::clone(&state.shutdown), || { - restore_owner(); + restore_owner()?; state.report_status(SERVICE_RUNNING, NO_ERROR)?; log_information( EVENT_SERVICE_STARTED, @@ -81,8 +84,9 @@ pub(super) fn run(state: &ServiceState, package_lifecycle: &PackageLifecycle) -> package_lifecycle.register_authenticated_user(installation, user_token) }, || loop { - restore_owner(); + restore_owner()?; if !crate::package_lifecycle::runtime_owner_removed()? { + last_cleanup_error.set(None); return Ok(()); } let Err(error) = package_lifecycle.clean_up() else { @@ -97,10 +101,7 @@ pub(super) fn run(state: &ServiceState, package_lifecycle: &PackageLifecycle) -> }) { return Err(error); } - log_error( - EVENT_SERVICE_FAILED, - &format!("sandbox cleanup deferred: {error:#}"), - ); + log_cleanup_error(&error, &last_cleanup_error); // Keep the pipe available during pre-fence backoff. A reinstall // resumes admission on the next owner check; SCM stops are never reset. if !wait_for_cleanup_retry(state) { @@ -122,6 +123,7 @@ pub(super) fn run(state: &ServiceState, package_lifecycle: &PackageLifecycle) -> /// Retries the current teardown step, never a phase recovered from stored intent. pub(crate) fn retry_cleanup(mut operation: impl FnMut() -> Result<()>) -> Result<()> { let mut stop_failures = 0; + let last_cleanup_error = Cell::new(None); loop { let Err(error) = operation() else { return Ok(()); @@ -129,10 +131,7 @@ pub(crate) fn retry_cleanup(mut operation: impl FnMut() -> Result<()>) -> Result let Some(state) = SERVICE_STATE.get() else { return Err(error); }; - log_error( - EVENT_SERVICE_FAILED, - &format!("sandbox cleanup deferred: {error:#}"), - ); + log_cleanup_error(&error, &last_cleanup_error); if state.stop_requested.load(Ordering::Acquire) { // SCM stop initiates uninstall cleanup rather than cancelling it. // Four one-second retry waits fit within the 10-second SCM wait hint. @@ -147,6 +146,15 @@ pub(crate) fn retry_cleanup(mut operation: impl FnMut() -> Result<()>) -> Result } } +fn log_cleanup_error(error: &anyhow::Error, previous: &Cell>) { + let message = format!("sandbox cleanup deferred: {error:#}"); + // Retrying an unchanged failure must not flood the Windows event log. + if previous.take().as_deref() != Some(message.as_str()) { + log_error(EVENT_SERVICE_FAILED, &message); + } + previous.set(Some(message)); +} + fn wait_for_cleanup_retry(state: &ServiceState) -> bool { for _ in 0..30 { if state.shutdown.load(Ordering::Acquire) {