From 781e513c8af9cbdf7b1da2408215f8d9d90e9ae8 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 15 Oct 2026 23:08:19 +0800 Subject: [PATCH 1/3] fix(ci): avoid Windows command bootstrap stalls --- CHANGELOG.md | 2 + README.md | 8 +- scripts/owned-command-windows.cs | 253 --------------- scripts/owned-command-windows.ps1 | 297 ++++++++++++++---- scripts/owned-command.mjs | 130 +++++--- .../owned-command-compiler-observer.ps1 | 16 +- test/owned-command.test.mjs | 223 ++++++++++--- test/plugin-inspector-checkout-lock.test.mjs | 76 +++++ 8 files changed, 591 insertions(+), 414 deletions(-) delete mode 100644 scripts/owned-command-windows.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 80f69cb33..e1a315149 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.2.2 - Unreleased +- Avoid Windows command startup failures caused by per-command compiler and JSON cmdlet loading, preserving the startup budget, atomic Job admission and confirmed cleanup before checkout reuse. + - Adopt published Plugin Inspector 0.4.0 and qualify retirement notices in source, packed, and published-package smoke: deprecated SDK member, replacement guidance, and final compatibility day remain non-blocking and author-facing. Refresh executable workspace-plan helper paths and operating docs to the release pin. - Assert DingTalk's explicit credential-free Gateway rejection contracts through method-scoped Inspector expectations, preserving unexpected response and handler failures. diff --git a/README.md b/README.md index e904696c3..3098fb116 100644 --- a/README.md +++ b/README.md @@ -215,7 +215,7 @@ not proof of process-group extinction. Failed commands include a bounded `error.startupTrace` with fixed phase names, emission timestamps (`atMs`) and parent observation timestamps (`observedAtMs`). These are same-host wall-clock milliseconds, not CPU measurements or deadline -extensions. Windows phases distinguish parsing, bootstrap containment, compilation +extensions. Windows phases distinguish native binding preparation, request parsing and native process creation. Missing later phases remain unknown; a phase marker never proves Job extinction. The parent retains received phases if its Worker dies. Contextual timeout errors retain the original error as `cause`. @@ -229,8 +229,10 @@ removing `.crabpot/plugin-inspector/.checkout.lock` and retrying preparation. Windows requires 64-bit Windows 10/Server 2016 or newer and Windows PowerShell: commands enter their private Job at creation, and setup failures never fall back -to uncontained execution. A separate helper Job contains bootstrap compiler -children too. Native Git runs directly; only batch commands use `cmd.exe`. +to uncontained execution. Native bindings and framework JSON serialization prepare +without loading cmdlets or starting a compiler; the parent then issues one request. +A definite failed admission releases checkout custody only after setup cleanup, +helper closure and control-pipe drain. Missing or invalid terminal receipts retain it. Native Git runs directly; only batch commands use `cmd.exe`. Windows Jobs retain owner-loss cleanup independently of the Worker. This process ownership is not a sandbox for hostile plugin code. diff --git a/scripts/owned-command-windows.cs b/scripts/owned-command-windows.cs deleted file mode 100644 index 6edc8bb3c..000000000 --- a/scripts/owned-command-windows.cs +++ /dev/null @@ -1,253 +0,0 @@ -using System; -using System.ComponentModel; -using System.Diagnostics; -using System.IO; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; - -// One command, one Job. The PowerShell controller stays outside the Job and -// admits the root atomically; the child never inherits the controller pipe. -public static class CrabpotCommandJob -{ - [StructLayout(LayoutKind.Sequential)] - struct BasicLimits - { - public long ProcessTime, JobTime; - public uint Flags; - public UIntPtr MinimumWorkingSet, MaximumWorkingSet; - public uint ActiveProcessLimit; - public UIntPtr Affinity; - public uint PriorityClass, SchedulingClass; - } - [StructLayout(LayoutKind.Sequential)] - struct IoCounters - { - public ulong ReadOperations, WriteOperations, OtherOperations; - public ulong ReadBytes, WriteBytes, OtherBytes; - } - [StructLayout(LayoutKind.Sequential)] - struct ExtendedLimits - { - public BasicLimits Basic; - public IoCounters Io; - public UIntPtr ProcessMemory, JobMemory, PeakProcessMemory, PeakJobMemory; - } - [StructLayout(LayoutKind.Sequential)] - struct Accounting - { - public long UserTime, KernelTime, PeriodUserTime, PeriodKernelTime; - public uint PageFaults, TotalProcesses, ActiveProcesses, TerminatedProcesses; - } - [StructLayout(LayoutKind.Sequential)] - struct StartupInfo - { - public uint Size; - public IntPtr Reserved, Desktop, Title; - public uint X, Y, XSize, YSize, XCount, YCount, Fill, Flags; - public ushort ShowWindow, ReservedSize; - public IntPtr ReservedBytes, Stdin, Stdout, Stderr; - } - [StructLayout(LayoutKind.Sequential)] - struct StartupInfoEx - { - public StartupInfo Startup; - public IntPtr Attributes; - } - [StructLayout(LayoutKind.Sequential)] - struct ProcessInfo - { - public IntPtr Process, Thread; - public uint ProcessId, ThreadId; - } - - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - static extern IntPtr CreateJobObjectW(IntPtr security, string name); - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool SetInformationJobObject(IntPtr job, int type, ref ExtendedLimits info, uint size); - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool QueryInformationJobObject(IntPtr job, int type, out Accounting info, uint size, IntPtr length); - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool TerminateJobObject(IntPtr job, uint code); - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool CloseHandle(IntPtr handle); - [DllImport("kernel32.dll")] - static extern IntPtr GetCurrentProcess(); - [DllImport("kernel32.dll", SetLastError = true)] - static extern IntPtr GetStdHandle(int stream); - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool DuplicateHandle(IntPtr sourceProcess, IntPtr source, IntPtr targetProcess, - out IntPtr target, uint access, bool inherit, uint options); - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool InitializeProcThreadAttributeList(IntPtr list, int count, uint flags, ref IntPtr size); - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool UpdateProcThreadAttribute(IntPtr list, uint flags, IntPtr attribute, - IntPtr value, IntPtr size, IntPtr previous, IntPtr returned); - [DllImport("kernel32.dll")] - static extern void DeleteProcThreadAttributeList(IntPtr list); - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - static extern bool CreateProcessW(string application, StringBuilder commandLine, IntPtr processSecurity, - IntPtr threadSecurity, bool inheritHandles, uint flags, IntPtr environment, - string cwd, ref StartupInfoEx startup, out ProcessInfo process); - [DllImport("kernel32.dll", SetLastError = true)] - static extern uint WaitForSingleObject(IntPtr handle, uint milliseconds); - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool GetExitCodeProcess(IntPtr handle, out uint code); - - static void Check(bool success, string operation) - { - if (!success) throw new Win32Exception(Marshal.GetLastWin32Error(), operation); - } - - static uint Active(IntPtr job) - { - Accounting accounting; - Check(QueryInformationJobObject(job, 1, out accounting, - (uint)Marshal.SizeOf(typeof(Accounting)), IntPtr.Zero), "QueryInformationJobObject"); - return accounting.ActiveProcesses; - } - - static void TerminateAndObserve(IntPtr job, int cleanup) - { - Check(TerminateJobObject(job, 1), "TerminateJobObject"); - Stopwatch wait = Stopwatch.StartNew(); - while (Active(job) != 0) - { - if (wait.ElapsedMilliseconds >= cleanup) - throw new TimeoutException("Windows Job extinction was not observed"); - Thread.Sleep(10); - } - } - - static void Phase(StreamWriter receipt, string name) - { - try { receipt.WriteLine("PHASE " + name + " " + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); } - catch (IOException) { } - catch (ObjectDisposedException) { } - } - - public static void Run(string application, string commandLine, string cwd, string environment, - int timeout, int cleanup, CancellationTokenSource ownerLost, - CancellationTokenRegistration bootstrapKill, StreamWriter receipt) - { - Phase(receipt, "native-entered"); - if (IntPtr.Size != 8 || Marshal.SizeOf(typeof(StartupInfoEx)) != 112 || - Marshal.SizeOf(typeof(ExtendedLimits)) != 144 || Marshal.SizeOf(typeof(Accounting)) != 48) - throw new PlatformNotSupportedException("Windows Job ownership requires a 64-bit Windows runtime"); - - IntPtr job = IntPtr.Zero, attributes = IntPtr.Zero, handles = IntPtr.Zero; - IntPtr jobList = IntPtr.Zero, environmentBlock = IntPtr.Zero; - IntPtr[] inherited = new IntPtr[3]; - ProcessInfo child = new ProcessInfo(); - bool initialized = false, created = false, extinct = false; - // Transfer from pre-compilation self-termination to explicit Job cleanup. - // Dispose synchronizes with any bootstrap callback already in flight. - bootstrapKill.Dispose(); - if (ownerLost.IsCancellationRequested) - throw new IOException("command owner disconnected before admission"); - try - { - job = CreateJobObjectW(IntPtr.Zero, null); - Check(job != IntPtr.Zero, "CreateJobObjectW"); - ExtendedLimits limits = new ExtendedLimits(); - limits.Basic.Flags = 0x2000; // KILL_ON_JOB_CLOSE, with neither breakaway flag. - Check(SetInformationJobObject(job, 9, ref limits, - (uint)Marshal.SizeOf(typeof(ExtendedLimits))), "SetInformationJobObject"); - - IntPtr self = GetCurrentProcess(); - for (int i = 0; i < 3; i++) - Check(DuplicateHandle(self, GetStdHandle(-10 - i), self, out inherited[i], - 0, true, 2), "DuplicateHandle(stdio)"); - - IntPtr bytes = IntPtr.Zero; - InitializeProcThreadAttributeList(IntPtr.Zero, 2, 0, ref bytes); - Check(bytes != IntPtr.Zero, "InitializeProcThreadAttributeList(size)"); - attributes = Marshal.AllocHGlobal(bytes); - Check(InitializeProcThreadAttributeList(attributes, 2, 0, ref bytes), - "InitializeProcThreadAttributeList"); - initialized = true; - handles = Marshal.AllocHGlobal(IntPtr.Size * 3); - for (int i = 0; i < 3; i++) Marshal.WriteIntPtr(handles, i * IntPtr.Size, inherited[i]); - jobList = Marshal.AllocHGlobal(IntPtr.Size); - Marshal.WriteIntPtr(jobList, job); - Check(UpdateProcThreadAttribute(attributes, 0, new IntPtr(0x20002), - handles, new IntPtr(IntPtr.Size * 3), IntPtr.Zero, IntPtr.Zero), "HANDLE_LIST"); - Check(UpdateProcThreadAttribute(attributes, 0, new IntPtr(0x2000d), - jobList, new IntPtr(IntPtr.Size), IntPtr.Zero, IntPtr.Zero), "JOB_LIST"); - - StartupInfoEx startup = new StartupInfoEx(); - startup.Startup.Size = (uint)Marshal.SizeOf(typeof(StartupInfoEx)); - startup.Startup.Flags = 0x100; - startup.Startup.Stdin = inherited[0]; - startup.Startup.Stdout = inherited[1]; - startup.Startup.Stderr = inherited[2]; - startup.Attributes = attributes; - environmentBlock = Marshal.StringToHGlobalUni(environment); - Phase(receipt, "create-process-pending"); - if (ownerLost.IsCancellationRequested) - throw new IOException("command owner disconnected before admission"); - Check(CreateProcessW(application, new StringBuilder(commandLine), IntPtr.Zero, - IntPtr.Zero, true, 0x08080400, environmentBlock, cwd, ref startup, out child), - "CreateProcessW(JOB_LIST)"); - created = true; - Phase(receipt, "create-process-completed"); - receipt.WriteLine("READY " + child.ProcessId); - // Backing HANDLE arrays stay alive through CreateProcess and attribute deletion. - DeleteProcThreadAttributeList(attributes); - initialized = false; - for (int i = 0; i < 3; i++) - { - Check(CloseHandle(inherited[i]), "CloseHandle(child stdio)"); - inherited[i] = IntPtr.Zero; - } - Check(CloseHandle(child.Thread), "CloseHandle(child thread)"); - child.Thread = IntPtr.Zero; - - Stopwatch clock = Stopwatch.StartNew(); - bool timedOut = false; - for (;;) - { - uint wait = WaitForSingleObject(child.Process, 0); - if (wait == 0) break; - Check(wait == 258, "WaitForSingleObject"); - if (ownerLost.IsCancellationRequested || - clock.ElapsedMilliseconds >= timeout) - { - timedOut = clock.ElapsedMilliseconds >= timeout; - TerminateAndObserve(job, cleanup); - extinct = true; - break; - } - Thread.Sleep(10); - } - uint code; - Check(GetExitCodeProcess(child.Process, out code), "GetExitCodeProcess"); - receipt.WriteLine("EXIT " + code); - if (!extinct) - { - if (Active(job) != 0) TerminateAndObserve(job, cleanup); - extinct = true; - } - if (timedOut) receipt.WriteLine("TIMEOUT"); - receipt.WriteLine("CLOSED"); - } - finally - { - // Owner loss or receipt failure still performs bounded explicit cleanup. - // KILL_ON_JOB_CLOSE is only the last fallback, never a success receipt. - try { if (created && !extinct) TerminateAndObserve(job, cleanup); } - finally - { - if (initialized) DeleteProcThreadAttributeList(attributes); - foreach (IntPtr handle in inherited) if (handle != IntPtr.Zero) CloseHandle(handle); - if (child.Thread != IntPtr.Zero) CloseHandle(child.Thread); - if (child.Process != IntPtr.Zero) CloseHandle(child.Process); - if (job != IntPtr.Zero) CloseHandle(job); - if (attributes != IntPtr.Zero) Marshal.FreeHGlobal(attributes); - if (handles != IntPtr.Zero) Marshal.FreeHGlobal(handles); - if (jobList != IntPtr.Zero) Marshal.FreeHGlobal(jobList); - if (environmentBlock != IntPtr.Zero) Marshal.FreeHGlobal(environmentBlock); - } - } - } -} diff --git a/scripts/owned-command-windows.ps1 b/scripts/owned-command-windows.ps1 index ea3bac017..aa4636770 100644 --- a/scripts/owned-command-windows.ps1 +++ b/scripts/owned-command-windows.ps1 @@ -5,107 +5,280 @@ $pipe = [System.IO.Pipes.NamedPipeClientStream]::new( $reader = $null $writer = $null $bootstrapKill = $null +$attempted = $false +$nativeReturned = $false +$created = $false +$setupReleased = $true try { $pipe.Connect(5000) $utf8 = [System.Text.UTF8Encoding]::new($false) $reader = [System.IO.StreamReader]::new($pipe, $utf8, $false, 4096, $true) $writer = [System.IO.StreamWriter]::new($pipe, $utf8, 4096, $true) $writer.AutoFlush = $true - $line = $reader.ReadLine() - if ($null -eq $line) { exit 1 } - # Direct .NET delegates remain responsive while JSON parsing or compilation blocks. - $ownerLost = [System.Threading.CancellationTokenSource]::new() + # No request is sent until PREPARED. Direct .NET callbacks still stop an + # abandoned helper while native binding or serializer preparation is blocked. $killSelf = [System.Delegate]::CreateDelegate([System.Action], [System.Diagnostics.Process]::GetCurrentProcess(), "Kill") - $cancelOwner = [System.Delegate]::CreateDelegate([System.Action], $ownerLost, "Cancel") - $bootstrapKill = $ownerLost.Token.Register($killSelf) - $ownerRead = $reader.ReadLineAsync() - $ownerRead.ConfigureAwait($false).GetAwaiter().OnCompleted($cancelOwner) - # Best-effort phase observations never replace the owner-loss watcher or receipts. + $preparing = [System.Threading.CancellationTokenSource]::new() + $cancelPreparation = [System.Delegate]::CreateDelegate([System.Action], $preparing, "Cancel") + $bootstrapKill = $preparing.Token.Register($killSelf) + $requestRead = $reader.ReadLineAsync() + $requestRead.ConfigureAwait($false).GetAwaiter().OnCompleted($cancelPreparation) function Write-Phase([string]$Name) { try { $writer.WriteLine("PHASE " + $Name + " " + [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()) } catch {} } - Write-Phase "request-read" - Write-Phase "parser-started" - $request = $line | ConvertFrom-Json - Write-Phase "parser-completed" - # CodeDOM starts csc.exe. Contain this dedicated helper before compiling the - # command owner; emitting these five fixed declarations starts no compiler. + function Check-Native([bool]$Success, [string]$Operation) { + if (-not $Success) { + throw [System.ComponentModel.Win32Exception]::new( + [System.Runtime.InteropServices.Marshal]::GetLastWin32Error(), $Operation) + } + } + Write-Phase "preparation-started" if ([IntPtr]::Size -ne 8) { throw "Windows Job ownership requires a 64-bit Windows runtime" } $assembly = [AppDomain]::CurrentDomain.DefineDynamicAssembly( - [System.Reflection.AssemblyName]::new("CrabpotBootstrapNative"), - [System.Reflection.Emit.AssemblyBuilderAccess]::Run) - $builder = $assembly.DefineDynamicModule("Native").DefineType( - "CrabpotBootstrapNative", [System.Reflection.TypeAttributes]"Public, Abstract, Sealed") + [System.Reflection.AssemblyName]::new("CrabpotCommandNative"), [System.Reflection.Emit.AssemblyBuilderAccess]::Run) + $module = $assembly.DefineDynamicModule("Native") + function Define-Struct([string]$Name, [object[]]$Fields) { + $builder = $module.DefineType($Name, + [System.Reflection.TypeAttributes]"Public, SequentialLayout, Sealed", [ValueType]) + foreach ($field in $Fields) { [void]$builder.DefineField($field[0], $field[1], [System.Reflection.FieldAttributes]::Public) } + return $builder.CreateType() + } + $basicType = Define-Struct "BasicLimits" @( + @("ProcessTime", [long]), @("JobTime", [long]), @("Flags", [uint32]), + @("MinimumWorkingSet", [UIntPtr]), @("MaximumWorkingSet", [UIntPtr]), @("ActiveProcessLimit", [uint32]), + @("Affinity", [UIntPtr]), @("PriorityClass", [uint32]), @("SchedulingClass", [uint32])) + $ioType = Define-Struct "IoCounters" @( + @("ReadOperations", [uint64]), @("WriteOperations", [uint64]), @("OtherOperations", [uint64]), + @("ReadBytes", [uint64]), @("WriteBytes", [uint64]), @("OtherBytes", [uint64])) + $limitsType = Define-Struct "ExtendedLimits" @( + @("Basic", $basicType), @("Io", $ioType), @("ProcessMemory", [UIntPtr]), @("JobMemory", [UIntPtr]), + @("PeakProcessMemory", [UIntPtr]), @("PeakJobMemory", [UIntPtr])) + $accountingType = Define-Struct "Accounting" @( + @("UserTime", [long]), @("KernelTime", [long]), @("PeriodUserTime", [long]), @("PeriodKernelTime", [long]), + @("PageFaults", [uint32]), @("TotalProcesses", [uint32]), @("ActiveProcesses", [uint32]), @("TerminatedProcesses", [uint32])) + $startupType = Define-Struct "StartupInfo" @( + @("Size", [uint32]), @("Reserved", [IntPtr]), @("Desktop", [IntPtr]), @("Title", [IntPtr]), + @("X", [uint32]), @("Y", [uint32]), @("XSize", [uint32]), @("YSize", [uint32]), + @("XCount", [uint32]), @("YCount", [uint32]), @("Fill", [uint32]), @("Flags", [uint32]), + @("ShowWindow", [uint16]), @("ReservedSize", [uint16]), @("ReservedBytes", [IntPtr]), + @("Stdin", [IntPtr]), @("Stdout", [IntPtr]), @("Stderr", [IntPtr])) + $startupExType = Define-Struct "StartupInfoEx" @(@("Startup", $startupType), @("Attributes", [IntPtr])) + $processType = Define-Struct "ProcessInfo" @( + @("Process", [IntPtr]), @("Thread", [IntPtr]), @("ProcessId", [uint32]), @("ThreadId", [uint32])) + if ([System.Runtime.InteropServices.Marshal]::SizeOf($startupExType) -ne 112 -or + [System.Runtime.InteropServices.Marshal]::SizeOf($limitsType) -ne 144 -or + [System.Runtime.InteropServices.Marshal]::SizeOf($accountingType) -ne 48) { throw "Windows Job ownership ABI mismatch" } + $builder = $module.DefineType("CrabpotCommandNative", [System.Reflection.TypeAttributes]"Public, Abstract, Sealed") $dllImport = [System.Runtime.InteropServices.DllImportAttribute] $attribute = [System.Reflection.Emit.CustomAttributeBuilder]::new( $dllImport.GetConstructor([Type[]]@([string])), [object[]]@("kernel32.dll"), - [System.Reflection.FieldInfo[]]@( - $dllImport.GetField("SetLastError"), $dllImport.GetField("ExactSpelling"), - $dllImport.GetField("CharSet")), + [System.Reflection.FieldInfo[]]@($dllImport.GetField("SetLastError"), $dllImport.GetField("ExactSpelling"), $dllImport.GetField("CharSet")), [object[]]@($true, $true, [System.Runtime.InteropServices.CharSet]::Unicode)) $declarations = @( @{ Name = "CreateJobObjectW"; Return = [IntPtr]; Args = [Type[]]@([IntPtr], [string]) }, - @{ Name = "SetInformationJobObject"; Return = [bool]; Args = [Type[]]@([IntPtr], [int], [IntPtr], [uint32]) }, - @{ Name = "AssignProcessToJobObject"; Return = [bool]; Args = [Type[]]@([IntPtr], [IntPtr]) }, + @{ Name = "SetInformationJobObject"; Return = [bool]; Args = [Type[]]@([IntPtr], [int], $limitsType.MakeByRefType(), [uint32]) }, + @{ Name = "QueryInformationJobObject"; Return = [bool]; Args = [Type[]]@([IntPtr], [int], $accountingType.MakeByRefType(), [uint32], [IntPtr]) }, + @{ Name = "TerminateJobObject"; Return = [bool]; Args = [Type[]]@([IntPtr], [uint32]) }, + @{ Name = "CloseHandle"; Return = [bool]; Args = [Type[]]@([IntPtr]) }, @{ Name = "GetCurrentProcess"; Return = [IntPtr]; Args = [Type[]]@() }, - @{ Name = "CloseHandle"; Return = [bool]; Args = [Type[]]@([IntPtr]) } + @{ Name = "GetStdHandle"; Return = [IntPtr]; Args = [Type[]]@([int]) }, + @{ Name = "DuplicateHandle"; Return = [bool]; Args = [Type[]]@([IntPtr], [IntPtr], [IntPtr], [IntPtr].MakeByRefType(), [uint32], [bool], [uint32]) }, + @{ Name = "InitializeProcThreadAttributeList"; Return = [bool]; Args = [Type[]]@([IntPtr], [int], [uint32], [IntPtr].MakeByRefType()) }, + @{ Name = "UpdateProcThreadAttribute"; Return = [bool]; Args = [Type[]]@([IntPtr], [uint32], [IntPtr], [IntPtr], [IntPtr], [IntPtr], [IntPtr]) }, + @{ Name = "DeleteProcThreadAttributeList"; Return = [void]; Args = [Type[]]@([IntPtr]) }, + @{ Name = "CreateProcessW"; Return = [bool]; Args = [Type[]]@([string], [System.Text.StringBuilder], [IntPtr], [IntPtr], [bool], [uint32], [IntPtr], [string], $startupExType.MakeByRefType(), $processType.MakeByRefType()) }, + @{ Name = "WaitForSingleObject"; Return = [uint32]; Args = [Type[]]@([IntPtr], [uint32]) }, + @{ Name = "GetExitCodeProcess"; Return = [bool]; Args = [Type[]]@([IntPtr], [uint32].MakeByRefType()) } ) foreach ($declaration in $declarations) { $method = $builder.DefineMethod($declaration.Name, - [System.Reflection.MethodAttributes]"Public, Static, PinvokeImpl", - $declaration.Return, $declaration.Args) + [System.Reflection.MethodAttributes]"Public, Static, PinvokeImpl", $declaration.Return, $declaration.Args) $method.SetCustomAttribute($attribute) $method.SetImplementationFlags([System.Reflection.MethodImplAttributes]::PreserveSig) } $native = $builder.CreateType() - $bootstrapJob = $native::CreateJobObjectW([IntPtr]::Zero, $null) - if ($bootstrapJob -eq [IntPtr]::Zero) { - throw [System.ComponentModel.Win32Exception]::new( - [System.Runtime.InteropServices.Marshal]::GetLastWin32Error(), "CreateJobObjectW(bootstrap)") + # Framework serialization loads directly; neither cmdlet autoload nor CodeDOM + # is on the command startup path. Simple dictionary format matches Node JSON. + [void][System.Reflection.Assembly]::Load("System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") + $settings = [System.Runtime.Serialization.Json.DataContractJsonSerializerSettings]::new() + $settings.UseSimpleDictionaryFormat = $true + $serializer = [System.Runtime.Serialization.Json.DataContractJsonSerializer]::new([System.Collections.Generic.Dictionary[string,object]], $settings) + # Synchronize BEFORE PREPARED: the first valid request must never trigger the + # preparation self-kill callback. EOF during preparation still kills directly. + $bootstrapKill.Dispose() + $bootstrapKill = $null + if ($preparing.IsCancellationRequested) { throw "command owner disconnected during preparation" } + $writer.WriteLine("PREPARED") + $line = $requestRead.GetAwaiter().GetResult() + if ($null -eq $line -or $line -eq "STOP") { exit 1 } + $ownerLost = [System.Threading.CancellationTokenSource]::new() + $cancelOwner = [System.Delegate]::CreateDelegate([System.Action], $ownerLost, "Cancel") + $bootstrapKill = $ownerLost.Token.Register($killSelf) + $ownerRead = $reader.ReadLineAsync() + $ownerRead.ConfigureAwait($false).GetAwaiter().OnCompleted($cancelOwner) + Write-Phase "request-read" + Write-Phase "parser-started" + $inputStream = [System.IO.MemoryStream]::new($utf8.GetBytes($line)) + try { $request = $serializer.ReadObject($inputStream) } finally { $inputStream.Dispose() } + if ($request.Count -ne 6) { throw "invalid command request fields" } + foreach ($key in @("application", "commandLine", "cwd", "environment")) { + if ($request[$key] -isnot [string]) { throw "invalid command request string" } + } + foreach ($key in @("timeout", "cleanup")) { + if ($request[$key] -isnot [int] -or $request[$key] -lt 1) { throw "invalid command request timeout" } + } + Write-Phase "parser-completed" + Write-Phase "native-entered" + # Dispose synchronizes with a self-kill already in flight. After this handoff, + # cooperative cancellation and the Job's explicit cleanup own admission. + $bootstrapKill.Dispose() + $bootstrapKill = $null + if ($ownerLost.IsCancellationRequested) { throw "command owner disconnected before admission" } + $job = [IntPtr]::Zero + $attributes = [IntPtr]::Zero + $handles = [IntPtr]::Zero + $jobList = [IntPtr]::Zero + $environmentBlock = [IntPtr]::Zero + $inherited = [IntPtr[]]::new(3) + $child = [Activator]::CreateInstance($processType) + $initialized = $false + $extinct = $false + $commandFailure = $null + $setupReleased = $false + function Active-Processes { + $accounting = [Activator]::CreateInstance($accountingType) + Check-Native ($native::QueryInformationJobObject($job, 1, [ref]$accounting, 48, [IntPtr]::Zero)) "QueryInformationJobObject" + return $accounting.ActiveProcesses + } + function Terminate-AndObserve { + Check-Native ($native::TerminateJobObject($job, 1)) "TerminateJobObject" + $wait = [System.Diagnostics.Stopwatch]::StartNew() + while ((Active-Processes) -ne 0) { + if ($wait.ElapsedMilliseconds -ge $request["cleanup"]) { throw "Windows Job extinction was not observed" } + [System.Threading.Thread]::Sleep(10) + } } - $assigned = $false - $limits = [IntPtr]::Zero try { - # JOBOBJECT_EXTENDED_LIMIT_INFORMATION on 64-bit Windows: 144 bytes; - # BasicLimitInformation.LimitFlags is at offset 16. - $limits = [System.Runtime.InteropServices.Marshal]::AllocHGlobal(144) - [System.Runtime.InteropServices.Marshal]::Copy([byte[]]::new(144), 0, $limits, 144) - [System.Runtime.InteropServices.Marshal]::WriteInt32($limits, 16, 0x2000) - if (-not $native::SetInformationJobObject($bootstrapJob, 9, $limits, 144)) { - throw [System.ComponentModel.Win32Exception]::new( - [System.Runtime.InteropServices.Marshal]::GetLastWin32Error(), "SetInformationJobObject(bootstrap)") + $job = $native::CreateJobObjectW([IntPtr]::Zero, $null) + Check-Native ($job -ne [IntPtr]::Zero) "CreateJobObjectW" + $basic = [Activator]::CreateInstance($basicType) + $basic.Flags = 0x2000 # KILL_ON_JOB_CLOSE, with neither breakaway flag. + $limits = [Activator]::CreateInstance($limitsType) + $limits.Basic = $basic + Check-Native ($native::SetInformationJobObject($job, 9, [ref]$limits, 144)) "SetInformationJobObject" + $self = $native::GetCurrentProcess() + for ($i = 0; $i -lt 3; $i++) { + $handle = [IntPtr]::Zero + Check-Native ($native::DuplicateHandle($self, $native::GetStdHandle(-10 - $i), $self, [ref]$handle, 0, $true, 2)) "DuplicateHandle(stdio)" + $inherited[$i] = $handle } - if (-not $native::AssignProcessToJobObject($bootstrapJob, $native::GetCurrentProcess())) { - throw [System.ComponentModel.Win32Exception]::new( - [System.Runtime.InteropServices.Marshal]::GetLastWin32Error(), "AssignProcessToJobObject(bootstrap)") + $bytes = [IntPtr]::Zero + [void]$native::InitializeProcThreadAttributeList([IntPtr]::Zero, 2, 0, [ref]$bytes) + Check-Native ($bytes -ne [IntPtr]::Zero) "InitializeProcThreadAttributeList(size)" + $attributes = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($bytes) + Check-Native ($native::InitializeProcThreadAttributeList($attributes, 2, 0, [ref]$bytes)) "InitializeProcThreadAttributeList" + $initialized = $true + $handles = [System.Runtime.InteropServices.Marshal]::AllocHGlobal([IntPtr]::Size * 3) + for ($i = 0; $i -lt 3; $i++) { [System.Runtime.InteropServices.Marshal]::WriteIntPtr($handles, $i * [IntPtr]::Size, $inherited[$i]) } + $jobList = [System.Runtime.InteropServices.Marshal]::AllocHGlobal([IntPtr]::Size) + [System.Runtime.InteropServices.Marshal]::WriteIntPtr($jobList, $job) + Check-Native ($native::UpdateProcThreadAttribute($attributes, 0, [IntPtr]::new(0x20002), $handles, [IntPtr]::new(24), [IntPtr]::Zero, [IntPtr]::Zero)) "HANDLE_LIST" + Check-Native ($native::UpdateProcThreadAttribute($attributes, 0, [IntPtr]::new(0x2000d), $jobList, [IntPtr]::new(8), [IntPtr]::Zero, [IntPtr]::Zero)) "JOB_LIST" + $startupInfo = [Activator]::CreateInstance($startupType) + $startupInfo.Size = 112 + $startupInfo.Flags = 0x100 + $startupInfo.Stdin = $inherited[0] + $startupInfo.Stdout = $inherited[1] + $startupInfo.Stderr = $inherited[2] + $startup = [Activator]::CreateInstance($startupExType) + $startup.Startup = $startupInfo + $startup.Attributes = $attributes + $environmentBlock = [System.Runtime.InteropServices.Marshal]::StringToHGlobalUni($request["environment"]) + Write-Phase "create-process-pending" + if ($ownerLost.IsCancellationRequested) { throw "command owner disconnected before admission" } + # An exception inside the native call leaves admission UNKNOWN. A definite + # false return is the only post-attempt path eligible for NOT_ADMITTED. + $attempted = $true + $created = $native::CreateProcessW($request["application"], [System.Text.StringBuilder]::new($request["commandLine"]), + [IntPtr]::Zero, [IntPtr]::Zero, $true, 0x08080400, $environmentBlock, $request["cwd"], [ref]$startup, [ref]$child) + $creationError = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error() + $nativeReturned = $true + if (-not $created) { throw [System.ComponentModel.Win32Exception]::new($creationError, "CreateProcessW(JOB_LIST)") } + Write-Phase "create-process-completed" + $writer.WriteLine("READY " + $child.ProcessId) + # Both backing arrays live through CreateProcess and attribute deletion. + $native::DeleteProcThreadAttributeList($attributes) + $initialized = $false + for ($i = 0; $i -lt 3; $i++) { + Check-Native ($native::CloseHandle($inherited[$i])) "CloseHandle(child stdio)" + $inherited[$i] = [IntPtr]::Zero } - $assigned = $true + Check-Native ($native::CloseHandle($child.Thread)) "CloseHandle(child thread)" + $child.Thread = [IntPtr]::Zero + $clock = [System.Diagnostics.Stopwatch]::StartNew() + $timedOut = $false + while ($true) { + $wait = $native::WaitForSingleObject($child.Process, 0) + if ($wait -eq 0) { break } + Check-Native ($wait -eq 258) "WaitForSingleObject" + if ($ownerLost.IsCancellationRequested -or $clock.ElapsedMilliseconds -ge $request["timeout"]) { + $timedOut = $clock.ElapsedMilliseconds -ge $request["timeout"] + Terminate-AndObserve + $extinct = $true + break + } + [System.Threading.Thread]::Sleep(10) + } + $code = [uint32]0 + Check-Native ($native::GetExitCodeProcess($child.Process, [ref]$code)) "GetExitCodeProcess" + $writer.WriteLine("EXIT " + $code) + if (-not $extinct) { + if ((Active-Processes) -ne 0) { Terminate-AndObserve } + $extinct = $true + } + if ($timedOut) { $writer.WriteLine("TIMEOUT") } + } catch { + $commandFailure = $_ } finally { - if ($limits -ne [IntPtr]::Zero) { [System.Runtime.InteropServices.Marshal]::FreeHGlobal($limits) } - if (-not $assigned) { [void]$native::CloseHandle($bootstrapJob) } + # KILL_ON_JOB_CLOSE is a last fallback, never an extinction receipt. + try { if ($created -and -not $extinct) { Terminate-AndObserve } } + catch { if ($null -eq $commandFailure) { $commandFailure = $_ } } + finally { + $released = $true + if ($initialized) { $native::DeleteProcThreadAttributeList($attributes) } + foreach ($handle in @($inherited) + @($child.Thread, $child.Process, $job)) { + if ($handle -ne [IntPtr]::Zero -and -not $native::CloseHandle($handle)) { $released = $false } + } + foreach ($memory in @($attributes, $handles, $jobList, $environmentBlock)) { + if ($memory -ne [IntPtr]::Zero) { [System.Runtime.InteropServices.Marshal]::FreeHGlobal($memory) } + } + $setupReleased = $released + } } - # Keep the non-inheritable Job handle for this process's lifetime. Helper - # death closes it and kills compiler descendants; that is not a success receipt. - Write-Phase "bootstrap-contained" - Write-Phase "compile-started" - Add-Type -Path (Join-Path $PSScriptRoot "owned-command-windows.cs") - Write-Phase "compile-completed" - [CrabpotCommandJob]::Run( - $request.application, $request.commandLine, $request.cwd, $request.environment, - [int]$request.timeout, [int]$request.cleanup, $ownerLost, $bootstrapKill, $writer) + if ($null -ne $commandFailure) { throw $commandFailure } + if (-not $setupReleased) { throw "Windows command setup handles did not close" } + $writer.WriteLine("CLOSED") } catch { - if ($null -ne $writer) { + if ($null -ne $bootstrapKill) { $bootstrapKill.Dispose(); $bootstrapKill = $null } + if ($null -ne $writer -and $null -ne $serializer) { $failure = $_.Exception while ($null -ne $failure.InnerException) { $failure = $failure.InnerException } $message = $failure.Message if ($message.Length -gt 2048) { $message = $message.Substring(0, 2048) } - $detail = @{ message = $message } + $detail = [System.Collections.Generic.Dictionary[string,object]]::new() + $detail["message"] = $message if ($failure -is [System.ComponentModel.Win32Exception]) { - $detail.nativeCode = $failure.NativeErrorCode - $detail.operation = $failure.Message + $detail["nativeCode"] = $failure.NativeErrorCode + $detail["operation"] = $failure.Message } - $encoded = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(($detail | ConvertTo-Json -Compress))) - try { $writer.WriteLine("ERROR " + $encoded) } catch {} + $errorStream = [System.IO.MemoryStream]::new() + try { + $serializer.WriteObject($errorStream, $detail) + $writer.WriteLine("ERROR " + [Convert]::ToBase64String($errorStream.ToArray())) + if ($setupReleased -and (-not $attempted -or ($nativeReturned -and -not $created))) { + $writer.WriteLine("NOT_ADMITTED") + } + } catch {} finally { $errorStream.Dispose() } } exit 1 } finally { diff --git a/scripts/owned-command.mjs b/scripts/owned-command.mjs index 9d6791596..799b2446d 100644 --- a/scripts/owned-command.mjs +++ b/scripts/owned-command.mjs @@ -16,7 +16,7 @@ const defaultMaxBuffer = 1024 * 1024; const startupPhases = new Set([ "owner-started", "worker-entered", "helper-spawned", "pipe-connected", "request-write-attempted", "request-read", - "parser-started", "parser-completed", "bootstrap-contained", "compile-started", "compile-completed", + "preparation-started", "prepared-received", "parser-started", "parser-completed", "not-admitted-received", "native-entered", "create-process-pending", "create-process-completed", "ready-received", "closed-received", ]); @@ -414,7 +414,13 @@ function runWindows(command, args, options, result, observe, ready, fail, shared let helper; let stopped = false; let requestIssued = false; + let prepared = false; let admitted = false; + let notAdmitted = false; + let exitReceived = false; + let timeoutReceived = false; + let reportedError = false; + let protocolFailed = false; let closedJob = false; let helperClosed = false; let helperExitError; @@ -484,16 +490,55 @@ function runWindows(command, args, options, result, observe, ready, fail, shared finish(); return; } - if (closedJob) { - delete result.cleanupError; + // Only our missing-receipt error can be discharged by a valid terminal + // receipt, after both helper closure and control-pipe drain. + const terminal = !protocolFailed && (closedJob || notAdmitted); + if (terminal) { + if (result.cleanupError === missingJobCleanupError) delete result.cleanupError; } else { - result.cleanupError = { code: "EOWNERCLEANUP", message: "Windows Job extinction receipt was not observed" }; + result.cleanupError ??= { code: "EOWNERCLEANUP", message: "Windows Job extinction receipt was not observed" }; } - if (!closedJob || result.status === null) { + if (!terminal || (closedJob && result.status === null)) { recordFailure({ code: "EOWNERNATIVE", message: "Windows adapter exited without Job extinction receipt" }); } finish(); }; + const sendRequest = () => { + try { + const executable = resolveWindowsCommand(command, options.cwd, options.env); + const batch = /\.(cmd|bat)$/i.test(executable); + const application = batch + ? resolveWindowsCommand(environmentValue(options.env, "COMSPEC") ?? "cmd.exe", options.cwd, options.env) + : executable; + const commandLine = batch + ? `${quoteWindowsArg(application)} /d /s /c "${batchCommandLine(executable, args)}"` + : [executable, ...args].map(quoteWindowsArg).join(" "); + const request = `${JSON.stringify({ + application, commandLine, cwd: options.cwd, timeout: options.timeout, cleanup: cleanupMs, + environment: Object.entries(options.env).filter(([, value]) => value !== undefined) + .sort(([a], [b]) => a.toUpperCase().localeCompare(b.toUpperCase())) + .map(([key, value]) => `${key}=${value}`).join("\0") + "\0\0", + })}\n`; + if (stopped || Atomics.load(shared, 1)) { + stop(); + socket.destroy(); + return; + } + // A throwing or partial write may still have issued a request. + requestIssued = true; + phase("request-write-attempted"); + socket.write(request); + if (stopped || Atomics.load(shared, 1)) socket.write("STOP\n"); + } catch (error) { fail(error); socket.destroy(); } + }; + const protocolFailure = (message) => { + protocolFailed = true; + if (requestIssued) result.cleanupError ??= { + code: "EOWNERCLEANUP", message: "Windows adapter terminal receipt is invalid", + cause: { code: "EOWNERPROTOCOL", message }, + }; + fail(Object.assign(new Error(message), { code: "EOWNERPROTOCOL" })); + }; server.on("error", (error) => { fail(error); finish(); }); server.once("connection", (connection) => { phase("pipe-connected"); @@ -502,39 +547,62 @@ function runWindows(command, args, options, result, observe, ready, fail, shared let pending = ""; socket.setEncoding("utf8"); socket.on("error", fail); - socket.on("close", () => { controlEnded = true; maybeFinish(); }); + socket.on("close", () => { + if (pending.length) protocolFailure("incomplete Windows adapter control message"); + controlEnded = true; + maybeFinish(); + }); socket.on("data", (data) => { pending += data; if (pending.length > 8192) { - fail(Object.assign(new Error("Windows adapter control message exceeded limit"), { code: "EOWNERPROTOCOL" })); + protocolFailure("Windows adapter control message exceeded limit"); + socket.destroy(); return; } let end; while ((end = pending.indexOf("\n")) !== -1) { - const line = pending.slice(0, end).trimEnd(); + const line = pending.slice(0, end).replace(/\r$/, ""); pending = pending.slice(end + 1); + if (protocolFailed) continue; const observation = /^PHASE ([a-z-]+) (\d+)$/.exec(line); - if (line.startsWith("PHASE ")) { + if (closedJob || notAdmitted) { + protocolFailure("Windows adapter sent a message after its terminal receipt"); + } else if (line.startsWith("PHASE ")) { if (observation && startupPhases.has(observation[1]) && Number.isSafeInteger(Number(observation[2]))) { phase(observation[1], Number(observation[2])); - } - } else if (/^READY \d+$/.test(line) && !admitted) { + } else protocolFailure("invalid Windows adapter phase observation"); + } else if (line === "PREPARED" && !prepared && !requestIssued && !reportedError) { + prepared = true; + phase("prepared-received"); + sendRequest(); + } else if (/^READY [1-9]\d*$/.test(line) && requestIssued && !admitted && !reportedError && + Number.isSafeInteger(Number(line.slice(6))) && Number(line.slice(6)) <= 2_147_483_647) { phase("ready-received"); admitted = true; clearTimeout(startupTimer); ready(Number(line.slice(6))); - } else if (/^EXIT \d+$/.test(line) && admitted) { + } else if (/^EXIT \d+$/.test(line) && admitted && !exitReceived && Number(line.slice(5)) <= 0xffffffff) { + exitReceived = true; result.status = Number(line.slice(5)); - } else if (line === "CLOSED" && admitted) { + } else if (line === "CLOSED" && admitted && exitReceived && !reportedError) { phase("closed-received"); closedJob = true; - } else if (line === "TIMEOUT" && admitted) { + } else if (line === "NOT_ADMITTED" && requestIssued && !admitted && reportedError) { + notAdmitted = true; + phase("not-admitted-received"); + } else if (line === "TIMEOUT" && admitted && exitReceived && !timeoutReceived && !reportedError) { + timeoutReceived = true; recordFailure({ code: "ETIMEDOUT", message: `command timed out after ${options.timeout}ms` }); - } else if (line.startsWith("ERROR ")) { + } else if (line.startsWith("ERROR ") && !reportedError) { + reportedError = true; try { const detail = JSON.parse(Buffer.from(line.slice(6), "base64").toString("utf8")); - if (typeof detail.message !== "string") throw new Error("missing native error message"); + if (typeof detail.message !== "string" || detail.message.length > 2048 || + (detail.operation !== undefined && typeof detail.operation !== "string") || + (detail.nativeCode !== undefined && !Number.isInteger(detail.nativeCode))) { + throw new Error("invalid native error fields"); + } const spawnFailure = detail.operation === "CreateProcessW(JOB_LIST)"; const code = spawnFailure ? ({ 2: "ENOENT", 3: "ENOENT", 5: "EACCES", 193: "EFTYPE", 216: "UNKNOWN" }[detail.nativeCode] ?? "EOWNERNATIVE") @@ -544,10 +612,10 @@ function runWindows(command, args, options, result, observe, ready, fail, shared ...(spawnFailure ? { syscall: `spawn ${command}`, path: command, spawnargs: args } : {}), })); } catch (error) { - fail(Object.assign(new Error(`invalid native error receipt: ${error.message}`), { code: "EOWNERPROTOCOL" })); + protocolFailure(`invalid native error receipt: ${error.message}`); } } else { - fail(Object.assign(new Error("invalid Windows adapter control message"), { code: "EOWNERPROTOCOL" })); + protocolFailure("invalid Windows adapter control message"); } } }); @@ -556,32 +624,6 @@ function runWindows(command, args, options, result, observe, ready, fail, shared socket.destroy(); return; } - try { - const executable = resolveWindowsCommand(command, options.cwd, options.env); - const batch = /\.(cmd|bat)$/i.test(executable); - const application = batch - ? resolveWindowsCommand(environmentValue(options.env, "COMSPEC") ?? "cmd.exe", options.cwd, options.env) - : executable; - const commandLine = batch - ? `${quoteWindowsArg(application)} /d /s /c "${batchCommandLine(executable, args)}"` - : [executable, ...args].map(quoteWindowsArg).join(" "); - const request = `${JSON.stringify({ - application, commandLine, cwd: options.cwd, timeout: options.timeout, cleanup: cleanupMs, - environment: Object.entries(options.env).filter(([, value]) => value !== undefined) - .sort(([a], [b]) => a.toUpperCase().localeCompare(b.toUpperCase())) - .map(([key, value]) => `${key}=${value}`).join("\0") + "\0\0", - })}\n`; - if (stopped || Atomics.load(shared, 1)) { - stop(); - socket.destroy(); - return; - } - // A throwing or partial write may still have issued a request. - requestIssued = true; - phase("request-write-attempted"); - socket.write(request); - if (stopped || Atomics.load(shared, 1)) socket.write("STOP\n"); - } catch (error) { fail(error); socket.destroy(); } }); server.listen(`\\\\.\\pipe\\${pipeName}`, () => { if (stopped) { finish(); return; } diff --git a/test/fixtures/owned-command-compiler-observer.ps1 b/test/fixtures/owned-command-compiler-observer.ps1 index 6b81b6455..fd5354639 100644 --- a/test/fixtures/owned-command-compiler-observer.ps1 +++ b/test/fixtures/owned-command-compiler-observer.ps1 @@ -66,12 +66,13 @@ public static class CompilerObserver public static void Run(string root) { Process compiler = null; + Process target = null; List threads = new List(); Stopwatch clock = Stopwatch.StartNew(); try { Publish(root, "observer-ready", "{}"); - string helperFile = Path.Combine(root, "helper-pid"); + string helperFile = Path.Combine(root, "target-pid"); string rescueFile = Path.Combine(root, "observer-rescue"); int helper = 0; while (helper == 0 && clock.ElapsedMilliseconds < 15000 && !File.Exists(rescueFile)) @@ -79,7 +80,11 @@ public static class CompilerObserver if (File.Exists(helperFile)) Int32.TryParse(File.ReadAllText(helperFile), out helper); if (helper == 0) Thread.Sleep(5); } - if (helper == 0) throw new InvalidOperationException("helper was not observed"); + if (helper == 0) throw new InvalidOperationException("target was not observed"); + target = Process.GetProcessById(helper); + IntPtr targetHeld = target.Handle; + if (target.ProcessName != "powershell" || target.HasExited) + throw new InvalidOperationException("compilation target identity changed"); int pid = 0; while (pid == 0 && clock.ElapsedMilliseconds < 20000 && !File.Exists(rescueFile)) { @@ -102,7 +107,7 @@ public static class CompilerObserver if (threads.Count == 0 || compiler.HasExited) throw new InvalidOperationException("compiler was not alive and suspended"); Publish(root, "compiler-suspended.json", - "{\"helperPid\":" + helper + ",\"pid\":" + pid + ",\"name\":\"csc.exe\",\"suspended\":true}"); + "{\"targetPid\":" + helper + ",\"pid\":" + pid + ",\"name\":\"csc.exe\",\"suspended\":true}"); while (!compiler.WaitForExit(20) && clock.ElapsedMilliseconds < 25000 && !File.Exists(rescueFile)) { } bool rescued = !compiler.HasExited; @@ -118,6 +123,11 @@ public static class CompilerObserver if (!compiler.WaitForExit(3000)) { compiler.Kill(); compiler.WaitForExit(3000); } compiler.Dispose(); } + if (target != null) + { + if (!target.WaitForExit(1000)) { target.Kill(); target.WaitForExit(3000); } + target.Dispose(); + } } } } diff --git a/test/owned-command.test.mjs b/test/owned-command.test.mjs index 7d4ba99e2..885a6d7c0 100644 --- a/test/owned-command.test.mjs +++ b/test/owned-command.test.mjs @@ -12,13 +12,13 @@ import { runOwnedCommand } from "../scripts/owned-command.mjs"; test("owned command preserves synchronous results, cwd, environment, and argument bytes", async (t) => { const root = await temporaryRoot(t); - const args = ["space here", 'embedded"quote', "trailing\\", "\u03a9", "%PATH%", "&"]; + const args = ["space here", 'embedded"quote', "trailing\\", "\u03a9", "汉字🦊", "", "line\nbreak", "\t", "%PATH%", "&"]; const result = runOwnedCommand(process.execPath, ["-e", ` process.stdout.write(JSON.stringify({ cwd: process.cwd(), env: process.env.OWNER_TEST, args: process.argv.slice(1) })); process.stderr.write("retained stderr"); process.exitCode = 7; `, ...args], { - cwd: root, env: { ...process.env, OWNER_TEST: "retained value" }, timeout: 5000, encoding: "utf8", + cwd: root, env: { ...process.env, OWNER_TEST: "retained value 汉字🦊\nsecond line" }, timeout: 5000, encoding: "utf8", }); assert.equal(typeof result.then, "undefined"); assert.ifError(result.error); @@ -27,7 +27,7 @@ test("owned command preserves synchronous results, cwd, environment, and argumen assert.equal(result.signal, null); const observed = JSON.parse(result.stdout); observed.cwd = await realpath(observed.cwd); - assert.deepEqual(observed, { cwd: await realpath(root), env: "retained value", args }); + assert.deepEqual(observed, { cwd: await realpath(root), env: "retained value 汉字🦊\nsecond line", args }); assert.equal(result.stderr, "retained stderr"); }); @@ -141,9 +141,9 @@ test("parent drains bounded phase observations without delaying queued results o } for (const observation of [ { phase: "arbitrary-private-value", atMs }, - { phase: "compile-started", atMs: Infinity }, - { phase: "compile-started", atMs: -1 }, - { phase: "compile-started", atMs: 0.5 }, + { phase: "preparation-started", atMs: Infinity }, + { phase: "preparation-started", atMs: -1 }, + { phase: "preparation-started", atMs: 0.5 }, ]) workerData.port.postMessage({ type: "phase", ...observation }); workerData.port.postMessage({ type: "result", result: { pid: 0, status: null, signal: null, stdout: Buffer.alloc(0), stderr: Buffer.alloc(0), @@ -341,7 +341,7 @@ test("Windows controller inherits its parent environment while the native comman const entry = '$ErrorActionPreference = "Stop"'; assert.equal(original.split(entry).length, 2); // Observe the actual controller before any cmdlet/module loading. Record only - // synthetic comparisons, then retain the real parser, compiler and Job owner. + // synthetic comparisons, then retain the real serializer and Job owner. await writeFile(path.join(root, "scripts/owned-command-windows.ps1"), original.replace(entry, `${entry} $parentOnly = [Environment]::GetEnvironmentVariable("CRABPOT_OWNER_PARENT_ONLY") -ceq "parent-only" $parentConflict = [Environment]::GetEnvironmentVariable("CRABPOT_OWNER_CONFLICT") -ceq "parent" @@ -352,8 +352,6 @@ test("Windows controller inherits its parent environment while the native comman [System.IO.File]::WriteAllText('${helperReceipt.replaceAll("'", "''")}', $observed) if (-not ($parentOnly -and $parentConflict -and $targetAbsent)) { throw "controller environment boundary failed" } `)); - await copyFile(new URL("../scripts/owned-command-windows.cs", import.meta.url), - path.join(root, "scripts/owned-command-windows.cs")); const systemRoot = Object.entries(process.env).find(([key]) => key.toUpperCase() === "SYSTEMROOT")?.[1]; assert.ok(systemRoot); // Workers preserve key casing. Exercise parent lookup with a non-default case @@ -409,6 +407,44 @@ test("Windows adapter setup failure never runs an uncontained command", { await assert.rejects(readFile(marker), { code: "ENOENT" }); }); + +test("Windows emitted Job structs match the native x64 ABI", { + skip: process.platform !== "win32", timeout: 20_000, +}, async (t) => { + const root = await temporaryRoot(t); + const ownerPath = await copyOwner(root); + const abiFile = path.join(root, "native-abi.json"); + const source = await readFile(new URL("../scripts/owned-command-windows.ps1", import.meta.url), "utf8"); + const prepared = ' $writer.WriteLine("PREPARED")'; + assert.equal(source.split(prepared).length, 2); + const probe = [ + '$abi = [System.Collections.Generic.Dictionary[string,object]]::new()', + '$abi["startupEx"] = [System.Runtime.InteropServices.Marshal]::SizeOf([type]$startupExType)', + '$abi["limits"] = [System.Runtime.InteropServices.Marshal]::SizeOf([type]$limitsType)', + '$abi["accounting"] = [System.Runtime.InteropServices.Marshal]::SizeOf([type]$accountingType)', + '$abi["process"] = [System.Runtime.InteropServices.Marshal]::SizeOf([type]$processType)', + '$abi["limitFlags"] = [System.Runtime.InteropServices.Marshal]::OffsetOf($basicType, "Flags").ToInt32()', + '$abi["attributes"] = [System.Runtime.InteropServices.Marshal]::OffsetOf($startupExType, "Attributes").ToInt32()', + '$abi["stdin"] = [System.Runtime.InteropServices.Marshal]::OffsetOf($startupType, "Stdin").ToInt32()', + '$abi["activeProcesses"] = [System.Runtime.InteropServices.Marshal]::OffsetOf($accountingType, "ActiveProcesses").ToInt32()', + '$abiStream = [System.IO.MemoryStream]::new()', + '$serializer.WriteObject($abiStream, $abi)', + '[System.IO.File]::WriteAllBytes(' + "'" + abiFile.replaceAll("'", "''") + "'" + ', $abiStream.ToArray())', + '$abiStream.Dispose()', + ].map((line) => " " + line).join("\n"); + await writeFile(path.join(root, "scripts/owned-command-windows.ps1"), source.replace(prepared, probe + "\n" + prepared)); + const { runOwnedCommand: invoke } = await import(pathToFileURL(ownerPath).href); + const result = invoke(process.execPath, ["-e", 'process.stdout.write("native ABI checked")'], { timeout: 1000, encoding: "utf8" }); + assert.ifError(result.error); + assert.ifError(result.cleanupError); + assert.equal(result.status, 0); + assert.equal(result.stdout, "native ABI checked"); + assert.deepEqual(JSON.parse(await readFile(abiFile, "utf8")), { + startupEx: 112, limits: 144, accounting: 48, process: 24, + limitFlags: 16, attributes: 104, stdin: 80, activeProcesses: 40, + }); +}); + test("Windows command preserves the native process creation error", { skip: process.platform !== "win32", timeout: 20_000, }, async (t) => { @@ -432,11 +468,13 @@ test("Windows command preserves the native process creation error", { assert.equal(phases.includes("create-process-completed"), false); assert.equal(phases.includes("ready-received"), false); assert.equal(phases.includes("closed-received"), false); + assert.ok(phases.includes("not-admitted-received")); + assert.equal(result.cleanupError, undefined, "a definite failed spawn with released setup is safe to retry"); t.diagnostic(`native error ${result.error.nativeCode} retains Node ${baseline.error.code}`); }); for (const [phase, ownerLost, historicalOrder] of [ - ["compilation", false, false], ["compilation", true, false], + ["preparation", false, false], ["preparation", true, false], ["parsing", false, false], ["parsing", true, false], ["parsing", true, true], ]) { test(`Windows blocked ${phase} ${historicalOrder ? "negative control survives owner loss" : ownerLost ? "observes owner loss" : "has a deadline"} before native admission`, { @@ -464,7 +502,8 @@ for (const [phase, ownerLost, historicalOrder] of [ const commandMarker = path.join(root, "command-started"); const original = (await readFile(new URL("../scripts/owned-command-windows.ps1", import.meta.url), "utf8")) .replaceAll("\r\n", "\n"); - const parsing = " $request = $line | ConvertFrom-Json\n"; + const parsing = " $inputStream = [System.IO.MemoryStream]::new($utf8.GetBytes($line))\n" + + " try { $request = $serializer.ReadObject($inputStream) } finally { $inputStream.Dispose() }\n"; const watcher = " $ownerLost = [System.Threading.CancellationTokenSource]::new()\n"; assert.equal(original.split(parsing).length, 2); assert.equal(original.split(watcher).length, 2); @@ -472,12 +511,13 @@ for (const [phase, ownerLost, historicalOrder] of [ ? original.replace(parsing, "").replace(watcher, parsing + watcher) : original; const gate = phase === "parsing" ? parsing.trim() - : 'Add-Type -Path (Join-Path $PSScriptRoot "owned-command-windows.cs")'; + : 'Write-Phase "preparation-started"'; assert.equal(source.split(gate).length, 2); const ps = (value) => `'${value.replaceAll("'", "''")}'`; // Block the actual helper without loading a cmdlet. Neither cooperative // rescue nor the independent fixture expiry may supply the asserted closure. const blocked = source.replace(gate, ` + ${phase === "preparation" ? gate : ""} $expiresAt = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() + 20000 $fixtureClock = [System.Diagnostics.Stopwatch]::StartNew() $observed = '{"pid":' + $PID + ',"expiresAtMs":' + $expiresAt + '}' @@ -518,10 +558,11 @@ for (const [phase, ownerLost, historicalOrder] of [ const result = await readReceipt(resultFile, 12_500, () => errors.map(String).join("\n")); assert.equal(result.error?.code, "EOWNERSTART"); const phases = tracePhases(result); - assert.ok(phases.includes(phase === "parsing" ? "parser-started" : "compile-started")); - assert.equal(phases.includes(phase === "parsing" ? "parser-completed" : "compile-completed"), false); + assert.ok(phases.includes(phase === "parsing" ? "parser-started" : "preparation-started")); + assert.equal(phases.includes(phase === "parsing" ? "parser-completed" : "prepared-received"), false); assert.equal(phases.includes("create-process-pending"), false); - assert.equal(result.cleanupError?.code, "EOWNERCLEANUP", "phase observations are not extinction receipts"); + assert.equal(result.cleanupError?.code, phase === "parsing" ? "EOWNERCLEANUP" : undefined, + "only preparation before any issued request has known nonadmission"); } const closed = await waitForExit(helper.pid, ownerLost ? 2000 : 100); assert.ok(Date.now() < helper.expiresAtMs, "closure assertion must precede independent fixture expiry"); @@ -565,23 +606,21 @@ for (const phase of ["preconnection", "late connection"]) { $fixtureRegistration = $fixtureExpiry.Token.Register($fixtureKill) $expiresAt = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() + 20000 $fixtureExpiry.CancelAfter(20000) - $live = @{ pid = $PID; phase = "preconnection"; expiresAtMs = $expiresAt } | ConvertTo-Json -Compress + $live = '{"pid":' + $PID + ',"phase":"preconnection","expiresAtMs":' + $expiresAt + '}' [System.IO.File]::WriteAllText(${ps(helperReady + ".pending")}, $live) [System.IO.File]::Move(${ps(helperReady + ".pending")}, ${ps(helperReady)}) while (-not [System.IO.File]::Exists(${ps(releaseFile)})) { if ([System.IO.File]::Exists(${ps(rescueFile)})) { - $escape = @{ rescued = $true } | ConvertTo-Json -Compress + $escape = '{"rescued":true}' [System.IO.File]::WriteAllText(${ps(escapeFile + ".pending")}, $escape) [System.IO.File]::Move(${ps(escapeFile + ".pending")}, ${ps(escapeFile)}) exit 99 } - Start-Sleep -Milliseconds 20 + [System.Threading.Thread]::Sleep(20) } ${connection} `); await writeFile(path.join(root, "scripts/owned-command-windows.ps1"), blocked); - await copyFile(new URL("../scripts/owned-command-windows.cs", import.meta.url), - path.join(root, "scripts/owned-command-windows.cs")); if (phase === "late connection") { const owner = await readFile(ownerPath, "utf8"); const accept = ' server.once("connection", (connection) => {'; @@ -656,31 +695,29 @@ for (const phase of ["preconnection", "late connection"]) { } for (const [ownerLost, guarded] of [[false, true], [true, true], [false, false], [true, false]]) { - test(`Windows actual compiler ${guarded ? "exits" : "negative control survives"} on ${ownerLost ? "owner loss" : "startup expiry"}`, { + test(`Windows actual compiler ${guarded ? "exits" : "negative control survives"} on ${ownerLost ? "owner loss" : "command timeout"}`, { skip: process.platform !== "win32", timeout: 45_000, }, async (t) => { const root = await mkdtemp(path.join(os.tmpdir(), "crabpot compiler owner ")); const ownerPath = await copyOwner(root); - const pidFile = path.join(root, "helper-pid"); + const pidFile = path.join(root, "target-pid"); const commandMarker = path.join(root, "command-started"); const resultFile = path.join(root, "result.json"); const original = await readFile(new URL("../scripts/owned-command-windows.ps1", import.meta.url), "utf8"); - const compilation = 'Add-Type -Path (Join-Path $PSScriptRoot "owned-command-windows.cs")'; - assert.equal(original.split(compilation).length, 2); - const guardStart = original.indexOf(" # CodeDOM starts csc.exe."); - const guardEnd = original.indexOf(` ${compilation}`); - assert.ok(guardStart > 0 && guardEnd > guardStart); - const helperSource = guarded ? original : original.slice(0, guardStart) + original.slice(guardEnd); - await writeFile(path.join(root, "scripts/owned-command-windows.ps1"), helperSource.replace(compilation, ` - [System.IO.File]::WriteAllText('${pidFile.replaceAll("'", "''")}', [string]$PID) - ${compilation} - `)); - const native = await readFile(new URL("../scripts/owned-command-windows.cs", import.meta.url), "utf8"); - // Keep a real CodeDOM compilation active long enough for native observation, - // without replacing the compiler or introducing a production test seam. - const source = native + Array.from({ length: 10_000 }, (_, index) => - `\ninternal class CompilerObservation${index} { }`).join(""); - await writeFile(path.join(root, "scripts/owned-command-windows.cs"), source); + const admission = ' Check-Native ($native::UpdateProcThreadAttribute($attributes, 0, [IntPtr]::new(0x2000d), $jobList, [IntPtr]::new(8), [IntPtr]::Zero, [IntPtr]::Zero)) "JOB_LIST"'; + assert.equal(original.split(admission).length, 2); + // The negative control removes only atomic Job admission. The real target, + // real CodeDOM compiler, owner-loss watcher and retained-handle rescue remain. + await writeFile(path.join(root, "scripts/owned-command-windows.ps1"), guarded ? original : original.replace(admission, "")); + const sourceFile = path.join(root, "target-compilation.cs"); + const targetScript = path.join(root, "compile-target.ps1"); + const ps = (value) => "'" + value.replaceAll("'", "''") + "'"; + await writeFile(sourceFile, Array.from({ length: 10_000 }, (_, index) => + "internal class CompilerObservation" + index + " { }").join("\n")); + await writeFile(targetScript, + "[System.IO.File]::WriteAllText(" + ps(pidFile) + ", [string]$PID)\n" + + "Add-Type -Path " + ps(sourceFile) + "\n" + + "[System.IO.File]::WriteAllText(" + ps(commandMarker) + ", 'compiled')\n"); const powershell = path.join(process.env.SystemRoot, "System32/WindowsPowerShell/v1.0/powershell.exe"); const observer = new Worker(` const { workerData, parentPort } = require("node:worker_threads"); @@ -731,34 +768,39 @@ for (const [ownerLost, guarded] of [[false, true], [true, true], [false, false], worker = new Worker(` const { workerData } = require("node:worker_threads"); import(workerData.owner).then(({ runOwnedCommand }) => { - const result = runOwnedCommand(process.execPath, ["-e", workerData.command], { timeout: 1000 }); + // Compilation previously ran inside the unchanged 10s startup window. + // Give the controlled target that same window; cleanup assertions stay bounded. + const result = runOwnedCommand(workerData.command, workerData.args, { timeout: 10000 }); const fs = require("node:fs"); fs.writeFileSync(workerData.result + ".pending", JSON.stringify(result)); fs.renameSync(workerData.result + ".pending", workerData.result); }); `, { eval: true, workerData: { owner: pathToFileURL(ownerPath).href, result: resultFile, - command: `require("node:fs").writeFileSync(${JSON.stringify(commandMarker)}, "started")`, + command: powershell, args: ["-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", targetScript], } }); worker.on("error", (error) => errors.push(error)); compiler = await readReceipt(path.join(root, "compiler-suspended.json"), 8000, observerError); assert.equal(compiler.name, "csc.exe"); assert.equal(compiler.suspended, true); - assert.ok(compiler.pid > 0 && compiler.helperPid > 0); + assert.ok(compiler.pid > 0 && compiler.targetPid > 0); assert.equal(process.kill(compiler.pid, 0), true, "actual compiler must be alive at interruption"); if (ownerLost) { await worker.terminate(); } else { const result = await readReceipt(resultFile, 14_000, observerError); - assert.equal(result.error?.code, "EOWNERSTART"); - assert.equal(result.status, null); + assert.equal(result.error?.code, "ETIMEDOUT"); const phases = tracePhases(result); - if (guarded) assert.ok(phases.includes("compile-started")); - assert.equal(phases.includes("compile-completed"), false); - assert.equal(phases.includes("create-process-pending"), false); - assert.equal(result.cleanupError?.code, "EOWNERCLEANUP"); + assert.ok(phases.includes("prepared-received")); + assert.ok(phases.includes("create-process-completed")); + assert.ok(phases.includes("ready-received")); + if (guarded) { + assert.ok(phases.includes("closed-received")); + assert.equal(result.cleanupError, undefined); + } } - assert.equal(await waitForExit(compiler.helperPid, 1000), true); + if (guarded) assert.equal(await waitForExit(compiler.targetPid, 1000), true); + else assert.equal(process.kill(compiler.targetPid, 0), true, "uncontained target survives with its suspended compiler"); const extinct = await waitForExit(compiler.pid, 2500); const requireExtinction = () => assert.equal(extinct, true, "actual compiler must exit before fixture rescue"); if (guarded) { @@ -769,7 +811,7 @@ for (const [ownerLost, guarded] of [[false, true], [true, true], [false, false], assert.throws(requireExtinction, { code: "ERR_ASSERTION", message: /actual compiler must exit before fixture rescue/, }); - t.diagnostic("negative control: helper exited; real csc.exe survived; extinction assertion failed before rescue"); + t.diagnostic("negative control: uncontained target and real csc.exe survived; extinction assertion failed before rescue"); } assert.equal(sentinel.exitCode, null); assert.equal(sentinel.signalCode, null); @@ -779,6 +821,89 @@ for (const [ownerLost, guarded] of [[false, true], [true, true], [false, false], }); } + +for (const cancellation of ["stop", "shared stop", "Worker loss"]) { + test("Windows PREPARED " + cancellation + " never issues the first request", { + skip: process.platform !== "win32", timeout: 30_000, + }, async (t) => { + const root = await mkdtemp(path.join(os.tmpdir(), "crabpot prepared owner ")); + const ownerPath = await copyOwner(root); + const helperPidFile = path.join(root, "helper-pid"); + const requestMarker = path.join(root, "request-issued"); + const commandMarker = path.join(root, "command-started"); + const ps = (value) => "'" + value.replaceAll("'", "''") + "'"; + const helper = await readFile(new URL("../scripts/owned-command-windows.ps1", import.meta.url), "utf8"); + const prepared = ' $writer.WriteLine("PREPARED")'; + assert.equal(helper.split(prepared).length, 2); + await writeFile(path.join(root, "scripts/owned-command-windows.ps1"), helper.replace(prepared, + " [System.IO.File]::WriteAllText(" + ps(helperPidFile) + ", [string]$PID)\n" + prepared)); + const source = await readFile(ownerPath, "utf8"); + const receipt = ' phase("prepared-received");'; + const send = ' socket.write(request);'; + assert.equal(source.split(receipt).length, 2); + assert.equal(source.split(send).length, 2); + const cancel = cancellation === "stop" ? "stop();" : cancellation === "shared stop" + ? "Atomics.store(shared, 1, 1);" : "process.exit(71);"; + await writeFile(ownerPath, 'import { writeFileSync } from "node:fs";\n' + + source.replace(receipt, receipt + "\n " + cancel).replace(send, + " writeFileSync(" + JSON.stringify(requestMarker) + ', "issued");\n' + send)); + const { runOwnedCommand: invoke } = await import(pathToFileURL(ownerPath).href); + let helperPid; + t.after(async () => { + if (helperPid) assert.equal(await waitForExit(helperPid, 2000), true, "EOF/STOP must close the exact helper"); + await rm(root, { recursive: true, force: true }); + }); + const result = invoke(process.execPath, ["-e", + 'require("node:fs").writeFileSync(' + JSON.stringify(commandMarker) + ', "started")'], { timeout: 1000 }); + helperPid = Number(await readFile(helperPidFile, "utf8")); + assert.ok(helperPid > 0); + assert.ok(result.error); + assert.ok(tracePhases(result).includes("prepared-received")); + assert.equal(tracePhases(result).includes("request-write-attempted"), false); + assert.equal(result.cleanupError?.code, cancellation === "Worker loss" ? "EOWNERCLEANUP" : undefined); + assert.equal(await waitForExit(helperPid, 100), true, "helper must close without fixture rescue"); + await assert.rejects(readFile(requestMarker), { code: "ENOENT" }); + await assert.rejects(readFile(commandMarker), { code: "ENOENT" }); + }); +} + +for (const corruption of ["duplicate prepared", "early closed", "early not admitted", "duplicate not admitted", "trailing partial"]) { + test("Windows rejects " + corruption + " instead of accepting a cleanup receipt", { + skip: process.platform !== "win32", timeout: 20_000, + }, async (t) => { + const root = await temporaryRoot(t); + const ownerPath = await copyOwner(root); + let helper = await readFile(new URL("../scripts/owned-command-windows.ps1", import.meta.url), "utf8"); + const prepared = ' $writer.WriteLine("PREPARED")'; + const parser = ' try { $request = $serializer.ReadObject($inputStream) } finally { $inputStream.Dispose() }'; + const terminal = ' $writer.WriteLine("NOT_ADMITTED")'; + for (const seam of [prepared, parser, terminal]) assert.equal(helper.split(seam).length, 2); + if (corruption === "duplicate prepared") helper = helper.replace(prepared, + prepared + '\n $writer.WriteLine("PREPARED")\n exit 1'); + else if (corruption === "early closed") helper = helper.replace(prepared, ' $writer.WriteLine("CLOSED")\n exit 1'); + else if (corruption === "early not admitted") helper = helper.replace(parser, ' $writer.WriteLine("NOT_ADMITTED")\n exit 1'); + else helper = helper.replace(terminal, terminal + (corruption === "duplicate not admitted" + ? '\n $writer.WriteLine("NOT_ADMITTED")' : '\n $writer.Write("partial")')); + await writeFile(path.join(root, "scripts/owned-command-windows.ps1"), helper); + const command = path.join(root, "invalid executable.exe"); + await writeFile(command, "not a Windows executable\n"); + const { runOwnedCommand: invoke } = await import(pathToFileURL(ownerPath).href); + const result = invoke(command, [], { timeout: 1000 }); + assert.ok(result.error); + assert.equal(result.status, null); + if (corruption === "early closed") { + assert.equal(result.error.code, "EOWNERPROTOCOL"); + assert.equal(result.cleanupError, undefined, "no request plus helper closure remains known nonadmission"); + } else { + assert.equal(result.cleanupError?.code, "EOWNERCLEANUP"); + assert.equal(result.cleanupError.cause?.code, "EOWNERPROTOCOL"); + if (["duplicate not admitted", "trailing partial"].includes(corruption)) { + assert.ok([193, 216].includes(result.error.nativeCode), "keep the original native spawn error"); + } else assert.equal(result.error.code, "EOWNERPROTOCOL"); + } + }); +} + test("Windows Job outlives its Worker only long enough to clean the admitted command", { skip: process.platform !== "win32", timeout: 25_000, }, async (t) => { diff --git a/test/plugin-inspector-checkout-lock.test.mjs b/test/plugin-inspector-checkout-lock.test.mjs index 816cd4f0b..3d7ea3692 100644 --- a/test/plugin-inspector-checkout-lock.test.mjs +++ b/test/plugin-inspector-checkout-lock.test.mjs @@ -172,3 +172,79 @@ test("lock release failure retains the original command failure and foreign owne assert.equal(readFileSync(path.join(lock, "foreign-owner"), "utf8"), "retained"); assert.equal(existsSync(marker), false); }); + +test("Windows preparation expiry releases only a nonadmitted checkout before the next resolver", { + skip: process.platform !== "win32", timeout: 60_000, +}, async (t) => { + const root = mkdtempSync(path.join(os.tmpdir(), "crabpot-native-checkout-")); + const scripts = path.join(root, "scripts"); + mkdirSync(scripts); + const sourceUrl = new URL("../scripts/", import.meta.url); + for (const name of ["plugin-inspector-source.mjs", "owned-command.mjs", "owned-command-windows.ps1", "portable-command.mjs"]) { + copyFileSync(new URL(name, sourceUrl), path.join(scripts, name)); + } + writeFileSync(path.join(root, "package.json"), '{"type":"module"}\n'); + writeFileSync(path.join(scripts, "manifest-lib.mjs"), "export const repoRoot = " + JSON.stringify(root) + ";\n"); + const resolver = path.join(scripts, "plugin-inspector-source.mjs"); + // Replace Git/npm payloads only. The real resolver, owner Worker, named pipe, + // native admission, handles, cleanup receipts and checkout token stay intact. + writeFileSync(resolver, readFileSync(resolver, "utf8").replace('from "./owned-command.mjs"', 'from "./checkout-command.mjs"')); + writeFileSync(path.join(scripts, "checkout-command.mjs"), [ + 'import { runOwnedCommand as native, configuredTimeoutMs } from "./owned-command.mjs";', + 'export { configuredTimeoutMs };', + 'export function runOwnedCommand(command, args, options) {', + ' return native(process.execPath, [' + JSON.stringify(path.join(scripts, "checkout-target.mjs")) + ', command, ...args], options);', + '}', + ].join("\n")); + const calls = path.join(root, "target-calls.jsonl"); + writeFileSync(path.join(scripts, "checkout-target.mjs"), [ + 'import { appendFileSync, mkdirSync, writeFileSync } from "node:fs";', + 'import path from "node:path";', + 'const [command, ...args] = process.argv.slice(2);', + 'appendFileSync(' + JSON.stringify(calls) + ', JSON.stringify({ pid: process.pid, command, args }) + "\\n");', + 'if (args.includes("rev-parse")) process.stdout.write(' + JSON.stringify(pluginInspectorRef + "\n") + ');', + 'else if (command === "git" && args[0] === "init") mkdirSync(args[1], { recursive: true });', + 'else if (command === "git" && args.includes("checkout")) {', + ' mkdirSync(path.join(args[1], "src"), { recursive: true });', + ' writeFileSync(path.join(args[1], "src/index.js"), "export {};\\n");', + '} else if (command === "npm") mkdirSync(path.join(process.cwd(), "node_modules"), { recursive: true });', + ].join("\n")); + const helperPath = path.join(scripts, "owned-command-windows.ps1"); + const helper = readFileSync(helperPath, "utf8"); + const seam = ' Write-Phase "preparation-started"'; + assert.equal(helper.split(seam).length, 2); + const helperPidFile = path.join(root, "helper-pid"); + const escaped = path.join(root, "helper-expired"); + const ps = (value) => "'" + value.replaceAll("'", "''") + "'"; + writeFileSync(helperPath, helper.replace(seam, seam + "\n" + [ + '[System.IO.File]::WriteAllText(' + ps(helperPidFile) + ', [string]$PID)', + '[System.Threading.Thread]::Sleep(20000)', + '[System.IO.File]::WriteAllText(' + ps(escaped) + ', "fixture expired")', + ].map((line) => " " + line).join("\n"))); + let helperPid; + t.after(() => { + if (helperPid) assert.throws(() => process.kill(helperPid, 0), { code: "ESRCH" }, "exact helper must close before removal"); + rmSync(root, { recursive: true, force: true }); + }); + const { resolvePluginInspectorCliPath } = await import(pathToFileURL(resolver).href); + let failure; + try { resolvePluginInspectorCliPath(); } catch (error) { failure = error; } + helperPid = Number(readFileSync(helperPidFile, "utf8")); + assert.equal(failure?.code, "EOWNERSTART"); + assert.equal(failure.cleanupError, undefined); + assert.throws(() => process.kill(helperPid, 0), { code: "ESRCH" }, "helper closure precedes the second resolver"); + const parent = path.join(root, ".crabpot/plugin-inspector"); + const lock = path.join(parent, ".checkout.lock"); + const marker = path.join(parent, pluginInspectorRef, "node_modules/.crabpot-install-ready"); + assert.equal(existsSync(lock), false); + assert.equal(existsSync(marker), false); + assert.equal(existsSync(calls), false, "the interrupted preparation admitted no checkout payload"); + assert.equal(existsSync(escaped), false, "fixture expiry cannot provide closure"); + writeFileSync(helperPath, helper); + assert.equal(resolvePluginInspectorCliPath(), path.join(parent, pluginInspectorRef, "src/cli.js")); + assert.equal(existsSync(lock), false); + assert.equal(readFileSync(marker, "utf8"), pluginInspectorRef + "\n"); + const observed = readFileSync(calls, "utf8").trim().split("\n").map(JSON.parse); + assert.deepEqual(observed.map(({ command }) => command), ["git", "git", "git", "npm", "git"]); + for (const { pid } of observed) assert.throws(() => process.kill(pid, 0), { code: "ESRCH" }); +}); From 232289d36b7e77a1c523ea84a5fda562c53df742 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 15 Oct 2026 23:21:39 +0800 Subject: [PATCH 2/3] fix(ci): bind Windows ABI checks to the Type overload --- scripts/owned-command-windows.ps1 | 7 +++-- .../owned-command-compiler-observer.ps1 | 31 +++++++++++++------ test/owned-command.test.mjs | 12 +++++-- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/scripts/owned-command-windows.ps1 b/scripts/owned-command-windows.ps1 index aa4636770..10a41fdbd 100644 --- a/scripts/owned-command-windows.ps1 +++ b/scripts/owned-command-windows.ps1 @@ -65,9 +65,10 @@ try { $startupExType = Define-Struct "StartupInfoEx" @(@("Startup", $startupType), @("Attributes", [IntPtr])) $processType = Define-Struct "ProcessInfo" @( @("Process", [IntPtr]), @("Thread", [IntPtr]), @("ProcessId", [uint32]), @("ThreadId", [uint32])) - if ([System.Runtime.InteropServices.Marshal]::SizeOf($startupExType) -ne 112 -or - [System.Runtime.InteropServices.Marshal]::SizeOf($limitsType) -ne 144 -or - [System.Runtime.InteropServices.Marshal]::SizeOf($accountingType) -ne 48) { throw "Windows Job ownership ABI mismatch" } + # Explicit Type casts avoid PowerShell selecting SizeOf(T) for RuntimeType. + if ([System.Runtime.InteropServices.Marshal]::SizeOf([type]$startupExType) -ne 112 -or + [System.Runtime.InteropServices.Marshal]::SizeOf([type]$limitsType) -ne 144 -or + [System.Runtime.InteropServices.Marshal]::SizeOf([type]$accountingType) -ne 48) { throw "Windows Job ownership ABI mismatch" } $builder = $module.DefineType("CrabpotCommandNative", [System.Reflection.TypeAttributes]"Public, Abstract, Sealed") $dllImport = [System.Runtime.InteropServices.DllImportAttribute] $attribute = [System.Reflection.Emit.CustomAttributeBuilder]::new( diff --git a/test/fixtures/owned-command-compiler-observer.ps1 b/test/fixtures/owned-command-compiler-observer.ps1 index fd5354639..d109d1b58 100644 --- a/test/fixtures/owned-command-compiler-observer.ps1 +++ b/test/fixtures/owned-command-compiler-observer.ps1 @@ -32,8 +32,6 @@ public static class CompilerObserver [DllImport("kernel32.dll")] static extern uint SuspendThread(IntPtr thread); [DllImport("kernel32.dll")] - static extern uint ResumeThread(IntPtr thread); - [DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr handle); static int CompilerChild(int parent) @@ -117,16 +115,31 @@ public static class CompilerObserver } finally { - foreach (IntPtr thread in threads) { ResumeThread(thread); CloseHandle(thread); } - if (compiler != null) + try { - if (!compiler.WaitForExit(3000)) { compiler.Kill(); compiler.WaitForExit(3000); } - compiler.Dispose(); + // Rescue is deliberately after the extinction assertion receipt. + // Kill only these retained process handles, including the target + // that intentionally escaped the negative control's missing Job. + foreach (Process process in new Process[] { compiler, target }) + { + if (process == null) continue; + try { if (!process.HasExited) process.Kill(); } + catch (InvalidOperationException) { } + catch (System.ComponentModel.Win32Exception) { } + } + Stopwatch rescue = Stopwatch.StartNew(); + while ((compiler != null && !compiler.HasExited) || (target != null && !target.HasExited)) + { + if (rescue.ElapsedMilliseconds >= 3000) + throw new InvalidOperationException("retained compilation processes did not close after rescue"); + Thread.Sleep(10); + } } - if (target != null) + finally { - if (!target.WaitForExit(1000)) { target.Kill(); target.WaitForExit(3000); } - target.Dispose(); + foreach (IntPtr thread in threads) CloseHandle(thread); + if (compiler != null) compiler.Dispose(); + if (target != null) target.Dispose(); } } } diff --git a/test/owned-command.test.mjs b/test/owned-command.test.mjs index 885a6d7c0..520885744 100644 --- a/test/owned-command.test.mjs +++ b/test/owned-command.test.mjs @@ -747,6 +747,7 @@ for (const [ownerLost, guarded] of [[false, true], [true, true], [false, false], let worker; let compiler; t.after(async () => { + let rescued = false; try { await writeFile(path.join(root, "observer-rescue"), "stop"); await worker?.terminate(); @@ -756,12 +757,19 @@ for (const [ownerLost, guarded] of [[false, true], [true, true], [false, false], assert.equal(observerResult?.status, 0, observerError()); assert.ifError(observerResult.error); assert.equal(await waitForExit(compiler.pid, 1000), true, "fixture rescue must leave no compiler process"); + assert.equal(await waitForExit(compiler.targetPid, 1000), true, "fixture rescue must leave no target process"); } + rescued = completed && observerResult?.status === 0 && !observerResult?.error; } finally { - await observer.terminate(); sentinel.kill(); await sentinelClosed; - await rm(root, { recursive: true, force: true }); + if (rescued) { + await observer.terminate(); + await rm(root, { recursive: true, force: true }); + } else { + // Do not interrupt a still-running retained-handle rescue on failure. + t.diagnostic("compiler fixture retained: native rescue closure unconfirmed"); + } } }); await readReceipt(path.join(root, "observer-ready"), 10_000, observerError); From 18f6d84e5b2e09f3476f62ad66695c8062fd3b86 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 15 Oct 2026 10:53:41 -0700 Subject: [PATCH 3/3] test: expose native compiler observer to code scanning --- .../owned-command-compiler-observer.cs | 143 +++++++++++++++++ .../owned-command-compiler-observer.ps1 | 146 +----------------- 2 files changed, 144 insertions(+), 145 deletions(-) create mode 100644 test/fixtures/owned-command-compiler-observer.cs diff --git a/test/fixtures/owned-command-compiler-observer.cs b/test/fixtures/owned-command-compiler-observer.cs new file mode 100644 index 000000000..9e181a158 --- /dev/null +++ b/test/fixtures/owned-command-compiler-observer.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using System.Threading; + +public static class CompilerObserver +{ + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + struct ProcessEntry + { + public uint Size, Usage, Pid; + public UIntPtr Heap; + public uint Module, Threads, Parent; + public int Priority; + public uint Flags; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] + public string Name; + } + [DllImport("kernel32.dll", SetLastError = true)] + static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint pid); + [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] + static extern bool Process32FirstW(IntPtr snapshot, ref ProcessEntry entry); + [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] + static extern bool Process32NextW(IntPtr snapshot, ref ProcessEntry entry); + [DllImport("kernel32.dll")] + static extern IntPtr OpenThread(uint access, bool inherit, uint id); + [DllImport("kernel32.dll")] + static extern uint SuspendThread(IntPtr thread); + [DllImport("kernel32.dll")] + static extern bool CloseHandle(IntPtr handle); + + static int CompilerChild(int parent) + { + IntPtr snapshot = CreateToolhelp32Snapshot(2, 0); + if (snapshot == new IntPtr(-1)) throw new InvalidOperationException("process snapshot failed"); + try + { + ProcessEntry entry = new ProcessEntry(); + entry.Size = (uint)Marshal.SizeOf(typeof(ProcessEntry)); + bool next = Process32FirstW(snapshot, ref entry); + while (next) + { + if (entry.Parent == parent && String.Equals(entry.Name, "csc.exe", StringComparison.OrdinalIgnoreCase)) + return (int)entry.Pid; + next = Process32NextW(snapshot, ref entry); + } + return 0; + } + finally { CloseHandle(snapshot); } + } + + static void Publish(string root, string name, string json) + { + string pending = Path.Combine(root, name + ".pending"); + File.WriteAllText(pending, json); + File.Move(pending, Path.Combine(root, name)); + } + + public static void Run(string root) + { + Process compiler = null; + Process target = null; + List threads = new List(); + Stopwatch clock = Stopwatch.StartNew(); + try + { + Publish(root, "observer-ready", "{}"); + string helperFile = Path.Combine(root, "target-pid"); + string rescueFile = Path.Combine(root, "observer-rescue"); + int helper = 0; + while (helper == 0 && clock.ElapsedMilliseconds < 15000 && !File.Exists(rescueFile)) + { + if (File.Exists(helperFile)) Int32.TryParse(File.ReadAllText(helperFile), out helper); + if (helper == 0) Thread.Sleep(5); + } + if (helper == 0) throw new InvalidOperationException("target was not observed"); + target = Process.GetProcessById(helper); + IntPtr targetHeld = target.Handle; + if (target.ProcessName != "powershell" || target.HasExited) + throw new InvalidOperationException("compilation target identity changed"); + int pid = 0; + while (pid == 0 && clock.ElapsedMilliseconds < 20000 && !File.Exists(rescueFile)) + { + pid = CompilerChild(helper); + if (pid == 0) Thread.Sleep(1); + } + if (pid == 0) throw new InvalidOperationException("actual csc.exe child was not observed"); + compiler = Process.GetProcessById(pid); + // Retain the exact process handle before suspension or any later rescue. + IntPtr held = compiler.Handle; + if (compiler.ProcessName != "csc" || compiler.HasExited) + throw new InvalidOperationException("compiler identity changed before suspension"); + foreach (ProcessThread thread in compiler.Threads) + { + IntPtr handle = OpenThread(2, false, (uint)thread.Id); + if (handle == IntPtr.Zero) continue; + if (SuspendThread(handle) == UInt32.MaxValue) { CloseHandle(handle); continue; } + threads.Add(handle); + } + if (threads.Count == 0 || compiler.HasExited) + throw new InvalidOperationException("compiler was not alive and suspended"); + Publish(root, "compiler-suspended.json", + "{\"targetPid\":" + helper + ",\"pid\":" + pid + ",\"name\":\"csc.exe\",\"suspended\":true}"); + while (!compiler.WaitForExit(20) && clock.ElapsedMilliseconds < 25000 && + !File.Exists(rescueFile)) { } + bool rescued = !compiler.HasExited; + Publish(root, "compiler-exited.json", + "{\"pid\":" + pid + ",\"exited\":" + (!rescued ? "true" : "false") + + ",\"rescued\":" + (rescued ? "true" : "false") + "}"); + } + finally + { + try + { + // Rescue is deliberately after the extinction assertion receipt. + // Kill only these retained process handles, including the target + // that intentionally escaped the negative control's missing Job. + foreach (Process process in new Process[] { compiler, target }) + { + if (process == null) continue; + try { if (!process.HasExited) process.Kill(); } + catch (InvalidOperationException) { } + catch (System.ComponentModel.Win32Exception) { } + } + Stopwatch rescue = Stopwatch.StartNew(); + while ((compiler != null && !compiler.HasExited) || (target != null && !target.HasExited)) + { + if (rescue.ElapsedMilliseconds >= 3000) + throw new InvalidOperationException("retained compilation processes did not close after rescue"); + Thread.Sleep(10); + } + } + finally + { + foreach (IntPtr thread in threads) CloseHandle(thread); + if (compiler != null) compiler.Dispose(); + if (target != null) target.Dispose(); + } + } + } +} diff --git a/test/fixtures/owned-command-compiler-observer.ps1 b/test/fixtures/owned-command-compiler-observer.ps1 index 4a6d1feed..fbb5d8a0b 100644 --- a/test/fixtures/owned-command-compiler-observer.ps1 +++ b/test/fixtures/owned-command-compiler-observer.ps1 @@ -4,149 +4,5 @@ $ErrorActionPreference = "Stop" $started = [System.IO.Path]::Combine($Root, "observer-started") [System.IO.File]::WriteAllText($started + ".pending", '{"pid":' + $PID + '}') [System.IO.File]::Move($started + ".pending", $started) -Add-Type -TypeDefinition @' -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Runtime.InteropServices; -using System.Threading; - -public static class CompilerObserver -{ - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - struct ProcessEntry - { - public uint Size, Usage, Pid; - public UIntPtr Heap; - public uint Module, Threads, Parent; - public int Priority; - public uint Flags; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] - public string Name; - } - [DllImport("kernel32.dll", SetLastError = true)] - static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint pid); - [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] - static extern bool Process32FirstW(IntPtr snapshot, ref ProcessEntry entry); - [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] - static extern bool Process32NextW(IntPtr snapshot, ref ProcessEntry entry); - [DllImport("kernel32.dll")] - static extern IntPtr OpenThread(uint access, bool inherit, uint id); - [DllImport("kernel32.dll")] - static extern uint SuspendThread(IntPtr thread); - [DllImport("kernel32.dll")] - static extern bool CloseHandle(IntPtr handle); - - static int CompilerChild(int parent) - { - IntPtr snapshot = CreateToolhelp32Snapshot(2, 0); - if (snapshot == new IntPtr(-1)) throw new InvalidOperationException("process snapshot failed"); - try - { - ProcessEntry entry = new ProcessEntry(); - entry.Size = (uint)Marshal.SizeOf(typeof(ProcessEntry)); - bool next = Process32FirstW(snapshot, ref entry); - while (next) - { - if (entry.Parent == parent && String.Equals(entry.Name, "csc.exe", StringComparison.OrdinalIgnoreCase)) - return (int)entry.Pid; - next = Process32NextW(snapshot, ref entry); - } - return 0; - } - finally { CloseHandle(snapshot); } - } - - static void Publish(string root, string name, string json) - { - string pending = Path.Combine(root, name + ".pending"); - File.WriteAllText(pending, json); - File.Move(pending, Path.Combine(root, name)); - } - - public static void Run(string root) - { - Process compiler = null; - Process target = null; - List threads = new List(); - Stopwatch clock = Stopwatch.StartNew(); - try - { - Publish(root, "observer-ready", "{}"); - string helperFile = Path.Combine(root, "target-pid"); - string rescueFile = Path.Combine(root, "observer-rescue"); - int helper = 0; - while (helper == 0 && clock.ElapsedMilliseconds < 15000 && !File.Exists(rescueFile)) - { - if (File.Exists(helperFile)) Int32.TryParse(File.ReadAllText(helperFile), out helper); - if (helper == 0) Thread.Sleep(5); - } - if (helper == 0) throw new InvalidOperationException("target was not observed"); - target = Process.GetProcessById(helper); - IntPtr targetHeld = target.Handle; - if (target.ProcessName != "powershell" || target.HasExited) - throw new InvalidOperationException("compilation target identity changed"); - int pid = 0; - while (pid == 0 && clock.ElapsedMilliseconds < 20000 && !File.Exists(rescueFile)) - { - pid = CompilerChild(helper); - if (pid == 0) Thread.Sleep(1); - } - if (pid == 0) throw new InvalidOperationException("actual csc.exe child was not observed"); - compiler = Process.GetProcessById(pid); - // Retain the exact process handle before suspension or any later rescue. - IntPtr held = compiler.Handle; - if (compiler.ProcessName != "csc" || compiler.HasExited) - throw new InvalidOperationException("compiler identity changed before suspension"); - foreach (ProcessThread thread in compiler.Threads) - { - IntPtr handle = OpenThread(2, false, (uint)thread.Id); - if (handle == IntPtr.Zero) continue; - if (SuspendThread(handle) == UInt32.MaxValue) { CloseHandle(handle); continue; } - threads.Add(handle); - } - if (threads.Count == 0 || compiler.HasExited) - throw new InvalidOperationException("compiler was not alive and suspended"); - Publish(root, "compiler-suspended.json", - "{\"targetPid\":" + helper + ",\"pid\":" + pid + ",\"name\":\"csc.exe\",\"suspended\":true}"); - while (!compiler.WaitForExit(20) && clock.ElapsedMilliseconds < 25000 && - !File.Exists(rescueFile)) { } - bool rescued = !compiler.HasExited; - Publish(root, "compiler-exited.json", - "{\"pid\":" + pid + ",\"exited\":" + (!rescued ? "true" : "false") + - ",\"rescued\":" + (rescued ? "true" : "false") + "}"); - } - finally - { - try - { - // Rescue is deliberately after the extinction assertion receipt. - // Kill only these retained process handles, including the target - // that intentionally escaped the negative control's missing Job. - foreach (Process process in new Process[] { compiler, target }) - { - if (process == null) continue; - try { if (!process.HasExited) process.Kill(); } - catch (InvalidOperationException) { } - catch (System.ComponentModel.Win32Exception) { } - } - Stopwatch rescue = Stopwatch.StartNew(); - while ((compiler != null && !compiler.HasExited) || (target != null && !target.HasExited)) - { - if (rescue.ElapsedMilliseconds >= 3000) - throw new InvalidOperationException("retained compilation processes did not close after rescue"); - Thread.Sleep(10); - } - } - finally - { - foreach (IntPtr thread in threads) CloseHandle(thread); - if (compiler != null) compiler.Dispose(); - if (target != null) target.Dispose(); - } - } - } -} -'@ +Add-Type -Path ([System.IO.Path]::Combine($PSScriptRoot, 'owned-command-compiler-observer.cs')) [CompilerObserver]::Run($Root)