From 6c0b45a79c63f73163e5267b1ba1254b141eb8a7 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:46:53 -0800 Subject: [PATCH 1/3] fix(service): retry Windows pipe spawn on packaged access denied create_process_refused_job treats both 5 and 0xC0070005 as a job-list refusal, and the Windows spawn path uses it to choose the jobless retry. When either code comes back from a job-backed attempt, the process is created again with no job attribute. A unit test covers both codes, the signed value -1073283067, that same code read from an io::Error, and non-matches for Some(2) and None. Fixes #63892 --- packages/process-daemon/src/service/pipe.rs | 22 +++++++++++++++++++ .../src/service/pipe/windows.rs | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index f2a39135c580..2fa7ab1a97b7 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -46,6 +46,15 @@ pub(in crate::service) use windows::Process; /// How long output is still read after the child exits. const DRAIN_AFTER_EXIT: Duration = Duration::from_secs(1); +/// Whether `CreateProcessW` refused `PROC_THREAD_ATTRIBUTE_JOB_LIST`. +/// +/// Win32 reports that as `ERROR_ACCESS_DENIED` (5). A packaged executable can +/// report the same refusal as `NTSTATUS_FROM_WIN32(5)` (`0xC0070005`). +#[cfg(any(windows, test))] +fn create_process_refused_job(code: Option) -> bool { + code == Some(5) || code == Some(0xC007_0005_u32 as i32) +} + /// The output file and the actor queue, in the order every byte goes through /// them: the file first, so it always holds at least what subscribers have /// seen, even when the actor is backpressured. @@ -343,4 +352,17 @@ mod tests { write_counting(&mut writer, b"x\ny\n", &mut newlines).unwrap(); assert_eq!(newlines, 3); } + + #[test] + fn create_process_refused_job_covers_packaged_ntstatus() { + assert!(create_process_refused_job(Some(5))); + let packaged = 0xC007_0005_u32 as i32; + assert_eq!(packaged, -1073283067); + assert!(create_process_refused_job(Some(packaged))); + assert!(create_process_refused_job( + io::Error::from_raw_os_error(packaged).raw_os_error() + )); + assert!(!create_process_refused_job(Some(2))); + assert!(!create_process_refused_job(None)); + } } diff --git a/packages/process-daemon/src/service/pipe/windows.rs b/packages/process-daemon/src/service/pipe/windows.rs index 2d55e6768dd9..de9513c4729c 100644 --- a/packages/process-daemon/src/service/pipe/windows.rs +++ b/packages/process-daemon/src/service/pipe/windows.rs @@ -54,7 +54,7 @@ use windows_sys::Win32::System::Threading::{ WaitForSingleObject, }; -use super::{DRAIN_AFTER_EXIT, Exit, OutputSink, Waiter, start_pipe}; +use super::{DRAIN_AFTER_EXIT, Exit, OutputSink, Waiter, create_process_refused_job, start_pipe}; use crate::service::{CreatePipe, ProcessSignal, TerminalHandle, TerminalId}; use crate::transport::retained::Retained; use crate::transport::windows::{event, owned_handle, security::PrivateSecurity, wide}; @@ -226,7 +226,7 @@ fn spawn( ) -> Result { let (created, job) = match create_process(request, job.as_ref(), &client) { Ok(created) => (created, job), - Err(error) if job.is_some() && error.raw_os_error() == Some(ERROR_ACCESS_DENIED as i32) => { + Err(error) if job.is_some() && create_process_refused_job(error.raw_os_error()) => { // An outer job with UI restrictions refuses new members. Run the // command anyway; only the main process can be killed then. let created = create_process(request, None, &client) From 46d978c9134d5ced7e17d89179734d9896b29191 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:43:55 -0800 Subject: [PATCH 2/3] fix(service): keep packaged pipe children in the job When CreateProcessW refuses the job attribute, create the process suspended, assign it to the job with AssignProcessToJobObject, then resume it. Store PowerShell rejects the attribute but accepts the assignment, so cancel and timeout still kill its whole tree. The job is dropped only when the assignment fails too, as under an outer job with UI restrictions, and only that case logs that the job cannot be joined. --- .../src/service/pipe/windows.rs | 87 ++++++++++++++----- 1 file changed, 66 insertions(+), 21 deletions(-) diff --git a/packages/process-daemon/src/service/pipe/windows.rs b/packages/process-daemon/src/service/pipe/windows.rs index de9513c4729c..0a417b1509df 100644 --- a/packages/process-daemon/src/service/pipe/windows.rs +++ b/packages/process-daemon/src/service/pipe/windows.rs @@ -38,20 +38,20 @@ use windows_sys::Win32::Storage::FileSystem::{ }; use windows_sys::Win32::System::IO::{CancelIoEx, GetOverlappedResult, OVERLAPPED}; use windows_sys::Win32::System::JobObjects::{ - CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, JOBOBJECT_BASIC_LIMIT_INFORMATION, - JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation, - SetInformationJobObject, TerminateJobObject, + AssignProcessToJobObject, CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, + JOBOBJECT_BASIC_LIMIT_INFORMATION, JOBOBJECT_EXTENDED_LIMIT_INFORMATION, + JobObjectExtendedLimitInformation, SetInformationJobObject, TerminateJobObject, }; use windows_sys::Win32::System::Pipes::{ CreateNamedPipeW, PIPE_READMODE_BYTE, PIPE_REJECT_REMOTE_CLIENTS, PIPE_TYPE_BYTE, PIPE_WAIT, }; use windows_sys::Win32::System::Threading::{ - CREATE_NO_WINDOW, CREATE_UNICODE_ENVIRONMENT, CreateProcessW, DeleteProcThreadAttributeList, - EXTENDED_STARTUPINFO_PRESENT, GetCurrentProcess, GetExitCodeProcess, INFINITE, - InitializeProcThreadAttributeList, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, - PROC_THREAD_ATTRIBUTE_JOB_LIST, PROCESS_INFORMATION, ResetEvent, STARTF_USESTDHANDLES, - STARTUPINFOEXW, SetEvent, TerminateProcess, UpdateProcThreadAttribute, WaitForMultipleObjects, - WaitForSingleObject, + CREATE_NO_WINDOW, CREATE_SUSPENDED, CREATE_UNICODE_ENVIRONMENT, CreateProcessW, + DeleteProcThreadAttributeList, EXTENDED_STARTUPINFO_PRESENT, GetCurrentProcess, + GetExitCodeProcess, INFINITE, InitializeProcThreadAttributeList, + PROC_THREAD_ATTRIBUTE_HANDLE_LIST, PROC_THREAD_ATTRIBUTE_JOB_LIST, PROCESS_INFORMATION, + ResetEvent, ResumeThread, STARTF_USESTDHANDLES, STARTUPINFOEXW, SetEvent, TerminateProcess, + UpdateProcThreadAttribute, WaitForMultipleObjects, WaitForSingleObject, }; use super::{DRAIN_AFTER_EXIT, Exit, OutputSink, Waiter, create_process_refused_job, start_pipe}; @@ -224,18 +224,33 @@ fn spawn( job: Option, client: OwnedHandle, ) -> Result { - let (created, job) = match create_process(request, job.as_ref(), &client) { + let (created, job) = match create_process(request, job.as_ref(), &client, false) { Ok(created) => (created, job), Err(error) if job.is_some() && create_process_refused_job(error.raw_os_error()) => { - // An outer job with UI restrictions refuses new members. Run the - // command anyway; only the main process can be killed then. - let created = create_process(request, None, &client) + // A packaged executable refuses the job attribute but can still + // join the job once created; an outer job with UI restrictions + // refuses both. The child starts suspended, so it cannot start + // anything outside the job before it is assigned. + let created = create_process(request, None, &client, true) .with_context(|| format!("failed to spawn {}", request.program))?; - eprintln!( - "pipe process {id}: cannot join the job object, killing only the main process: {error}" - ); - drop(job); - (created, None) + let job = job.and_then(|job| match job.assign(&created.process) { + Ok(()) => Some(job), + Err(assign) => { + eprintln!( + "pipe process {id}: cannot join the job object, killing only the main process: {error}; {assign}" + ); + None + } + }); + if let Err(error) = created.resume() { + // SAFETY: the process handle is open; a failed kill leaves + // nothing more to clean up here. + unsafe { + TerminateProcess(created.process.as_raw_handle(), 1); + } + return Err(error).context(format!("failed to resume {}", request.program)); + } + (created, job) } Err(error) => return Err(error).context(format!("failed to spawn {}", request.program)), }; @@ -258,12 +273,29 @@ fn spawn( struct Spawned { pid: u32, process: OwnedHandle, + /// The main thread, kept only while a suspended process waits to resume. + thread: Option, +} + +impl Spawned { + /// Starts a process created suspended. A no-op for any other process. + fn resume(&self) -> io::Result<()> { + let Some(thread) = &self.thread else { + return Ok(()); + }; + // SAFETY: the thread handle stays open through the call. + if unsafe { ResumeThread(thread.as_raw_handle()) } == u32::MAX { + return Err(io::Error::last_os_error()); + } + Ok(()) + } } fn create_process( request: &CreatePipe, job: Option<&Job>, client: &OwnedHandle, + suspended: bool, ) -> io::Result { let stdin = null_handle()?; // A child that closes one of stdout or stderr must still write to the @@ -297,7 +329,10 @@ fn create_process( null(), null(), 1, - CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT, + CREATE_NO_WINDOW + | CREATE_UNICODE_ENVIRONMENT + | EXTENDED_STARTUPINFO_PRESENT + | if suspended { CREATE_SUSPENDED } else { 0 }, environment.as_ptr().cast(), directory.as_ptr(), (&raw const startup).cast(), @@ -308,12 +343,13 @@ fn create_process( return Err(io::Error::last_os_error()); } // SAFETY: CreateProcessW returned new, uniquely owned handles; the thread - // handle is not needed. + // handle is needed only to resume a suspended process. let process = owned_handle(info.hProcess)?; - drop(owned_handle(info.hThread)?); + let thread = owned_handle(info.hThread)?; Ok(Spawned { pid: info.dwProcessId, process, + thread: suspended.then_some(thread), }) } @@ -693,6 +729,15 @@ impl Job { fn handle(&self) -> HANDLE { self.0.as_raw_handle() } + + /// Adds a process created without the job attribute to the job. + fn assign(&self, process: &OwnedHandle) -> io::Result<()> { + // SAFETY: both handles stay open through the call. + if unsafe { AssignProcessToJobObject(self.handle(), process.as_raw_handle()) } == 0 { + return Err(io::Error::last_os_error()); + } + Ok(()) + } } fn warn_no_job(id: TerminalId, error: &io::Error) { From fb9f24776d24444e913909bb37e7c58af22a6ec6 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:52:15 -0800 Subject: [PATCH 3/3] refactor(service): inline the packaged job refusal check Drop the create_process_refused_job helper and its constants test from pipe.rs and check both refusal codes next to the spawn retry in windows.rs. --- packages/process-daemon/src/service/pipe.rs | 22 ------------------- .../src/service/pipe/windows.rs | 9 ++++++-- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index 2fa7ab1a97b7..f2a39135c580 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -46,15 +46,6 @@ pub(in crate::service) use windows::Process; /// How long output is still read after the child exits. const DRAIN_AFTER_EXIT: Duration = Duration::from_secs(1); -/// Whether `CreateProcessW` refused `PROC_THREAD_ATTRIBUTE_JOB_LIST`. -/// -/// Win32 reports that as `ERROR_ACCESS_DENIED` (5). A packaged executable can -/// report the same refusal as `NTSTATUS_FROM_WIN32(5)` (`0xC0070005`). -#[cfg(any(windows, test))] -fn create_process_refused_job(code: Option) -> bool { - code == Some(5) || code == Some(0xC007_0005_u32 as i32) -} - /// The output file and the actor queue, in the order every byte goes through /// them: the file first, so it always holds at least what subscribers have /// seen, even when the actor is backpressured. @@ -352,17 +343,4 @@ mod tests { write_counting(&mut writer, b"x\ny\n", &mut newlines).unwrap(); assert_eq!(newlines, 3); } - - #[test] - fn create_process_refused_job_covers_packaged_ntstatus() { - assert!(create_process_refused_job(Some(5))); - let packaged = 0xC007_0005_u32 as i32; - assert_eq!(packaged, -1073283067); - assert!(create_process_refused_job(Some(packaged))); - assert!(create_process_refused_job( - io::Error::from_raw_os_error(packaged).raw_os_error() - )); - assert!(!create_process_refused_job(Some(2))); - assert!(!create_process_refused_job(None)); - } } diff --git a/packages/process-daemon/src/service/pipe/windows.rs b/packages/process-daemon/src/service/pipe/windows.rs index 0a417b1509df..6bacec91490a 100644 --- a/packages/process-daemon/src/service/pipe/windows.rs +++ b/packages/process-daemon/src/service/pipe/windows.rs @@ -54,7 +54,7 @@ use windows_sys::Win32::System::Threading::{ UpdateProcThreadAttribute, WaitForMultipleObjects, WaitForSingleObject, }; -use super::{DRAIN_AFTER_EXIT, Exit, OutputSink, Waiter, create_process_refused_job, start_pipe}; +use super::{DRAIN_AFTER_EXIT, Exit, OutputSink, Waiter, start_pipe}; use crate::service::{CreatePipe, ProcessSignal, TerminalHandle, TerminalId}; use crate::transport::retained::Retained; use crate::transport::windows::{event, owned_handle, security::PrivateSecurity, wide}; @@ -226,7 +226,12 @@ fn spawn( ) -> Result { let (created, job) = match create_process(request, job.as_ref(), &client, false) { Ok(created) => (created, job), - Err(error) if job.is_some() && create_process_refused_job(error.raw_os_error()) => { + // A packaged executable reports the refusal as NTSTATUS_FROM_WIN32(5). + Err(error) + if job.is_some() + && (error.raw_os_error() == Some(ERROR_ACCESS_DENIED as i32) + || error.raw_os_error() == Some(0xC007_0005_u32 as i32)) => + { // A packaged executable refuses the job attribute but can still // join the job once created; an outer job with UI restrictions // refuses both. The child starts suspended, so it cannot start